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.EmployeeRepositoryImpl;
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.Eproject;
import com.itsol.quantrivanphong.model.Project;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

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

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private EmployeeRepositoryImpl employeeRepository;

    @Autowired
    private ProjectGroupUtils utils;

    @Override
    public List<ProjectGroupDTO> getGroupByProjectId(Integer projectId) {
        logger.info("getGroupByProjectId");
        Project project = projectRepository.findByEmtityId(projectId);
        if (project != null) {
            List<Eproject> lstModels = projectGroupRepository.finByProperty("project", project, "joinDate", "DESC");
            List<ProjectGroupDTO> lstDTO = utils.getListDTO(lstModels);
            return lstDTO;
        }
        return null;
    }
    public boolean checkEmployeeInGroupProject(ProjectGroupDTO dto){
        List<Eproject> lst = projectGroupRepository.finByProperty("project",projectRepository.findByEmtityId(dto.getProjectId()),null,null);
        for (Eproject eproject:lst) {
            if(eproject.getEmployee().getId()==dto.getUserId()){
                return true;
            }
        }
        return false;
    }
    @Override
    public String saveEmployeeProject(ProjectGroupDTO dto) {
        logger.info("saveEmployeeProject");
        String views = "";
        if(!checkEmployeeInGroupProject(dto)) {
            try {
                projectGroupRepository.saveEntity(utils.Model(dto));
                views = "thêm thành công nhân viên vào nhóm dự án";
            } catch (Exception e) {
                views = "thêm không thành công!";
            }
        }else {
            views = "thành viên đã tồn tại";
        }
        return views;
    }

    @Override
    public String updateEmployeeProject(ProjectGroupDTO dto) {
        logger.info("updateEmployeeProject");
        String views="";
        Eproject eproject = projectGroupRepository.findByEmtityId(dto.getId());
        if(eproject!=null){
            Eproject updateProjectGroup = projectGroupRepository.updateEntity(utils.Model(dto));
            if(updateProjectGroup!=null){
                views = "Sửa thành công";
            }
        }else{
            views = "đối tượng không tồn tại";
        }
        return null;
    }

    @Override
    public ProjectGroupDTO findEmployeeProject(String username) {
        return null;
    }

    @Override
    public String outProjectGroup(Integer[] ids) {
        logger.info("outProjectGroup");
        String views = "";
        int count=0;
        count = projectGroupRepository.deleteEntity(ids);
        if(count!=0){
            views="Bạn đã cho "+count+" thoát ra khỏi nhóm";
        }else{
            views=" thành viên không tồn tại";
        }
        return views;
    }

    @Override
    public String deleteEmployeeProject(Integer id) {
        logger.info("deleteEmployeeProject");
        String views = "";
        int count=0;
        count = projectGroupRepository.deleteEntityByID(id);
        if(count!=0){
            views="Bạn đã cho "+count+" thoát ra khỏi nhóm";
        }else{
            views=" thành viên không tồn tại";
        }
        return views;
    }
}
