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

import com.itsol.quantrivanphong.access.login.dto.EmployeeDTO;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.manager.project.project.dto.ProjectDTO;
import com.itsol.quantrivanphong.model.Project;
import com.itsol.quantrivanphong.manager.project.project.repository.ProjectRepository;
import com.itsol.quantrivanphong.manager.project.projectgroup.dto.ProjectGroupDTO;
import com.itsol.quantrivanphong.model.Eproject;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component
public class ProjectGroupUtils {
    private static Logger logger = Logger.getLogger(ProjectGroupUtils.class);
    @Autowired
    private  EmployeeRepositoryImpl employeeRepository;
    @Autowired
    private  ProjectRepository projectRepository;


    public  Eproject Model(ProjectGroupDTO dto){
        Eproject model = new Eproject();
        model.setId(dto.getId());
        model.setJoinDate(dto.getJoinDate());
        model.setOutDate(dto.getOutDate());
        model.setPosition(dto.getPosition());
        try{
            Employee findEmployee = employeeRepository.getEmployeeById(dto.getUserId());
            if(findEmployee!=null){
               model.setEmployee(findEmployee);
            }
        }catch (Exception e){
            logger.info("(Lỗi không tìm thấy Employee) "+e.getMessage());
        }
        try{
            Project project = projectRepository.findByEmtityId(dto.getProjectId());
            if(project!=null){
                model.setProject(project);
            }
        }catch (Exception e){
            logger.info("(Lỗi không tìm thấy Project) "+e.getMessage());
        }
        return model;
    }

    public  ProjectGroupDTO DTO(Eproject models) {
        ProjectGroupDTO dto = new ProjectGroupDTO();
        dto.setId(models.getId());
//        set project
        ProjectDTO projectDTO = new ProjectDTO();
        BeanUtils.copyProperties(models.getProject(),projectDTO);
        dto.setProjectDTO(projectDTO);
//        set employee
        EmployeeDTO employeeDTO = new EmployeeDTO();
        employeeDTO.setId(models.getEmployee().getId());
        employeeDTO.setUsername(models.getEmployee().getUsername());
        employeeDTO.setLastName(models.getEmployee().getLastName());
        employeeDTO.setEmail(models.getEmployee().getEmailAddress());
        employeeDTO.setFacebookProfile(models.getEmployee().getFacebookProfile());
        employeeDTO.setSkype(models.getEmployee().getSkype());
        dto.setEmployeeDTO(employeeDTO);

        dto.setJoinDate(models.getJoinDate());
        dto.setOutDate(models.getOutDate());
        dto.setPosition(models.getPosition());
        return dto;
    }
    public  List<ProjectGroupDTO> getListDTO(List<Eproject> lstModel){
        if(!lstModel.isEmpty()||lstModel.size()!=0){
            List<ProjectGroupDTO> lstDTO = new ArrayList<ProjectGroupDTO>();
            for (Eproject eproject:lstModel) {
                lstDTO.add(DTO(eproject));
            }
            return lstDTO;
        }
        return null;
    }
}
