package com.dbnt.faisp.kwms.service;

import com.dbnt.faisp.kwms.model.VEmployee;
import com.dbnt.faisp.kwms.model.VHordYundongStat;
import com.dbnt.faisp.kwms.repository.VEmployeeRepository;
import com.dbnt.faisp.kwms.repository.VHordYundongStatRepository;
import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;
import com.dbnt.faisp.main.userInfo.model.UserCareer;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import com.dbnt.faisp.main.userInfo.repository.UserCareerRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


@Service
@RequiredArgsConstructor
public class KwmsService {
  private final VEmployeeRepository vEmployeeRepository;
  private final VHordYundongStatRepository vHordRepository;
  private final CodeMgtService codeMgtService;
  private final UserCareerRepository careerRepository;

  public UserInfo selectEmpInfo(String dicCode){
    VEmployee empInfo = vEmployeeRepository.findByDicCode(dicCode).orElse(null);
    if(empInfo==null){
      return null;
    }else{
      return convertVEmployeeToUserInfo(empInfo);
    }
  }

  private UserInfo convertVEmployeeToUserInfo(VEmployee empInfo){
    UserInfo userInfo = new UserInfo();
    userInfo.setDicCode(empInfo.getDicCode());
    userInfo.setUserNm(empInfo.getEmpNm());
    userInfo.setSex(empInfo.getSex().strip().equals("F")?"SEX001":"SEX002");
    String[] positionAry = empInfo.getCallBuseoNm().split(" ");
    userInfo.setOgCd(codeMgtService.searchCodeToCategoryAndValue("OG", positionAry[0]));
    if (positionAry.length>1){
      userInfo.setOfcCd(codeMgtService.searchCodeToCategoryAndValue("OFC", positionAry[1]));
    }
    userInfo.setTitleCd(codeMgtService.searchCodeToCategoryAndValue("JT", empInfo.getJikgeup()));
    userInfo.setOutturnCd(codeMgtService.searchCodeToCategoryAndValue("OTC", empInfo.getGyunggwa()));
    userInfo.setSeriesCd(codeMgtService.searchCodeToCategoryAndValue("SRC", empInfo.getJikbyul()));

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    empInfo.setBirthDate(
        (Integer.parseInt(empInfo.getBirthDate().substring(0,2))>60?"19":"20")+empInfo.getBirthDate());
    userInfo.setBirthDate(LocalDate.parse(empInfo.getBirthDate(), formatter));
    userInfo.setPoliceInDate(LocalDate.parse(empInfo.getPoliceBmngIl(), formatter));
    userInfo.setTitleInDate(LocalDate.parse(empInfo.getHnJikgeupImyngil(), formatter));
    return userInfo;
  }

  public List<UserCareer> selectUserCareerList(Integer userSeq, String dicCode) {
    List<VHordYundongStat> statList = vHordRepository.findByDicCodeOrderByStartDateDesc(dicCode);
    List<UserCareer> savedList = careerRepository.findByUserSeqOrderByStartDateDesc(userSeq);
    List<UserCareer> careerList = new ArrayList<>();
    for(VHordYundongStat stat: statList){
      boolean savedFlag = false;
      for(UserCareer savedCareer: savedList){
        if (stat.getHordYundongCd().equals(savedCareer.getHordCd())) {
          savedFlag = true;
          break;
        }
      }
      if(!savedFlag){
        UserCareer career = new UserCareer();
        career.setDesignationCd(codeMgtService.searchCodeToCategoryAndValue("DSN", stat.getImyongGubunNm()));
        career.setWorkPositionStr(stat.getImyongbuseoChongching());
        career.setWorkTitle(codeMgtService.searchCodeToCategoryAndValue("JT", stat.getImyongJikgeupNm()));
        career.setStartDate(stat.getStartDate());
        career.setEndDate(stat.getEndDate());
        career.setWorkDay(stat.getWorkDay()==null?0:stat.getWorkDay());
        career.setWorkMonth(stat.getWorkMonth()==null?0:stat.getWorkMonth());
        career.setHordCd(stat.getHordYundongCd());
        careerList.add(career);
      }
    }
    return careerList;
  }
}
