package com.bizofficer.admin.students;



import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.crossstore.ChangeSetPersister.NotFoundException;
import org.springframework.stereotype.Service;

import com.bizofficer.hibernate.entity.PgprepAdministrator;
import com.bizofficer.hibernate.entity.PgprepStudentCourses;
import com.bizofficer.hibernate.entity.PgprepStudentDetails;
import com.bizofficer.hibernate.entity.SubscriptionCourses;
import com.bizofficer.hibernate.repository.PgprepAdministratorRepository;
import com.bizofficer.hibernate.repository.PgprepStudentCoursesRepository;
import com.bizofficer.hibernate.repository.SubscriptionCoursesRepository;
import com.bizofficer.util.module.PrivilegesBeans;
import com.bizofficer.util.module.PrivilegesUtil;
import com.bizofficer.util.module.RoleModules;
import com.bizofficer.util.system.MakeDate;


@Service
public class StudentUpdateBatchAdminService{
	
	private static final Logger logger = Logger.getLogger(StudentUpdateBatchAdminService.class);
	
	@Autowired
	EntityManagerFactory entityManagerFactory;
	
	@Autowired
	PgprepAdministratorRepository adminRepo;
	
	@Autowired
	PgprepStudentCoursesRepository corsRepo;
	
	@Autowired
	SubscriptionCoursesRepository  subCorsRepo;
	
	public Object execute(Object obj) throws NotFoundException{
		StudentAdminBean objBean = (StudentAdminBean) obj;
		StudentAdmResponsePojo responseObj = new StudentAdmResponsePojo();
		
		try {
			
			logger.info("getLoginEmailId >> " + objBean.getLoginEmailId() );
			logger.info("Token >> " + objBean.getToken() );

			logger.info("Auto Id >> " + objBean.getId() );
			logger.info("schoolId >> " + objBean.getSchoolId() );
			logger.info("SchoolName >> " + objBean.getSchoolName() );
			logger.info("ClassName >> " + objBean.getClassName() );
			logger.info("CourseId >> " + objBean.getCourseId() );
			logger.info("OLD ClassCourseId >> " + objBean.getClassCourseId() );
			
			if(objBean.getLoginEmailId()==null || objBean.getToken()==null || objBean.getSchoolId()==null) {
				return responseObj;
			}
			
			PgprepAdministrator adminObj = (PgprepAdministrator) adminRepo.findByEmailIdAndLoginToken(objBean.getLoginEmailId(), objBean.getToken());
			if(adminObj==null) {
				logger.info("InvalidToken");
				responseObj.setResponseTxt("InvalidToken");
				return responseObj;
			}
			logger.info("Verify >> " + adminObj.getId() );
			
			if(adminObj.getUserType()==0 && adminObj.getRoleId()!=null && adminObj.getPrivileges()!=null){				
				PrivilegesUtil	myPgUtil = new PrivilegesUtil();
				PrivilegesBeans prvgbnObj = myPgUtil.authPrivileges(adminObj.getPrivileges(), RoleModules.getStudents() );
				logger.info(prvgbnObj.toString());
				if(prvgbnObj==null || (prvgbnObj!=null && (prvgbnObj.getUpdate()==null || prvgbnObj.getUpdate()==0))) {
					logger.info("NoAuthorization");
					responseObj.setResponseTxt("NoAuthorization");
					return responseObj;
				}
			}
			
			EntityManager entityManager = entityManagerFactory.createEntityManager();
			entityManager.getTransaction().begin();

			PgprepStudentDetails findObj = (PgprepStudentDetails)entityManager.find(PgprepStudentDetails.class ,objBean.getId());
			
			if(objBean.getSchoolId()!=null && objBean.getSchoolId()>0) {
				findObj.setSchoolId(objBean.getSchoolId());
				findObj.setSchoolName(objBean.getSchoolName());			
			}else {
				findObj.setSchoolId(null);
				findObj.setSchoolName(null);
			}
			
			findObj.setClassName(null);		
			findObj.setClassCourseId(null);
			
			if(objBean.getCourseId()!=null && objBean.getCourseId()>0) {				
				
				findObj.setClassName(objBean.getClassName());
				findObj.setClassCourseId(objBean.getCourseId());
				
				PgprepStudentCourses checkExistingObj = null;
				if(objBean.getClassCourseId()!=null && objBean.getClassCourseId()>0) {
					checkExistingObj = corsRepo.findIdBySidAndCourseId(objBean.getId(), objBean.getClassCourseId());
					if(checkExistingObj==null) {
						responseObj.setResponseTxt("OLD Course Not Found");
						return responseObj;
					}	
				}				
				if(checkExistingObj==null) {
					checkExistingObj = corsRepo.findIdBySidAndCourseId(objBean.getId(), objBean.getCourseId());
				}
				
				SubscriptionCourses mySCors =  subCorsRepo.findByCourseId(objBean.getCourseId());
				
				logger.info("Validity Date: " + mySCors.getValidityDate() );
				
				if(checkExistingObj!=null && checkExistingObj.getId()>0) {			
					PgprepStudentCourses saveCorsObj = (PgprepStudentCourses)entityManager.find(PgprepStudentCourses.class ,checkExistingObj.getId());
					saveCorsObj.setCourseId(mySCors.getCourseId());
					saveCorsObj.setCourseTitle(mySCors.getCourseTitle());
					saveCorsObj.setOrganisationId(mySCors.getPaidOrganisationId());
					saveCorsObj.setSubscriptionType("PAID");
					
					if(mySCors.getValidityDate()!=null) {
						MakeDate dateObj = new MakeDate();
						saveCorsObj.setValidTillDate(dateObj.getStringToDate(mySCors.getValidityDate().toString(), "yyyy-MM-dd"));
					}
					
				}else {
					PgprepStudentCourses saveObj = new PgprepStudentCourses();
					saveObj.setSid(objBean.getId());
					saveObj.setStudentId(objBean.getStudentId());
					saveObj.setSubscriptionType("PAID");
					
					saveObj.setCourseId(mySCors.getCourseId());
					saveObj.setCourseTitle(mySCors.getCourseTitle());
					saveObj.setOrganisationId(mySCors.getPaidOrganisationId());
					
					if(mySCors.getValidityDate()!=null) {
						MakeDate dateObj = new MakeDate();
						saveObj.setValidTillDate(dateObj.getStringToDate(mySCors.getValidityDate().toString(), "yyyy-MM-dd"));
					}
					
					
					corsRepo.save(saveObj);
				}
				
			}
			
			
				
			entityManager.getTransaction().commit();
			entityManager.close();

			responseObj.setResponseTxt("OK");

			
		}catch(Exception e) {
			e.getStackTrace();
		}
		
		
		return responseObj;
	}
	

	
	

	
	
}
