package com.itsol.quantrivanphong.manager.project.projectgroup.bussiness;

import com.itsol.quantrivanphong.manager.project.project.repository.ProjectRepository;
import com.itsol.quantrivanphong.manager.project.projectgroup.common.EmployeeProject;
import com.itsol.quantrivanphong.manager.project.projectgroup.common.ProjectGroupUtils;
import com.itsol.quantrivanphong.manager.project.projectgroup.dto.ProjectGroupDTO;
import com.itsol.quantrivanphong.manager.project.projectgroup.repository.ProjectGroupRepository;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.Eproject;
import com.itsol.quantrivanphong.report.issue.common.WrapperResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class ProjectGroupBussinessImpl implements ProjectGroupBussiness {
    private Logger logger = Logger.getLogger(ProjectGroupBussinessImpl.class);

    @Autowired
    private ProjectGroupRepository projectGroupRepository;

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private ProjectGroupUtils utils;

    @Autowired
    private EmployeeProject employeeProject;

    @Override
    public List<ProjectGroupDTO> getGroupByProjectId(Integer projectId) {
        logger.info("getGroupByProjectId");
        List<Eproject> lstModels = projectGroupRepository.findByProjectId(projectId);
        List<ProjectGroupDTO> lstDTO = utils.getListDTO(lstModels);
        return lstDTO;
    }

    public boolean checkEmployeeInGroupProject(ProjectGroupDTO dto) {
            if (dto.getProjectId()!=null) {
                List<Eproject> lst = projectGroupRepository.findByProjectId(dto.getProjectId());
                for (Eproject eproject : lst) {
                    if (eproject.getEmployee().getUsername().equals(dto.getUserName())) {
                        return true;
                    }
                }
            }
        return false;
    }

    @Override
    public WrapperResult saveEmployeeProject(ProjectGroupDTO dto) {
        String views = "";
        int status = 113;
        logger.info("saveEmployeeProject");
        Employee employee = employeeProject.findByUsername(dto.getUserName());
        if (employee == null) {
            views = "Nhân viên không tồn tại !";
        } else if (!checkEmployeeInGroupProject(dto)) {
            try {
                dto.setStatus(1);
                dto.setUserId(employee.getId());
                dto.setJoinDate(new Timestamp(System.currentTimeMillis()));
                Eproject eproject = projectGroupRepository.save(utils.Model(dto));
                if (eproject != null) {
                    views = "thêm thành công nhân viên vào nhóm dự án";
                    status = 200;
                } else {
                    views = "thêm không thành công!";
                }
            } catch (Exception e) {
                logger.info("Lỗi " + e.getMessage());
            }
        } else {
            status = 113;
            views = "thành viên đã tồn tại";
        }
        return new WrapperResult(status, views);
    }

    @Override
    public WrapperResult updateEmployeeProject(ProjectGroupDTO dto) {
        String views = "";
        int status = 113;
        logger.info("updateEmployeeProject");

        Eproject eproject = projectGroupRepository.findEprojectById(dto.getId());
        if (eproject != null) {
            eproject.setPosition(dto.getPosition());
            Eproject updateProjectGroup = projectGroupRepository.save(eproject);
            if (updateProjectGroup != null) {
                views = "cập nhật chức vụ thành viên thành công";
                status = 200;
            }
        } else {
            views = "đối tượng không tồn tại";
        }

        return new WrapperResult(status, views);
    }

    @Override
    public ProjectGroupDTO findEmployeeProjectById(Integer id) {
        try {
            ProjectGroupDTO projectGroupDTO = utils.DTO(projectGroupRepository.findEprojectById(id));
            if (projectGroupDTO != null) {
                return projectGroupDTO;
            }
        } catch (Exception e) {
            logger.info("Lỗi update EProject " + e.getMessage());
        }
        return null;
    }

    @Override
    public WrapperResult deleteEmployeeProject(Integer id) {
        String views = "";
        int status = 113;
        logger.info("deleteEmployeeProject");
        if (projectGroupRepository.findEprojectById(id) != null) {
            projectGroupRepository.deleteById(id);
            views = "Bạn đã cho thoát ra khỏi nhóm";
            status = 200;
        } else {
            views = "Thành viên không tồn tại trong nhóm";
        }
        return new WrapperResult(status, views);
    }

    @Override
    public WrapperResult updateEmployeeOutGroup(ProjectGroupDTO dto) {
        String views = "";
        int status = 113;
        logger.info("updateEmployeeOutGroup");
        Eproject eproject = projectGroupRepository.findEprojectById(dto.getId());
        if (eproject != null) {
            eproject.setStatus(2);
            eproject.setOutDate(new Timestamp(System.currentTimeMillis()));
            Eproject updateProjectGroup = projectGroupRepository.save(eproject);
            if (updateProjectGroup != null) {
                views = "thành viên đã out nhóm";
                status = 200;
            }
        } else {
            views = "đối tượng không tồn tại";
        }
        return new WrapperResult(status, views);
    }
}
