package com.itsol.quantrivanphong.manager.reportdetail.bussiness;


import com.itsol.quantrivanphong.manager.employee.repository.EmployeeRepository;
import com.itsol.quantrivanphong.manager.project.project.repository.ProjectRRepository;
import com.itsol.quantrivanphong.manager.reportdetail.dto.request.ReportDTO;
import com.itsol.quantrivanphong.manager.reportdetail.dto.request.TimesheetRequestDTO;
import com.itsol.quantrivanphong.manager.reportdetail.repository.ProjectReportRepository;
import com.itsol.quantrivanphong.model.*;
import com.itsol.quantrivanphong.report.timesheet.repository.EProjectRepository;
import com.itsol.quantrivanphong.report.timesheet.repository.TimeSheetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ReportProjectBussiness {
    @Autowired
    private ProjectRRepository projectRepository;

    @Autowired
    private EmployeeRepository employeeRepository;

    @Autowired
    private ProjectReportRepository projectReportRepository;

    @Autowired
    private EProjectRepository eProjectRepository;

    @Autowired
    private TimeSheetRepository timeSheetRepository;


    //======================================================================================================================

    public List<Project> listProjectWorking(){

        List<Project> projectList = projectRepository.listProjectWorking();
        if(projectList.isEmpty()){
            return null;
        }else
            return projectList;
    }
    //======================================================================================================================

    public List<TimeSheet> findAllTimeSheetStatusTrue(int projectId, String currentDate){

     List<TimeSheet> timeSheetList = timeSheetRepository.findAllTimeSheetStatusTrue(projectId, currentDate);
        return timeSheetList;
    }

    public String updateTimeSheetStatus(int projectId, String currentDate){
        String mess;
        try {
            List<Eproject> eprojectList = eProjectRepository.listEprojectLack(projectId, currentDate);
            List<Integer> list = new ArrayList<>();
            for (Eproject eproject: eprojectList) {
                list.add(eproject.getId());
            }
            for (Integer i: list){
                timeSheetRepository.updateLack(i,false,"checked");
            }
            mess = "OK !";
        }catch (Exception e){
            mess = e.getMessage();
        }

        return mess;
    }
    public List<Employee> listLackOfReport(int projectId, String currentDate){
        List<Employee> employeeList = employeeRepository.listLackOfReport(projectId, currentDate);
        return employeeList;
    }

    public String updateTimeSheetChecked(int timeSheetId, String check){
        String mess;
        try {
            timeSheetRepository.updateTimeSheetChecked(check, timeSheetId);
            mess = "OK !";
        }catch (Exception e){
            mess = e.getMessage();
        }
        return mess;
    }
    //======================================================================================================================




    public List<ProjectReport> allProjectReport(int projectId){

        List<ProjectReport> projectReportList = projectReportRepository.allProjectReport(projectId);
        return projectReportList;
    }

    public ProjectReport latestReport(int projectId){

        return projectReportRepository.latestReport(projectId);
    }



    public String insertProjectReport(ReportDTO reportDTO){
        String mess;
        try {

            ProjectReport projectReport = new ProjectReport();
            projectReport.setProjectName(projectRepository.ProjectName(reportDTO.getProjectId()));
            projectReport.setTeamLeader(employeeRepository.findTeamLeader(reportDTO.getProjectId()));
            projectReport.setNumberOfMember(employeeRepository.numberOfMember(reportDTO.getProjectId()));
            projectReport.setCalendarEffort(10);
            projectReport.setLackOfReport(timeSheetRepository.numberEmployeeLack(reportDTO.getProjectId(), reportDTO.getFirstPoint(), reportDTO.getFinalPoint()));
            projectReport.setFirstDate(reportDTO.getFirstPoint());
            projectReport.setFinalDate(reportDTO.getFinalPoint());
            projectReport.setStatus(reportDTO.isStatus());

            projectReportRepository.insertProjectReport(projectReport.getProjectName(),projectReport.getTeamLeader(), projectReport.getNumberOfMember(), projectReport.getCalendarEffort(), projectReport.getLackOfReport(), projectReport.isStatus(),reportDTO.getProjectId(), projectReport.getFirstDate(), projectReport.getFinalDate());

            mess = "Ok !";
        }catch (Exception e){
            mess = e.getMessage();
        }

        return  mess;

    }

    //======================================================================================================================

    public List<Employee> listEmployeeLack(String firstDate, String finalDate, int projectId) {
        List<Employee> employeeList = employeeRepository.listEmployeeLackOfReport(firstDate, finalDate, projectId);

        return employeeList;
    }

    public int numberLack(String firstDate, String finalDate, int projectId, int employeeId){
        return employeeRepository.numberLack(firstDate, finalDate, projectId, employeeId);
    }

    public List<TimeSheet> listDateOfLack(String firstDate, String finalDate, int projectId, int employeeId) {
        List<TimeSheet> timeSheetList = timeSheetRepository.listDateOfLack(firstDate,finalDate,projectId, employeeId);

        return timeSheetList;
    }

    //======================================================================================================================

}
