package com.itsol.quantrivanphong.report.timesheet.business;

import com.itsol.quantrivanphong.manager.employee.repository.EmployeeRepository;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.Eproject;
import com.itsol.quantrivanphong.model.TimeSheet;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TimeSheetBusiness {
    @Autowired
    TimeSheetRepository timeSheetRepository;
    @Autowired
    EProjectRepository eProjectRepository;
    @Autowired
    EmployeeRepository employeeRepository;
    public String insertTimeSheet(int employee_Id, TimeSheetDTO timeSheetDTO) {
        String message;
        Employee employee = employeeRepository.findEmployeeById(employee_Id);
        Eproject eproject = eProjectRepository.findEprojectByIdAndEmployee(timeSheetDTO.getEproject_id(), employee);
        if (eproject != null) {
//            SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-yyyy HH:mm");
//            Date date = new Date(System.currentTimeMillis());

            TimeSheet timeSheet1 = new TimeSheet();
            timeSheet1.setTitle(timeSheetDTO.getTitle());
            timeSheet1.setContent(timeSheetDTO.getContent());
            timeSheet1.setNote(timeSheetDTO.getNote());
            timeSheet1.setEproject(eproject);
            timeSheet1.setChecked("");
            timeSheet1.setStatus(true);

            TimeSheet timeSheet = timeSheetRepository.save(timeSheet1);
            if (timeSheet != null) {
                message = "Insert Time Sheet success";
            } else {
                message = "Insert Time Sheet failed";
            }
        } else {
            message = "Eproject does not exist";
        }
        return message;
    }

    public String deleteTimeSheet(int employee_Id, int timeSheet_Id) {
        String message;
        Employee employee = employeeRepository.findEmployeeById(employee_Id);
        Eproject eproject = eProjectRepository.findEprojectByEmployee(employee);
        TimeSheet timeSheet = timeSheetRepository.findTimeSheetByEprojectAndId(eproject, timeSheet_Id);
        if (timeSheet != null) {
            timeSheetRepository.delete(timeSheet);
            if (timeSheetRepository.findTimeSheetByEprojectAndId(eproject, timeSheet_Id) == null) {
                message = "Delete success";
            } else {
                message = "Delete failed";
            }
        } else {
            message = "Time Sheet does not exist";
        }
        return message;
    }

    public List<TimeSheet> findAll(){
        return timeSheetRepository.findAll();
    }

    @Transactional
    public String updateTimeSheet(int employee_Id, TimeSheetDTO timeSheetDTO, int timesheet_Id) {
        String message;
        Employee employee = employeeRepository.findEmployeeById(employee_Id);
        Eproject eproject = eProjectRepository.findEprojectByEmployee(employee);
        TimeSheet timeSheet = timeSheetRepository.findTimeSheetByEprojectAndId(eproject, timesheet_Id);
        if (eproject != null) {
            if (timeSheet != null) {
                int i = timeSheetRepository.updateTimeSheet(timeSheetDTO.getTitle(), timeSheetDTO.getContent()
                        , timeSheetDTO.getNote(), timesheet_Id);
                if (i == 1) {
                    message = "Update success";
                } else {
                    message = "Update failed";
                }
            } else {
                message = "TimeSheet does not exist";
            }
        } else {
            message = "Eproject does not exist";
        }
        return message;
    }

//    @Transactional
//    public String updateLeaveFormStatus(TimeSheetDTO timeSheetDTO) {
//        String message;
//        TimeSheet timeSheet = timeSheetRepository.findTimeSheetById(timeSheetDTO.getId());
//        if (timeSheet != null) {
//            int i = timeSheetRepository.updateTimeSheetStatus(timeSheetDTO.isStatus(), timeSheetDTO.getId());
//            if (i == 1) {
//                message = "Update status success";
//            } else {
//                message = "Update status failed";
//            }
//        } else {
//            message = "TimeSheet does not exist";
//        }
//
//        return message;
//    }

    public List<TimeSheet> findTimeSheetById(int employee_Id) {
        Employee employee = employeeRepository.findEmployeeById(employee_Id);
        Eproject eproject = eProjectRepository.findEprojectByEmployee(employee);
        if (employee != null && eproject != null && timeSheetRepository.showTimeSheetByEproject(eproject) != null) {
            return timeSheetRepository.showTimeSheetByEproject(eproject);
        } else {
            return null;
        }
    }
}
