package com.bizofficer.apiweb.testdetail;



import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

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.PgprepTestSummary;
import com.bizofficer.hibernate.repository.PgprepTestDetailsRepository;
import com.bizofficer.hibernate.repository.PgprepTestSummaryRepository;
import com.bizofficer.pojo.response.ListResponsePojo;
import com.fasterxml.jackson.databind.ObjectMapper;



@Service
public class TestDetailExistingService{
	
	private static final Logger logger = Logger.getLogger(TestDetailExistingService.class);
	
	@Autowired
	EntityManagerFactory entityManagerFactory;
	
	@Autowired
	PgprepTestDetailsRepository tdRepoObj;
	
	@Autowired
	PgprepTestSummaryRepository tsRepoObj;
	
	public Object execute(Object obj) throws NotFoundException{
		BeanTesting objBean = (BeanTesting) obj; 	
		ListResponsePojo responseObj = new ListResponsePojo();
		
		try {
			
			responseObj.setResponseTxt("failed");
			
			if(objBean.getTestSession()==null || objBean.getOrganisationId()==null || objBean.getStudentId()==null) {
				return responseObj;
			}
			
			logger.info("TestDetailExistingService Test Id: "+objBean.getTestSession() );
			logger.info("TestDetailExistingService Organisation Id: "+objBean.getOrganisationId() );
			logger.info("TestDetailExistingService Student Id: "+objBean.getStudentId() );
			
			EntityManager entityManager = entityManagerFactory.createEntityManager();
			entityManager.getTransaction().begin();
			
			TypedQuery<PgprepTestSummary> query = (TypedQuery<PgprepTestSummary>) entityManager.createQuery("from "+PgprepTestSummary.class.getName()+" where testStatus=0 and organisationId=:organisationId and studentId=:studentId and testSession=:testSession ", PgprepTestSummary.class);
			query.setParameter("organisationId", objBean.getOrganisationId());
			query.setParameter("studentId", objBean.getStudentId()); 
			query.setParameter("testSession", objBean.getTestSession());
			query.setFirstResult(0);
			query.setMaxResults(1);
			List<?> resultListCat = query.getResultList();
			Iterator<?> iteratorCat=resultListCat.iterator();	
			if(iteratorCat.hasNext()){
				PgprepTestSummary myObj = (PgprepTestSummary)iteratorCat.next();				
				String testDetail = (myObj.getTestDetail().substring(1, myObj.getTestDetail().length()-1));
				
				if(testDetail!=null && testDetail.length()>0) {
					List<TestingPageBean> list = new ArrayList<TestingPageBean>();
					ObjectMapper objectMapper = new ObjectMapper();
					TestingPageBean testingPageBean = objectMapper.readValue(testDetail, TestingPageBean.class);
					list.add(testingPageBean);
					
					responseObj.setList(list);
				}
				
			}
				
			entityManager.getTransaction().commit();
			entityManager.close();
			
			responseObj.setResponseTxt("success");
				
		}catch(Exception e) {
			e.getStackTrace();
		}
		
		
		return responseObj;
	}
	
	
	
	
}
