package com.bankaudit.rest.process.service;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.process.model.AuditSchedule;
import com.bankaudit.process.model.AuditTeam;
import com.bankaudit.process.model.AuditTeamWrk;
import com.bankaudit.rest.process.dao.AuditTeamDao;
import com.bankaudit.rest.process.dao.MyAuditsDao;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.service.MaintEntityService;

@Service("auditTeamService")
@Transactional("transactionManager")
public class AuditTeamServiceImpl implements AuditTeamService {

	@Autowired
	AuditTeamDao auditTeamDao;
	
	@Autowired
	MyAuditsDao myAuditsDao;
	
	@Autowired
	MaintEntityService maintEntityService;
	
	@Autowired
	BusinessDateService businessDateService;
	
	@Override
	public void createAuditTeamWrk(AuditTeamWrk auditTeamWrk) {
		auditTeamDao.save(auditTeamWrk);
	}
	
	@Override
	public DataTableResponse getAuditsList(int legalEntityCode, String userId, String search, Integer orderColumn,
			String orderDirection, Integer page, Integer size ) {
		List<String> unitsList=maintEntityService.getSubBranchesGyUserIdOrUnitId(legalEntityCode,"user", userId);
		DataTableResponse ds=auditTeamDao.getAuditsList(legalEntityCode,userId,search,orderColumn,
				orderDirection,page,size, unitsList);
		return ds;
	}
	
	@Override
	public AuditSchedule getAuditByPlanIdAndAuditId(int legalEntityCode, String auditTypeCode, String planId,
			String auditId) {
		AuditSchedule auditDtls=auditTeamDao.getAuditByPlanIdAndAuditId(legalEntityCode,auditTypeCode,planId,auditId); 
		return auditDtls; 
	}
	
	@Override
	public List<AuditTeam> getAuditTeamsByAuditId(int legalEntityCode,String auditTypeCode, String planId,String auditId){
		return auditTeamDao.getAuditTeamsByAuditId(legalEntityCode, auditTypeCode, planId, auditId);
	}
	
	@Override
	public List<AuditTeam> getAuditTeamsByAuditIdAndType(int legalEntityCode,String auditTypeCode, String auditId){
		return auditTeamDao.getAuditTeamsByAuditIdAndType(legalEntityCode, auditTypeCode, auditId);
	}
	
	@Override
	public AuditTeam getAuditTeamLeadByAuditIdAndType(int legalEntityCode,String auditTypeCode, String auditId){
		return auditTeamDao.getAuditTeamLeadByAuditIdAndType(legalEntityCode, auditTypeCode, auditId);
	}
	
	@Override
	public void updateAuditTeams(AuditSchedule auditSchedule) {
		if(!auditSchedule.getTeamStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){
			if(auditSchedule.getTeamStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)
					|| auditSchedule.getTeamStatus().equalsIgnoreCase(BankAuditConstant.STATUS_MOD)){
				auditSchedule.setTeamMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditSchedule.getLegalEntityCode()));
			}

			// get Db object and save into the history
			/*MaintAuditActivity maintAuditActivityDb=getMaintAuditActivity(maintAuditActivity.getLegalEntityCode(), maintAuditActivity.getActivityId(), BankAuditConstant.STATUS_UNAUTH);
			if(maintAuditActivityDb!=null){
				MaintAuditActivityHst MaintAuditActivityHst=new MaintAuditActivityHst();
				BeanUtils.copyProperties(maintAuditActivityDb, MaintAuditActivityHst);
				maintAuditActivityDao.save(MaintAuditActivityHst);
			}*/
			auditTeamDao.deleteAuditTeam(auditSchedule.getLegalEntityCode(), auditSchedule.getAuditId(), BankAuditConstant.STATUS_UNAUTH);
			if (auditSchedule.getAuditTeams()!=null){
				for (AuditTeam auditTeam : auditSchedule.getAuditTeams()) {
					AuditTeamWrk auditTeamWrk = new AuditTeamWrk();
					BeanUtils.copyProperties(auditTeam, auditTeamWrk);
					auditTeamDao.flushSession();
					auditTeamDao.saveOrUpdate(auditTeamWrk);
				}
			}
		}else{
			auditTeamDao.deleteAuditTeam(auditSchedule.getLegalEntityCode(), auditSchedule.getAuditId(), BankAuditConstant.STATUS_AUTH);
			if (auditSchedule.getAuditTeams()!=null){
				for (AuditTeam auditTeam : auditSchedule.getAuditTeams()) {
					//AuditTeamWrk auditTeamWrk = new AuditTeamWrk();
					//BeanUtils.copyProperties(auditTeam, auditTeamWrk);
					auditTeamDao.flushSession();
					auditTeamDao.saveOrUpdate(auditTeam);
				}
			}
			auditTeamDao.deleteAuditTeam(auditSchedule.getLegalEntityCode(), auditSchedule.getAuditId(), BankAuditConstant.STATUS_UNAUTH);
		}
		myAuditsDao.updateAuditTeamStatus(auditSchedule); // to updated the Maker, Checker and Status of Team
	}
	
	@Override
	public void deleteAuditTeamByIdAndType(Integer legalEntityCode, String auditId, String auditTypeCode, String statusUnauth){
		auditTeamDao.deleteAuditTeamByIdAndType(legalEntityCode, auditId, auditTypeCode, statusUnauth);
	}

}
