package com.hz.pm.api.expert.assembler;


import cn.hutool.core.collection.CollUtil;
import com.hz.pm.api.common.model.FileBasicInfo;
import com.hz.pm.api.expert.model.*;
import com.hz.pm.api.expert.model.bo.ExpertInfoSensitiveFieldCheckBO;
import com.hz.pm.api.expert.model.cmd.ExpertFullInfoSaveCmd;
import com.hz.pm.api.expert.model.cmd.ExpertInfoModifyCmd;
import com.hz.pm.api.expert.model.dto.*;
import com.hz.pm.api.meta.constant.ExpertDictTypeEnum;
import com.hz.pm.api.meta.constant.ExpertTagEnum;
import com.hz.pm.api.meta.model.ExpertRegionInfo;
import com.ningdatech.basic.util.CollUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2022/7/22 下午5:19
 */

public class ExpertInfoCmdAssembler {

    public static ExpertFullInfoSaveCmd buildExpertFullInfoSaveCmd(Long userId
            , ExpertBasicInfo expertBasicInfo, ExpertEduInfo expertEduInfo
            , ExpertJobInfo expertJobInfo, ExpertProfessionalInfo expertProfessionalInfo) {

        ExpertFullInfoSaveCmd expertFullInfoSaveCmd = new ExpertFullInfoSaveCmd();
        ExpertUserFullInfoDTO expertUserInfoDTO = buildExpertUserFullInfoDTO(
                expertBasicInfo, expertEduInfo, expertJobInfo, expertProfessionalInfo, null);
        List<ExpertDictionaryDTO> expertDictionaryList =
                buildExpertDictionaryList(expertBasicInfo, expertEduInfo, expertJobInfo, expertProfessionalInfo, null);
        List<ExpertTagDTO> expertTagList = buildExpertTagList(expertProfessionalInfo, expertBasicInfo, null);
        // 专家履职意向(区域编码）
        List<ExpertRegionDTO> expertIntentionWorkRegionInfo = buildExpertRegionList(expertBasicInfo);
        List<ExpertAvoidCompanyDTO> expertAvoidCompanyList = buildExpertAvoidCompanyList(expertProfessionalInfo);

        expertFullInfoSaveCmd.setUserId(userId);
        expertFullInfoSaveCmd.setExpertUserInfoDTO(expertUserInfoDTO);
        expertFullInfoSaveCmd.setExpertDictionaryList(expertDictionaryList);
        expertFullInfoSaveCmd.setExpertTagList(expertTagList);
        expertFullInfoSaveCmd.setExpertIntentionWorkRegionInfo(expertIntentionWorkRegionInfo);
        expertFullInfoSaveCmd.setExpertAvoidCompanyList(expertAvoidCompanyList);

        return expertFullInfoSaveCmd;
    }


    private static List<ExpertAvoidCompanyDTO> buildExpertAvoidCompanyList(ExpertProfessionalInfo professionalInfo) {
        List<ExpertAvoidCompanyDTO> expertAvoidCompanyList = new ArrayList<>();
        List<ExpertAvoidCompanyInfo> avoidCompanyList = professionalInfo.getAvoidCompanyList();
        if (CollectionUtils.isNotEmpty(avoidCompanyList)) {
            expertAvoidCompanyList = avoidCompanyList.stream().map(r -> {
                ExpertAvoidCompanyDTO expertAvoidCompanyDTO = new ExpertAvoidCompanyDTO();
                expertAvoidCompanyDTO.setCompanyName(r.getCompanyName());
                expertAvoidCompanyDTO.setCompanyUniqCode(r.getCompanyUniqCode());
                return expertAvoidCompanyDTO;
            }).collect(Collectors.toList());
        }
        return expertAvoidCompanyList;
    }

    private static List<ExpertRegionDTO> buildExpertRegionList(ExpertBasicInfo basicInfo) {
        List<ExpertRegionInfo> expertIntentionWorkRegions = basicInfo.getExpertIntentionWorkRegions();
        if (CollectionUtils.isEmpty(expertIntentionWorkRegions)) {
            return new ArrayList<>();
        }
        return expertIntentionWorkRegions.stream().map(r -> {
            ExpertRegionDTO expertRegionDTO = new ExpertRegionDTO();
            expertRegionDTO.setRegionCode(r.getRegionCode());
            expertRegionDTO.setRegionLevel(r.getRegionLevel());
            return expertRegionDTO;
        }).collect(Collectors.toList());
    }

    private static List<ExpertTagDTO> buildExpertTagList(ExpertProfessionalInfo professionalInfo
            , ExpertBasicInfo basicInfo, ExpertOtherInfo otherInfo) {
        List<TagFieldInfo> goodAt = professionalInfo.getGoodAt();
        List<TagFieldInfo> technicalExpertise = professionalInfo.getTechnicalExpertise();
        List<TagFieldInfo> industrySector = professionalInfo.getIndustrySector();
        List<TagFieldInfo> expertSource = basicInfo.getExpertSource();
        List<TagFieldInfo> other;
        if (Objects.nonNull(otherInfo)) {
            other = otherInfo.getOther();
        } else {
            other = professionalInfo.getOther();
        }
        if (other == null) {
            other = Collections.emptyList();
        }
        List<TagFieldInfo> tagFieldInfoList = new ArrayList<>();
        assemblerTagExpertInfoFieldName(tagFieldInfoList, goodAt, ExpertTagEnum.GOOD_AT);
        assemblerTagExpertInfoFieldName(tagFieldInfoList, technicalExpertise, ExpertTagEnum.TECHNICAL_EXPERTISE);
        assemblerTagExpertInfoFieldName(tagFieldInfoList, industrySector, ExpertTagEnum.INDUSTRY_SECTOR);
        assemblerTagExpertInfoFieldName(tagFieldInfoList, expertSource, ExpertTagEnum.EXPERT_SOURCE);
        assemblerTagExpertInfoFieldName(tagFieldInfoList, other, ExpertTagEnum.OTHER);

        return tagFieldInfoList.stream().map(r -> {
            ExpertTagDTO expertTagDTO = new ExpertTagDTO();
            expertTagDTO.setTagCode(r.getTagCode());
            expertTagDTO.setExpertInfoField(r.getTagFieldName());
            return expertTagDTO;
        }).collect(Collectors.toList());
    }

    private static void assemblerTagExpertInfoFieldName(List<TagFieldInfo> allTagList
            , List<TagFieldInfo> targetTagList, ExpertTagEnum expertTagEnum) {
        if (CollUtil.isNotEmpty(targetTagList)) {
            targetTagList.forEach(r -> r.setTagFieldName(expertTagEnum.getKey()));
            allTagList.addAll(targetTagList);
        }
    }

    private static List<ExpertDictionaryDTO> buildExpertDictionaryList(ExpertBasicInfo basicInfo, ExpertEduInfo eduInfo
            , ExpertJobInfo jobInfo, ExpertProfessionalInfo professionalInfo, ExpertRecommendInfo recommendInfo) {
        List<DictFieldInfoDTO> political = basicInfo.getPolitical();
        List<DictFieldInfoDTO> expertType = basicInfo.getExpertType();
        List<DictFieldInfoDTO> edu = eduInfo.getEdu();
        List<DictFieldInfoDTO> degree = eduInfo.getDegree();
        List<DictFieldInfoDTO> jobStatus = jobInfo.getJobStatus();
        List<DictFieldInfoDTO> companyAttribute = jobInfo.getCompanyAttribute();
        List<DictFieldInfoDTO> administrativeRank = jobInfo.getAdministrativeRank();
        List<DictFieldInfoDTO> titleLevel = professionalInfo.getTitleLevel();

        List<DictFieldInfoDTO> recommendedWay = new ArrayList<>();
        if (Objects.nonNull(recommendInfo)) {
            recommendedWay = recommendInfo.getRecommendedWay();
        }
        List<DictFieldInfoDTO> dictionaryFieldInfoList = new ArrayList<>();
        assemblerDictionaryFieldName(dictionaryFieldInfoList, political, ExpertDictTypeEnum.POLITICAL);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, expertType, ExpertDictTypeEnum.EXPERT_TYPE);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, edu, ExpertDictTypeEnum.EDU);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, degree, ExpertDictTypeEnum.DEGREE);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, jobStatus, ExpertDictTypeEnum.JOB_STATUS);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, companyAttribute, ExpertDictTypeEnum.COMPANY_ATTRIBUTE);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, administrativeRank, ExpertDictTypeEnum.ADMINISTRATIVE_RANK);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, titleLevel, ExpertDictTypeEnum.TITLE_LEVEL);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, recommendedWay, ExpertDictTypeEnum.RECOMMENDED_WAY);

        return dictionaryFieldInfoList.stream().map(r -> {
            ExpertDictionaryDTO expertDictionaryDTO = new ExpertDictionaryDTO();
            expertDictionaryDTO.setDictionaryCode(r.getDictionaryCode());
            expertDictionaryDTO.setExpertInfoField(r.getDictionaryFieldName());
            return expertDictionaryDTO;
        }).collect(Collectors.toList());
    }

    private static void assemblerDictionaryFieldName(
            List<DictFieldInfoDTO> allDictionaryFieldInfoList, List<DictFieldInfoDTO> originalDictionaryFieldInfoList
            , ExpertDictTypeEnum dictExpertInfoTypeEnum) {
        if (CollectionUtils.isNotEmpty(originalDictionaryFieldInfoList)) {
            originalDictionaryFieldInfoList = originalDictionaryFieldInfoList.stream().map(r -> {
                r.setDictionaryFieldName(dictExpertInfoTypeEnum.getKey());
                return r;
            }).collect(Collectors.toList());
            allDictionaryFieldInfoList.addAll(originalDictionaryFieldInfoList);
        }
    }

    private static ExpertUserFullInfoDTO buildExpertUserFullInfoDTO(ExpertBasicInfo basicInfo, ExpertEduInfo eduInfo
            , ExpertJobInfo jobInfo, ExpertProfessionalInfo professionalInfo, ExpertOtherInfo otherInfo) {
        ExpertUserFullInfoDTO expertFullInfo = new ExpertUserFullInfoDTO();

        expertFullInfo.setIsDingUser(basicInfo.getIsDingUser());
        expertFullInfo.setPhoneNo(basicInfo.getPhoneNo());
        expertFullInfo.setGender(basicInfo.getGender());
        expertFullInfo.setName(basicInfo.getName());

        expertFullInfo.setIdCard(basicInfo.getIdCard());
        expertFullInfo.setOfficePhone(basicInfo.getOfficePhone());
        expertFullInfo.setBirthday(basicInfo.getBirthday());
        expertFullInfo.setBankNo(basicInfo.getBankNo());
        expertFullInfo.setBank(basicInfo.getBank());
        expertFullInfo.setEmail(basicInfo.getEmail());
        expertFullInfo.setHometown(basicInfo.getHometown());
        expertFullInfo.setNationality(basicInfo.getNationality());
        if (Objects.nonNull(otherInfo)) {
            expertFullInfo.setRemark(otherInfo.getRemark());
        }
        expertFullInfo.setSchoolMajor(eduInfo.getSchool());
        expertFullInfo.setGraduatedAt(eduInfo.getGraduatedAt());
        expertFullInfo.setAcademicTitle(eduInfo.getAcademicTitle());

        expertFullInfo.setCompany(jobInfo.getCompany());
        expertFullInfo.setCompanyUniqCode(jobInfo.getCompanyUniqCode());
        expertFullInfo.setAdministrativeDuties(jobInfo.getAdministrativeDuties());
        expertFullInfo.setStartWorkAt(jobInfo.getStartWorkAt());
        expertFullInfo.setAddress(jobInfo.getAddress());
        expertFullInfo.setExperience(jobInfo.getExperience());

        expertFullInfo.setTechnicalTitles(professionalInfo.getTechnicalTitles());
        if (CollectionUtils.isNotEmpty(professionalInfo.getTitleCertificateFile())) {
            expertFullInfo.setTitleCertificateFileIdList(CollUtils.fieldList(professionalInfo.getTitleCertificateFile(), FileBasicInfo::getFileId));
        }

        if (Objects.nonNull(basicInfo.getExpertRegionInfo())) {
            expertFullInfo.setRegionCode(basicInfo.getExpertRegionInfo().getRegionCode());
            expertFullInfo.setRegionLevel(basicInfo.getExpertRegionInfo().getRegionLevel());
        }
        return expertFullInfo;
    }

    public static ExpertInfoModifyCmd buildExpertInfoModifyCmd(Long userId
            , ExpertBasicInfo expertBasicInfo, ExpertEduInfo expertEduInfo
            , ExpertJobInfo expertJobInfo, ExpertProfessionalInfo expertProfessionalInfo
            , ExpertRecommendInfo recommendInfo
            , ExpertOtherInfo otherInfo
            , ExpertInfoSensitiveFieldCheckBO expertInfoSensitiveFieldCheckBO
            , ModifyApplyExtraInfo modifyApplyExtraInfo) {
        ExpertInfoModifyCmd expertInfoModifyCmd = new ExpertInfoModifyCmd();
        ExpertUserFullInfoDTO expertUserInfoDTO = buildExpertUserFullInfoDTO(expertBasicInfo, expertEduInfo, expertJobInfo, expertProfessionalInfo, otherInfo);

        List<ExpertDictionaryDTO> expertDictionaryList = buildExpertDictionaryList(expertBasicInfo, expertEduInfo, expertJobInfo, expertProfessionalInfo, recommendInfo);
        List<ExpertTagDTO> expertTagList = buildExpertTagList(expertProfessionalInfo, expertBasicInfo, otherInfo);
        // 专家履职意向(区域编码）
        List<ExpertRegionDTO> expertIntentionWorkRegionInfo = buildExpertRegionList(expertBasicInfo);
        List<ExpertAvoidCompanyDTO> expertAvoidCompanyList = buildExpertAvoidCompanyList(expertProfessionalInfo);

        if (Objects.nonNull(modifyApplyExtraInfo)) {
            ModifyApplyExtraInfoDTO modifyApplyExtraInfoDTO = new ModifyApplyExtraInfoDTO();
            modifyApplyExtraInfoDTO.setEvidenceList(modifyApplyExtraInfo.getEvidenceList());
            modifyApplyExtraInfoDTO.setFactSheet(modifyApplyExtraInfo.getFactSheet());
            expertInfoModifyCmd.setModifyApplyExtraInfo(modifyApplyExtraInfoDTO);
        }

        expertInfoModifyCmd.setUserId(userId);
        expertInfoModifyCmd.setExpertUserInfoDTO(expertUserInfoDTO);
        expertInfoModifyCmd.setExpertDictionaryList(expertDictionaryList);
        expertInfoModifyCmd.setExpertTagList(expertTagList);
        expertInfoModifyCmd.setExpertIntentionWorkRegionInfo(expertIntentionWorkRegionInfo);
        expertInfoModifyCmd.setExpertAvoidCompanyList(expertAvoidCompanyList);
        expertInfoModifyCmd.setExpertInfoSensitiveFieldCheckBO(expertInfoSensitiveFieldCheckBO);
        expertInfoModifyCmd.setBusinessStrips(expertJobInfo.getBusinessStrips());
        return expertInfoModifyCmd;
    }


}
