package com.nisum.myteam.service.impl;

import com.nisum.myteam.model.dao.Employee;
import com.nisum.myteam.model.dao.EmployeeSubStatus;
import com.nisum.myteam.model.vo.EmployeeSubStatusVO;
import com.nisum.myteam.repository.EmployeeRepo;
import com.nisum.myteam.repository.EmployeeSubStatusRepo;
import com.nisum.myteam.service.ISubStatusService;
import com.nisum.myteam.utils.MyTeamDateUtils;
import com.nisum.myteam.utils.MyTeamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SubStatusService implements ISubStatusService {

    @Autowired
    private EmployeeSubStatusRepo employeeSubStatusRepo;
    @Autowired
    private EmployeeRepo employeeRepo;

    public HashMap<String, Object> respMap = new HashMap<>();


    @Override
    public EmployeeSubStatus getLatestEmployeeSubStatus(String empId) {
        List<EmployeeSubStatus> employeeSubStatusList = employeeSubStatusRepo.findByEmployeeID(empId);
        EmployeeSubStatus latestSubStatus = null;
        if(!employeeSubStatusList.isEmpty()){
            latestSubStatus = employeeSubStatusList.get(0);
            for(EmployeeSubStatus s:employeeSubStatusList) {
                if(s.getToDate().after(latestSubStatus.getToDate())){
                    latestSubStatus = s;
                }
            }
        }
        return latestSubStatus;
    }


    @Override
    public EmployeeSubStatus updateExistingEmplyeeSubStatus(String loginnEmployeeId,EmployeeSubStatus employeeSubStatusReq) {
        employeeSubStatusReq.setFromDate(MyTeamDateUtils.getDayMoreThanDate(employeeSubStatusReq.getFromDate()));
        employeeSubStatusReq.setToDate(MyTeamDateUtils.getDayMoreThanDate(employeeSubStatusReq.getToDate()));
        employeeSubStatusReq.setAuditFields(loginnEmployeeId,MyTeamUtils.UPDATE);
        return employeeSubStatusRepo.save(employeeSubStatusReq);
    }

    @Override
    public EmployeeSubStatus addEmployeeSubStatus(String loginnEmployeeId,EmployeeSubStatus employeeSubStatus) {
        employeeSubStatus.setFromDate(MyTeamDateUtils.getDayMoreThanDate(employeeSubStatus.getFromDate()));
        employeeSubStatus.setToDate(MyTeamDateUtils.getDayMoreThanDate(employeeSubStatus.getToDate()));
        employeeSubStatus.setAuditFields(loginnEmployeeId,MyTeamUtils.CREATE);
        return employeeSubStatusRepo.insert(employeeSubStatus);
    }

    @Override
    public EmployeeSubStatus getCurrentSubStatus(String employeeId){
        List<EmployeeSubStatus> currentEmployeeSubStatusList = employeeSubStatusRepo.findByEmployeeID(employeeId).stream().filter(
                s -> s.getFromDate().compareTo(new Date())<=0 && s.getToDate().compareTo(new Date())>=0).collect(Collectors.toList());
        if(!currentEmployeeSubStatusList.isEmpty()){
            return currentEmployeeSubStatusList.get(0);
        }else
            return null;
    }

    @Override
    public EmployeeSubStatus endSubStatus(String loginnEmployeeId,EmployeeSubStatus subStatus){
        EmployeeSubStatus currentSubStatus = getCurrentSubStatus(subStatus.getEmployeeID());
        if(currentSubStatus!=null){
            currentSubStatus.setToDate(MyTeamDateUtils.getDayLessThanDate(new Date()));
            currentSubStatus.setAuditFields(loginnEmployeeId,MyTeamUtils.UPDATE);
            updateExistingEmplyeeSubStatus(loginnEmployeeId,currentSubStatus);
        }
        return null;
    }
 //not called by ANY ONE
    public boolean isDatesValid(EmployeeSubStatus subStatus){
        boolean isValid = false;
        List<EmployeeSubStatus> subStatusList = employeeSubStatusRepo.findByEmployeeID(subStatus.getEmployeeID());
        List<EmployeeSubStatus> matchedList = subStatusList.stream().filter(s -> (s.getFromDate().compareTo(subStatus.getFromDate())<=0 && s.getToDate().compareTo(subStatus.getFromDate())>=0) &&
                (s.getFromDate().compareTo(subStatus.getToDate())<=0 && s.getToDate().compareTo(subStatus.getToDate())>=0)).collect(Collectors.toList());
        if(!matchedList.isEmpty()){
        	respMap.put("statusCode", 811);
            respMap.put("message", "resource is already on substatus on these days");
            //resource is already on substatus on these days
        }else{
            isValid = true;

        }
        return isValid;
    }


	@Override
	public List<EmployeeSubStatusVO> employeesBasedOnSubStatusForGivenDates(Date fromDate, Date toDate, String subStatus) {
		List<EmployeeSubStatusVO> employees=new ArrayList<>();
		 employeeSubStatusRepo.findBySubStatus(subStatus).stream().filter(e->
		 ( e.getFromDate().compareTo(fromDate)<=0 && e.getToDate().compareTo(fromDate)>=0)
		 	|| (e.getFromDate().compareTo(fromDate)<=0 && e.getToDate().compareTo(fromDate)>=0)
		 )		
		.forEach(e->{
		Employee employee=employeeRepo.findByEmployeeIdAndEmpStatus(e.getEmployeeID(), MyTeamUtils.ACTIVE);
		if(employee!=null) {
		EmployeeSubStatusVO empSubStatus=new EmployeeSubStatusVO();
		empSubStatus.setEmpId(e.getEmployeeID());
		empSubStatus.setEmpName(employee.getEmployeeName());
		empSubStatus.setEmpStatus(e.getSubStatus());
		empSubStatus.setFromDate(e.getFromDate());
		empSubStatus.setToDate(e.getToDate());
		empSubStatus.setFunctionalGroup(employee.getFunctionalGroup());
		employees.add(empSubStatus);
		}});
		return employees;
	}

}
