package com.nisum.myteam.service.impl;

import com.nisum.myteam.model.dao.Employee;
import com.nisum.myteam.model.dao.Project;
import com.nisum.myteam.model.vo.EmployeeDashboardVO;
import com.nisum.myteam.model.vo.ResourceVO;
import com.nisum.myteam.service.IDashboardService;
import com.nisum.myteam.service.IEmployeeService;
import com.nisum.myteam.service.IProjectService;
import com.nisum.myteam.service.IResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class DashboardService implements IDashboardService {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IResourceService resourceService;

    @Autowired
    private IProjectService projectService;

    @Override
    public List<EmployeeDashboardVO> getEmployeesDashBoard() {
        //List<Employee> allEmployees = employeeRepo.findAll();
        List<Employee> allEmployees = employeeService.getAllEmployees();

        List<EmployeeDashboardVO> employeeDashboard = new ArrayList<>();
        Map<String, Object> resourcesMap = new HashMap();
        Map<String, Object> teamMatesStatusMap = new HashMap();

        // Find all active employees
        //List<Resource> resources = resourceService.getAllResources();

        List<ResourceVO> resources = resourceService.getAllResourcesVO();


        for (ResourceVO resource : resources) {
            //if (resource.isActive()) {
            if (resource.getBillingEndDate().compareTo(new Date()) > 0) {


                Project project = projectService.getProjectByProjectId(resource.getProjectId());
                if (project != null && project.getStatus() != null
                        && !"Completed".equalsIgnoreCase(project.getStatus())) {
                    Object projectResource = resourcesMap.get(resource.getEmployeeId());

                    if (projectResource == null) {
                        List listOfObjects = new ArrayList<>();
                        listOfObjects.add(resource);
                        // A person can have multiple active projects with billability
                        resourcesMap.put(resource.getEmployeeId(), listOfObjects);
                    } else {
                        List existingRecordsInMap = (List) resourcesMap
                                .get(resource.getEmployeeId());
                        existingRecordsInMap.add(resource);
                        resourcesMap.put(resource.getEmployeeId(),
                                existingRecordsInMap);

                    }
                }
            }
        }
        for (Employee emp : allEmployees) {
            if (resourcesMap.containsKey(emp.getEmployeeId())) {
                Object value = resourcesMap.get(emp.getEmployeeId());
                if (value instanceof List) {
                    List listOfTeamMates = (List) value;
                    String billableStatus = "NA";
                    for (Object obj : listOfTeamMates) {
                        ResourceVO projectTeamMate = (ResourceVO) obj;
                        String status = projectTeamMate.getBillableStatus();
                        if (status == null) {
                            status = "NA";
                        }
                        EmployeeDashboardVO empVo = new EmployeeDashboardVO();
                        BeanUtils.copyProperties(emp, empVo);
                        BeanUtils.copyProperties(projectTeamMate, empVo,
                                "employeeId", "employeeName", "emailId", "role",
                                "designation", "mobileNumber");
                        employeeDashboard.add(empVo);
                    }
                }
            } else {
                EmployeeDashboardVO empVo = new EmployeeDashboardVO();
                BeanUtils.copyProperties(emp, empVo);
                empVo.setBillableStatus("UA");
                empVo.setProjectAssigned(false);
                employeeDashboard.add(empVo);
            }
        }
        return employeeDashboard;
    }
}
