package com.bizofficer.parents.web;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Service
public class ParentStudentNewController {
	
	private static final Logger logger = Logger.getLogger(ParentStudentNewController.class);

	@Autowired
	TokenVerifyParentsService objTokenVerifyParentsService;
	
	@Autowired
	private ParentStudentNewService serviceObj;
	
	@Autowired
	private ParentsChildrenService serviceListObj;
	
	@RequestMapping(value = {"/parents-add-students"}, method = {RequestMethod.POST})
	public ResponseEntity<Object> execute(@ModelAttribute("SpringWeb")ParentsRequestBean objBean, @RequestHeader("token") String token) {
		
		try {
			
			objBean.setToken(token);
			TokenVerifyParentsPojo objTokenVerifyResponse = (TokenVerifyParentsPojo) (Object) objTokenVerifyParentsService.execute(objBean);
			if(objTokenVerifyResponse.getResponseTxt().equals("success")) {
				objBean.setParentId(objTokenVerifyResponse.getParentId());
				ParentsResponse tsrbObj = (ParentsResponse) (Object) serviceObj.execute(objBean);
				
				if(tsrbObj.getResponseTxt().equals("success")) {
					ParentsResponse resObj = (ParentsResponse) (Object) serviceListObj.execute(objBean);
					tsrbObj.setList(resObj.getList());
				}
				
				HttpHeaders headers = new HttpHeaders();
		        return ResponseEntity.ok().headers(headers).body(tsrbObj);
			        
			}
			
			logger.info("Token Verify ResponseTxt: "+objTokenVerifyResponse.getResponseTxt());
			
			HttpHeaders headers = new HttpHeaders();
	        return ResponseEntity.ok().headers(headers).body(objTokenVerifyResponse);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
	}
	

	
	
}
