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


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.manager.project.project.repository.ProjectRRepository;
import com.itsol.quantrivanphong.model.*;
import com.itsol.quantrivanphong.report.timesheet.business.TimeSheetBusiness;
import com.itsol.quantrivanphong.report.timesheet.dto.TimeSheetDTO;
import com.itsol.quantrivanphong.report.timesheet.repository.EProjectRepository;
import com.itsol.quantrivanphong.report.timesheet.repository.TimeSheetRepository;
import com.itsol.quantrivanphong.repository.EmployeeRepository;
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(ReportDTO reportDTO){

     List<TimeSheet> timeSheetList = timeSheetRepository.findAllTimesheetStatusTrue(reportDTO.getProjectId(), reportDTO.getCurrentDate());
     return timeSheetList;
    }

    public String updateTimesheetStatus(ReportDTO reportDTO){
        String mess;
        try {
            List<Eproject> eprojectList = eProjectRepository.listEprojectLack(reportDTO.getProjectId(), reportDTO.getCurrentDate());
            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 String updateTimesheetChecked(TimesheetRequestDTO timesheetRequestDTO){
        String mess;
        try {
            timeSheetRepository.updateTimesheetChecked(timesheetRequestDTO.getChecked(),timesheetRequestDTO.getId());
            mess = "OK !";
        }catch (Exception e){
            mess = e.getMessage();
        }
        return mess;
    }
    //======================================================================================================================




    public List<ProjectReport> allProjectReport(ReportDTO reportDTO){

        List<ProjectReport> projectReportList = projectReportRepository.allProjectReport(reportDTO.getProjectId());
        if (projectReportList.isEmpty()){
            return  null;
        }else
        return projectReportList;
    }

    public ProjectReport latestReport(ReportDTO reportDTO){

        return projectReportRepository.latestReport(reportDTO.getProjectId());
    }

    public List<Employee> listLackOfReport(ReportDTO reportDTO){
        List<Employee> employeeList = employeeRepository.listLackOfReport(reportDTO.getProjectId(), reportDTO.getCurrentDate());
        return employeeList;
    }



}
