package com.nisum.myteam.service.impl;

import com.nisum.myteam.exception.handler.MyTeamException;
import com.nisum.myteam.model.dao.*;
import com.nisum.myteam.model.vo.EmployeeShiftsVO;
import com.nisum.myteam.model.vo.MyProjectAllocationVO;
import com.nisum.myteam.model.vo.ResourceVO;
import com.nisum.myteam.repository.ResourceRepo;
import com.nisum.myteam.service.*;
import com.nisum.myteam.statuscodes.ResourceStatus;
import com.nisum.myteam.utils.MyTeamDateUtils;
import com.nisum.myteam.utils.MyTeamUtils;
import com.nisum.myteam.utils.constants.Shifts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ResourceService implements IResourceService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ResourceRepo resourceRepo;

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IDomainService domainService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IEmployeeShiftService empShiftService;

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


    private Resource getLatestAllocation(List<Resource> resourceAllocList){
        Resource latestAlloc = null;
        if(!resourceAllocList.isEmpty()) {
            latestAlloc = resourceAllocList.get(0);
            for (Resource resource : resourceAllocList) {
                if (latestAlloc.getBillingEndDate().before(resource.getBillingEndDate()))
                    latestAlloc = resource;
            }
        }
        return latestAlloc;
    }
    public Resource addResource(Resource resourceReq, String loginEmpId) throws MyTeamException {

        List<Resource> resourceAllocList = resourceRepo.findByEmployeeIdAndProjectId(resourceReq.getEmployeeId(), resourceReq.getProjectId());

        Resource prevAllocation = this.getLatestAllocation(resourceAllocList);
        if (prevAllocation != null) {
            if (prevAllocation.getBillingEndDate().compareTo(new Date()) == 0) {
                prevAllocation.setBillingEndDate(new Date());
            } else {
                prevAllocation.setBillingEndDate(MyTeamDateUtils.getDayLessThanDate(resourceReq.getBillingStartDate())); //adding resource.
            }
            this.updateExistedResource(prevAllocation);
        }
        return resourceRepo.save(resourceReq);
    }

    public boolean isResourceExistsForProject(String employeeId, String projectId) {
        boolean isExists = false;

        List<Resource> resourceList = resourceRepo.findByEmployeeIdAndProjectId(employeeId, projectId);
        if (resourceList != null && resourceList.size() > 0) {
            isExists = true;
            respMap.put("message", "Resourse is already in the project");
            return isExists;
        }
        respMap.put("statusCode", 810);
        respMap.put("message", "Resource Not Found");
        return isExists;
    }




    public void updateResourceDetails(Resource resourceReq, String loginEmpId) throws MyTeamException {

//        List<Resource> resourceAllocList = resourceRepo.findByEmployeeIdAndProjectId(resourceReq.getEmployeeId(), resourceReq.getProjectId());
//
//        List<Resource> resourceListWithLatestRecord = resourceAllocList.stream().filter(resource -> resource.getBillingEndDate().compareTo(new Date()) >= 0).collect(Collectors.toList());
//
//        if (resourceListWithLatestRecord != null && resourceListWithLatestRecord.size() > 0) {
//
//            Resource resourcePrev = resourceListWithLatestRecord.get(0);                                     //latest resource record.
//            log.info("Requsting Resource Allocation BillingStart Date::::"+resourceReq.getBillingStartDate());
//            log.info("The before date is::" + MyTeamDateUtils.getDayLessThanDate(resourceReq.getBillingStartDate()));
//            if(!resourcePrev.getBillableStatus().equals(resourceReq.getBillableStatus())) {
//                if (resourcePrev.getBillingEndDate().compareTo(new Date()) == 0) {
//                    resourcePrev.setBillingEndDate(new Date());
//                } else {
//                    resourcePrev.setBillingEndDate(MyTeamDateUtils.getDayLessThanDate(resourceReq.getBillingStartDate())); //adding resource.
//                }
//                insertNewResourceWithNewStatus(resourceReq,loginEmpId);
//            }else {
//                resourcePrev.setResourceRole(resourceReq.getResourceRole());
//                resourcePrev.setBillingStartDate(resourceReq.getBillingStartDate());
//                resourcePrev.setBillingEndDate(resourceReq.getBillingEndDate());
//                //resourceAllocPrev.setBillingEndDate(); //adding resource.
//            }
//            log.info("After setting the date:::before saving the Resource::" + resourcePrev);
//            this.updateExistedResource(resourcePrev);
//        }
        Resource resource = resourceRepo.findById(resourceReq.getId());
        if(resource!=null){
            this.updateExistedResource(resourceReq);
        }else {
            respMap.put("statusCode", 801);
            respMap.put("message", "Record Not Found");
        }

    }

    public void updateExistedResource(Resource resource) {

        if (resource != null) {
            Resource resourcePers = resourceRepo.save(resource);
            respMap.put("statusCode", 801);
            respMap.put("message", "Resource updated successfully");
            respMap.put("resourceObj", resourcePers);

        }

    }

    public void insertNewResourceWithNewStatus(Resource resourceReq, String loginEmpId) throws MyTeamException {

        resourceReq.setId(null);
        Resource resourcePers = resourceRepo.insert(resourceReq);
        respMap.put("statusCode", 801);
        respMap.put("message", "Resource updated successfully");
        respMap.put("resourceObj", resourcePers);

    }
    public boolean validateAllocationAgainstPrevAllocation(Resource resourceReq){
        boolean isValid = true;
        List<Resource> resourceAllocList = resourceRepo.findByEmployeeIdAndProjectId(resourceReq.getEmployeeId(), resourceReq.getProjectId());
        Resource prevAllocation = this.getLatestAllocation(resourceAllocList);
        if (prevAllocation != null) {
            if(!prevAllocation.getBillingStartDate().before(resourceReq.getBillingStartDate())){
                respMap.put("statusCode", 811);
                respMap.put("message", "Billing start date should be after previous allocation start date");
                isValid = false;
            }
            if(prevAllocation.getBillableStatus().equalsIgnoreCase(resourceReq.getBillableStatus())){
                respMap.put("statusCode", 811);
                respMap.put("message", "Resource is already in "+prevAllocation.getBillableStatus()+" status only");
                isValid = false;
            }
        }
        return isValid;
    }
    public boolean validateBillingStartEndDateAgainstProjectStartEndDate(Resource resource, String loginEmpId) throws MyTeamException {

        boolean isValid = true;

        Project project = projectService.getProjectByProjectId(resource.getProjectId());
        log.info("Project::" + project);

        if (!resource.getBillingStartDate().after(project.getProjectStartDate())) {
            log.info("Billing start date should be after Project start date");
            respMap.put("statusCode", 811);
            respMap.put("message", "Billing start date should be after Project start date");
            isValid = false;
        }

        if (!resource.getBillingStartDate().before(resource.getBillingEndDate())) {
            log.info("Billing start date should be before Billing End Date.");
            respMap.put("statusCode", 812);
            respMap.put("message", "Billing start date should be before Billing End Date.");
            isValid = false;
        }
        log.info("ResourceALloc Req::" + resource);
        log.info("" + project.getProjectEndDate().toString());

        //if (!resourceAllocation.getBillingEndDate().before(project.getProjectEndDate())|| !resourceAllocation.getBillingEndDate().equals(project.getProjectEndDate())) {
          if(!(resource.getBillingEndDate().compareTo(project.getProjectEndDate())<=0))  {
            log.info("Billing end date should be on or before Project End Date.");
            respMap.put("statusCode", 813);
            respMap.put("message", "Billing end date should be before Project End Date.");
            isValid = false;
        }

        respMap.put("resourceObj", resource);
        return isValid;
    }


    public boolean validateBillingStartDateAgainstDOJ(Resource resource) {
        String message = "";
        boolean isValid = true;

        Employee employee = employeeService.getEmployeeById(resource.getEmployeeId());
        Date empDoj = employeeService.getEmployeeById(resource.getEmployeeId()).getDateOfJoining();
        if (resource.getBillingStartDate().compareTo(empDoj) < 0) {
            message = "Resource Billing Start Date (" + resource.getBillingStartDate() + " ) in "
                    + projectService.getProjectByProjectId(resource.getProjectId()).getProjectName()
                    + " project should not be before Date of Joining ( " + empDoj + ").";
            isValid = false;
            respMap.put("statusCode", 814);
            respMap.put("message", message);
        }
        return isValid;
    }


    public boolean isResourceAssignedToAnyProject(Resource resourceReq) {

        boolean isAssigned = false;
        String message = "";
        //List<Resource> resourceAllocList = resourceRepo.findByEmployeeIdAndProjectId(resourceAllocReq.getEmployeeId(), resourceAllocReq.getProjectId());
        List<Resource> resourceAllocList = resourceRepo.findByEmployeeId(resourceReq.getEmployeeId());

        List<Resource> resourceListWithLatestRecord = resourceAllocList.stream().filter(resource -> resource.getBillingEndDate().compareTo(new Date()) >= 0).collect(Collectors.toList());

        if (resourceListWithLatestRecord != null && resourceListWithLatestRecord.size() > 0) {
            Resource resourcePrev = resourceListWithLatestRecord.get(0);//latest resource record.

            if (!resourcePrev.getProjectId().equalsIgnoreCase(MyTeamUtils.BENCH_PROJECT_ID) && !resourceReq.getProjectId().equalsIgnoreCase(resourcePrev.getProjectId())) {
                message = "Resource " + resourcePrev.getEmployeeId() + " already Assigned to the "
                        + projectService.getProjectByProjectId(resourcePrev.getProjectId()).getProjectName()
                        + " Project" + " from " + resourcePrev.getBillingStartDate() + "to " + resourcePrev.getBillingEndDate();
                isAssigned = true;
                respMap.put("statusCode", 815);
                respMap.put("message", message);
            }
        }


        return isAssigned;
    }




    public void deleteResource(Resource resourceReq, String loginEmpId) {
        List<Resource> resourcesList = resourceRepo.findByEmployeeIdAndProjectId(resourceReq.getEmployeeId(), resourceReq.getProjectId());
        resourcesList.forEach(resource -> resourceRepo.delete(resource));

    }

    @Override
    public List<Resource> getAllResources() {
        return resourceRepo.findAll();
    }


    public List<ResourceVO> getAllResourcesVO() {

        return getAllResources().stream().map(resource -> {

            ResourceVO resourceVO = new ResourceVO();
            resourceVO.setId(resource.getId());
            resourceVO.setProjectId(resource.getProjectId());
            resourceVO.setEmployeeId(resource.getEmployeeId());

            Employee employee = employeeService.getEmployeeById(resource.getEmployeeId());
            if (employee != null) {
                resourceVO.setEmployeeName(employee.getEmployeeName());
                resourceVO.setDesignation(employee.getDesignation());
                resourceVO.setEmailId(employee.getEmailId());
                resourceVO.setMobileNo(employee.getMobileNumber());
            }

            Project project = projectService.getProjectByProjectId(resource.getProjectId());
            if (project != null) {
                resourceVO.setProjectName(project.getProjectName());
                resourceVO.setProjectStartDate(project.getProjectStartDate());
                resourceVO.setProjectEndDate(project.getProjectEndDate());
                resourceVO.setProjectStatus(project.getStatus());
                if (project.getAccountId() != null) {
                    Account account = accountService.getAccountById(project.getAccountId());
                    if (account != null) {
                        resourceVO.setAccountName(account.getAccountName());
                    }
                }
            }

            //Account account=accountService.getAccountById(domainService.getDomainById(project.getProjectId()).getAccountId());

            resourceVO.setBillableStatus(resource.getBillableStatus());
            resourceVO.setBillingStartDate(resource.getBillingStartDate());
            resourceVO.setBillingEndDate(resource.getBillingEndDate());
            resourceVO.setResourceRole(resource.getResourceRole());
            if (resource.getBillingEndDate().compareTo(new Date()) > 0) {
                resourceVO.setResourceStatus(ResourceStatus.ACTIVE.getStatus());
            } else {
                resourceVO.setResourceStatus(ResourceStatus.IN_ACTIVE.getStatus());
            }
            return resourceVO;

        }).collect(Collectors.toList());

    }

    public List<Resource> getResourcesSortByBillingStartDate(String employeeId) {
        Query query = prepareQuery(employeeId, MyTeamUtils.BILLING_START_DATE);
        return mongoTemplate.find(query, Resource.class);
    }

    private Query prepareQuery(String employeeId, String dateColumn) {
        Query query = new Query();
        query.addCriteria(Criteria.where(MyTeamUtils.EMPLOYEE_ID).is(employeeId));
        query.limit(MyTeamUtils.ONE);
        query.with(new Sort(Sort.Direction.DESC, dateColumn));
        return query;
    }

    @Override
    public List<ResourceVO> getActiveResources(String empId) {
        List<ResourceVO> resourcesList = new ArrayList<>();
        for (Resource resource : resourceRepo.findByEmployeeId(empId)) {
            if (resource.getBillingEndDate().compareTo(new Date()) > 0) {
                resourcesList.addAll(prepareProjectTeamMembersList(resource.getProjectId()));
            }
        }
        return resourcesList;
    }


    public List<ResourceVO> prepareProjectTeamMembersList(String projectId) {
        List<ResourceVO> finalResourcesList = new ArrayList<>();
        Employee employee = null;

        for (Resource resource : getAllResourcesForProject(projectId)) {

                ResourceVO resourceVO = new ResourceVO();
                resourceVO.setId(resource.getId());
                resourceVO.setProjectId(resource.getProjectId());
                resourceVO.setEmployeeId(resource.getEmployeeId());

                employee = employeeService.getEmployeeById(resource.getEmployeeId());
                resourceVO.setEmployeeName(employee.getEmployeeName());
                resourceVO.setDesignation(employee.getDesignation());
                resourceVO.setEmailId(employee.getEmailId());
                resourceVO.setMobileNo(employee.getMobileNumber());

                resourceVO.setProjectName(projectService.getProjectByProjectId(resource.getProjectId()).getProjectName());
                resourceVO.setBillableStatus(resource.getBillableStatus());
                resourceVO.setBillingStartDate(resource.getBillingStartDate());
                resourceVO.setBillingEndDate(resource.getBillingEndDate());
                resourceVO.setResourceRole(resource.getResourceRole());
                if(resource.getBillingEndDate().compareTo(new Date())>0)
                {
                    resourceVO.setResourceStatus(ResourceStatus.ACTIVE.getStatus());
                }else
                {
                    resourceVO.setResourceStatus(ResourceStatus.IN_ACTIVE.getStatus());
                }

                finalResourcesList.add(resourceVO);

        }
        return finalResourcesList;
    }

    public List<Resource> getAllResourcesForProject(String projectId) {
        return resourceRepo.findByProjectId(projectId);
    }


    @Override
    public List<Resource> getAllResourcesForAllActiveProjects() {
        List<Resource> resourceList = new ArrayList<>();

        for (Project activeProject : projectService.getOnlyActiveProjects()) {
            resourceList.addAll(getAllResourcesForProject(activeProject.getProjectId()));
        }

        return resourceList;
    }


    @Override
    public List<ResourceVO> getResourcesForProject(String projectId, String statusFlag) {
        List<ResourceVO> resourcesList = new ArrayList<>();

        for (Resource resource : resourceRepo.findByProjectId(projectId)) {
            Date billingEndDate = resource.getBillingEndDate();
            if (billingEndDate != null) {

                ResourceVO resourceVO = new ResourceVO();
                resourceVO.setId(resource.getId());
                resourceVO.setProjectId(resource.getProjectId());
                resourceVO.setProjectName(projectService.getProjectByProjectId(resource.getProjectId()).getProjectName());
                resourceVO.setResourceRole(resource.getResourceRole());
                resourceVO.setBillingStartDate(resource.getBillingStartDate());
                resourceVO.setBillingEndDate(resource.getBillingEndDate());
                resourceVO.setBillableStatus(resource.getBillableStatus());
                resourceVO.setEmployeeId(resource.getEmployeeId());

                Employee employee = employeeService.getEmployeeById(resource.getEmployeeId());
                resourceVO.setEmailId(employee.getEmailId());
                resourceVO.setEmployeeName(employee.getEmployeeName());
                resourceVO.setDesignation(employee.getDesignation());


                // Active
                if (statusFlag.equals(ResourceStatus.ACTIVE.getStatus()) && billingEndDate.compareTo(new Date()) >= 0) {
                    resourceVO.setResourceStatus(ResourceStatus.ACTIVE.getStatus());
                    resourcesList.add(resourceVO);
                } else if (statusFlag.equals(ResourceStatus.IN_ACTIVE.getStatus()) && billingEndDate.compareTo(new Date()) < 0) {
                    resourceVO.setResourceStatus(ResourceStatus.IN_ACTIVE.getStatus());
                    resourcesList.add(resourceVO);
                } else if (statusFlag.equals(MyTeamUtils.BOTH))
                    resourcesList.add(resourceVO);

            }

        }
        return resourcesList;
    }


    @Override
    public List<MyProjectAllocationVO> getWorkedProjectsForResource(String empId) {

        Project project = null;
        Account account = null;
        Domain domain = null;
        Employee employee = null;
        List<MyProjectAllocationVO> myProjectList = new ArrayList<>();

        List<Resource> resourcesAllocatedList = resourceRepo.findByEmployeeId(empId);

        if (null != resourcesAllocatedList && !resourcesAllocatedList.isEmpty() && MyTeamUtils.INT_ZERO < resourcesAllocatedList.size()) {
            for (Resource resourceAlloc : resourcesAllocatedList) {

                project = projectService.getProjectByProjectId(resourceAlloc.getProjectId());
                account = accountService.getAccountById(project.getAccountId());
                domain = domainService.getDomainById(project.getDomainId());
                employee = employeeService.getEmployeeById(resourceAlloc.getEmployeeId());

                MyProjectAllocationVO myProject = new MyProjectAllocationVO();
                myProject.setProjectId(project.getProjectId());
                myProject.setProjectName(project.getProjectName());
                myProject.setProjectStartDate(project.getProjectStartDate());
                myProject.setProjectEndDate(project.getProjectEndDate());
                myProject.setProjectStatus(project.getStatus());
                myProject.setAccountName(account.getAccountName());

                myProject.setBillableStatus(resourceAlloc.getBillableStatus());
                myProject.setBillingStartDate(resourceAlloc.getBillingStartDate());
                myProject.setBillingEndDate(resourceAlloc.getBillingEndDate());
                myProject.setShift(employee.getShift());
                if (resourceAlloc.getBillingEndDate().compareTo(new Date()) > 0) {
                    myProject.setResourceStatus(ResourceStatus.ACTIVE.getStatus());
                } else {
                    myProject.setResourceStatus(ResourceStatus.IN_ACTIVE.getStatus());
                }

                if (project.getDeliveryLeadIds() != null) {
                    myProject.setDeliveryLeadIds(employeeService.getDeliveryManagerMap(project.getDeliveryLeadIds()));
                }
                myProjectList.add(myProject);
            }
        }
        return myProjectList;
    }


    @Override
    public List<Resource> getResourcesUnderDeliveryLead(String deliveryLeadId) {
        List<String> projectIdsList = new ArrayList<>();
        List<Resource> resourcesList = new ArrayList<>();

        for (Project project : projectService.getProjectsForDeliveryLead(deliveryLeadId))
            projectIdsList.add(project.getProjectId());

        Query query = new Query(Criteria.where("projectId").in(projectIdsList));
        List<Resource> resourcesListPersisted = mongoTemplate.find(query, Resource.class);

        for (Resource resourceAlloc : resourcesListPersisted) {
            if (!resourceAlloc.getEmployeeId().equals(deliveryLeadId))
                resourcesList.add(resourceAlloc);
        }
        return resourcesList;

    }


    @Override
    public List<ResourceVO> getBillingsForEmployee(String empId) {
        List<ResourceVO> finalList=new ArrayList<>();
        List<Resource> resourcesList = resourceRepo.findByEmployeeId(empId);
        if (resourcesList != null && resourcesList.size() > 0) {
            log.info("The resources billing list before sorting::" + resourcesList);
            //return billingsList.stream().sorted(Comparator.comparing(Billing::getCreatedOn).reversed()).collect(Collectors.toList());
            List<Resource> sortedList = resourcesList.stream().sorted(Comparator.comparing(Resource::getBillingStartDate).reversed()).collect(Collectors.toList());

            finalList= convertResourcesToResourcesVO(sortedList);
        }
        return  finalList;
    }

    @Override
    public List<Resource> getBillingsForProject(String empId, String projectId) {
        List<Resource> resourcesList = resourceRepo.findByEmployeeIdAndProjectId(empId, projectId);

        if (resourcesList == null || resourcesList.size() == 0) {
            return resourcesList;
        } else {
            //return billingsList.stream().sorted(Comparator.comparing(Billing::getCreatedOn).reversed()).collect(Collectors.toList());
            return resourcesList.stream().sorted(Comparator.comparing(Resource::getBillingStartDate).reversed()).collect(Collectors.toList());
        }

    }


    @Override
    public List<Employee> getUnAssignedEmployees() {

        List<Employee> notAssignedEmployees = new ArrayList<>();
        List<String> resourceIdsList = new ArrayList<>();

        for (Resource resource : this.getAllResources()) {
            Project project = projectService.getProjectByProjectId(resource.getProjectId());
            if (project != null && project.getStatus() != null && !"Completed".equalsIgnoreCase(project.getStatus())) {
                resourceIdsList.add(resource.getEmployeeId());
            }
        }
        for (Employee employee : employeeService.getAllEmployees()) {
            if (!resourceIdsList.contains(employee.getEmployeeId())) {
                notAssignedEmployees.add(employee);
            }
        }

        return notAssignedEmployees;
    }


    public void deleteResourcesUnderProject(String projectId) {
        Query query = new Query(Criteria.where("projectId").is(projectId));
        List<Resource> list = mongoTemplate.find(query, Resource.class);

        resourceRepo.delete(list);
    }


    private List<ResourceVO> convertResourcesToResourcesVO(List<Resource> resourcesList) {

        List<ResourceVO> finalList = new ArrayList<>();
        if (resourcesList != null && resourcesList.size() > 0) {
            finalList = resourcesList.stream().map(resource -> {

                ResourceVO resourceVO = new ResourceVO();
                resourceVO.setId(resource.getId());
                resourceVO.setProjectId(resource.getProjectId());
                resourceVO.setEmployeeId(resource.getEmployeeId());

                Employee employee = employeeService.getEmployeeById(resource.getEmployeeId());
                if (employee != null) {
                    resourceVO.setEmployeeName(employee.getEmployeeName());
                    resourceVO.setDesignation(employee.getDesignation());
                    resourceVO.setEmailId(employee.getEmailId());
                    resourceVO.setMobileNo(employee.getMobileNumber());
                }

                Project project = projectService.getProjectByProjectId(resource.getProjectId());
                if (project != null) {
                    resourceVO.setProjectName(project.getProjectName());
                    resourceVO.setProjectStartDate(project.getProjectStartDate());
                    resourceVO.setProjectEndDate(project.getProjectEndDate());
                    resourceVO.setProjectStatus(project.getStatus());
                    if (project.getAccountId() != null) {
                        Account account = accountService.getAccountById(project.getAccountId());
                        if (account != null) {
                            resourceVO.setAccountName(account.getAccountName());
                        }
                    }
                }

                //Account account=accountService.getAccountById(domainService.getDomainById(project.getProjectId()).getAccountId());

                resourceVO.setBillableStatus(resource.getBillableStatus());
                resourceVO.setBillingStartDate(resource.getBillingStartDate());
                resourceVO.setBillingEndDate(resource.getBillingEndDate());
                resourceVO.setResourceRole(resource.getResourceRole());
                if (resource.getBillingEndDate().compareTo(new Date()) > 0) {
                    resourceVO.setResourceStatus(ResourceStatus.ACTIVE.getStatus());
                } else {
                    resourceVO.setResourceStatus(ResourceStatus.IN_ACTIVE.getStatus());
                }
                return resourceVO;

            }).collect(Collectors.toList());
        }

        return finalList;
    }


    @Override
    public Resource addResourceToBenchProject(Employee employee, String loginEmpId) throws MyTeamException {
        Resource resourcePersisted = null;

        Resource resourceBench = new Resource();
        resourceBench.setProjectId(MyTeamUtils.BENCH_PROJECT_ID);
        resourceBench.setEmployeeId(employee.getEmployeeId());
        resourceBench.setResourceRole(employee.getRole());
        resourceBench.setBillingStartDate(employee.getDateOfJoining() != null ? employee.getDateOfJoining() : new Date());
        resourceBench.setBillableStatus(MyTeamUtils.BENCH_BILLABILITY_STATUS);
        resourceBench.setEmployeeId(employee.getEmployeeId());
        resourceBench.setBillingEndDate(projectService.getProjectByProjectId(MyTeamUtils.BENCH_PROJECT_ID).getProjectEndDate());
        resourcePersisted = addResource(resourceBench, loginEmpId);

        return resourcePersisted;

    }


    @Override
    public List<EmployeeShiftsVO> getResourcesForShift(String shift) {
        List<Resource> resourcesListPers = null;
        List<EmployeeShiftsVO> resourcesList = new ArrayList<>();

        List<Project> projects = projectService.getAllProjects();
        for (Project project : projects) {
            if ("Active".equalsIgnoreCase(project.getStatus())) {
                resourcesListPers = getAllResourcesForProject(project.getProjectId());

                for (Resource resource : resourcesListPers) {

//                    EmployeeShift empShift = empShiftService.getEmployeeShift(resource.getEmployeeId());
//                    if (empShift != null) {
//                        if (empShift.getShift() != null && empShift.getShift().equalsIgnoreCase(shift))
//                            resourcesList.add(resource);
//                        else if (empShift.getShift() == null && Shifts.SHIFT1.getShiftType().equalsIgnoreCase(shift))
//                            resourcesList.add(resource);
//
//                    }

                    if (resource.getBillingEndDate().compareTo(new Date()) >= 0) {
                        Employee employee = employeeService.getEmployeeById(resource.getEmployeeId());

                        EmployeeShiftsVO shiftsVO = new EmployeeShiftsVO();
                        shiftsVO.setEmployeeId(employee.getEmployeeId());
                        shiftsVO.setEmployeeName(employee.getEmployeeName());
                        shiftsVO.setEmailId(employee.getEmailId());
                        shiftsVO.setMobileNo(employee.getMobileNumber());
                        shiftsVO.setProjectName(project.getProjectName());

                        if (employee != null) {
                            if (shift.equalsIgnoreCase(employee.getShift()))
                                resourcesList.add(shiftsVO);
                            else if (employee.getShift() == null && Shifts.SHIFT1.getShiftType().equalsIgnoreCase(shift))
                                resourcesList.add(shiftsVO);

                        }
                    }

                }//for
            }

        }
        return resourcesList;

    }




}//class




//    @Override
//    public List<ResourceVO> getActiveResources(String empId) {
//        List<ResourceVO> finalResourcesList = new ArrayList<>();
//
//        List<Resource> resourceList = resourceRepo.findByEmployeeId(empId);
//        if (resourceList != null && resourceList.size() > 0) {
//
//            Resource resourceAlloc=resourceList.get(0);
//
//
//
//
//        }
//
//        for (Resource resource : resourceRepo.findByEmployeeId(empId)) {
//
//            ResourceVO resourceVO=new ResourceVO();
//            resourceVO.setEmployeeId(resource.getEmployeeId());
//
//            Employee employee=employeeService.getEmployeeById(resource.getEmployeeId());
//            resourceVO.setEmployeeName(employee.getEmployeeName());
//            resourceVO.setDesignation(employee.getDesignation());
//            resourceVO.setEmailId(employee.getEmailId());
//            resourceVO.setMobileNo(employee.getMobileNumber());
//
//            resourceVO.setProjectName(projectService.getProjectByProjectId(resource.getProjectId()).getProjectName());
//
//
//            if (resource.getBillingEndDate().compareTo(new Date()) > 0) {
//                finalResourcesList.addAll(getAllResourcesForProject(resource.getProjectId()));
//            }
//
//
//        }
//        return finalResourcesList;
//    }


//    @Override
//    public List<ResourceVO> getActiveResources(String empId) {
//        List<ResourceVO> finalResourcesList = new ArrayList<>();
//        Employee employee = null;
//
//        List<Resource> resourceList = resourceRepo.findByEmployeeId(empId);
//        Optional<Resource> optionalResource = resourceList.stream().filter(resource -> resource.getBillingEndDate().compareTo(new Date()) > 0).findAny();
//        if (optionalResource.isPresent()) {
//            finalResourcesList = prepareProjectTeamMembersList(optionalResource.get().getProjectId());
//        }
//        return finalResourcesList;
//    }