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;
<<<<<<< HEAD
    public String insertTimeSheet(TimeSheetDTO timeSheetDTO) {
=======
    public String addTimeSheet(TimeSheetDTO timeSheetDTO) {
>>>>>>> master
        String message;
        Employee employee = employeeRepository.findEmployeeById(timeSheetDTO.getEmployeeId());
        Eproject eproject = eProjectRepository.findEprojectByIdAndEmployee(timeSheetDTO.getEprojectId(), 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;
    }

<<<<<<< HEAD
    public String deleteTimeSheet(int timesheetId) {
        String message;
//        Employee employee = employeeRepository.findEmployeeById(timeSheetDTO.getEmployeeId());
//        Eproject eproject = eProjectRepository.findEprojectByEmployee(employee);
//        TimeSheet timeSheet = timeSheetRepository.findTimeSheetByEprojectAndId(eproject, timeSheetDTO.getId());
        TimeSheet timeSheet = timeSheetRepository.findTimeSheetById(timesheetId);
        if (timeSheet != null) {
            timeSheetRepository.delete(timeSheet);
            if (timeSheetRepository.findTimeSheetById(timesheetId) == null) {
=======
    public String deleteTimeSheet(int timeSheetId) {
        String message;
//        Employee employee = employeeRepository.findEmployeeById(employee_Id);
//        Eproject eproject = eProjectRepository.findEprojectByEmployee(employee);
        TimeSheet timeSheet = timeSheetRepository.findTimeSheetById(timeSheetId);
        if (timeSheet != null) {
            timeSheetRepository.delete(timeSheet);
            if (timeSheetRepository.findTimeSheetById(timeSheetId) == null) {
>>>>>>> master
                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(TimeSheetDTO timeSheetDTO) {
        String message;
        Employee employee = employeeRepository.findEmployeeById(timeSheetDTO.getEmployeeId());
        Eproject eproject = eProjectRepository.findEprojectByEmployee(employee);
        TimeSheet timeSheet = timeSheetRepository.findTimeSheetByEprojectAndId(eproject, timeSheetDTO.getId());
        if (eproject != null) {
            if (timeSheet != null) {
                int i = timeSheetRepository.updateTimeSheet(timeSheetDTO.getTitle(), timeSheetDTO.getContent()
                        , timeSheetDTO.getNote(), timeSheetDTO.getId());
                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 updateCheck(TimeSheetDTO timeSheetDTO) {
        String message;
        int i = timeSheetRepository.updateCheck(timeSheetDTO.getChecked(), timeSheetDTO.getId());
        if (i == 1) {
            message = "Success";
        } else {
            message = "Failed";
        }
        return message;
    }

//    @Transactional
//    public String updateLeaveFormStatus(TimeSheetDTO timeSheetDTO) {
//        String message;
//        TimeSheet timeSheet = timeSheetRepository.findTimeSheetByEmployeeId(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> findTimeSheetByEmployeeId(int employeeId) {
        Employee employee = employeeRepository.findEmployeeById(employeeId);
        Eproject eproject = eProjectRepository.findEprojectByEmployee(employee);
        if (employee != null && eproject != null && timeSheetRepository.showTimeSheetByEproject(eproject) != null) {
            return timeSheetRepository.showTimeSheetByEproject(eproject);
        } else {
            return null;
        }
    }
    public TimeSheet findTimeSheetById(int id) {
        return timeSheetRepository.findTimeSheetById(id);
    }
}
