package com.ningdatech.pmapi.expert.assembler;


import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.model.FileBasicInfo;
import com.ningdatech.pmapi.expert.model.*;
import com.ningdatech.pmapi.expert.model.bo.ExpertInfoSensitiveFieldCheckBO;
import com.ningdatech.pmapi.expert.model.cmd.ExpertFullInfoSaveCmd;
import com.ningdatech.pmapi.expert.model.cmd.ExpertInfoModifyCmd;
import com.ningdatech.pmapi.expert.model.dto.*;
import com.ningdatech.pmapi.meta.constant.DictExpertInfoTypeEnum;
import com.ningdatech.pmapi.meta.constant.ExpertTagEnum;
import com.ningdatech.pmapi.meta.model.ExpertRegionInfo;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
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, 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 = new ArrayList<>();
        if (Objects.nonNull(otherInfo)) {
            other = otherInfo.getOther();
        }
        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> allTagFieldInfoList
            , List<TagFieldInfo> originalTagFieldInfoList, ExpertTagEnum tagExpertInfoFieldEnum) {
        if (CollectionUtils.isNotEmpty(originalTagFieldInfoList)) {
            originalTagFieldInfoList = originalTagFieldInfoList.stream().map(r -> {
                r.setTagFieldName(tagExpertInfoFieldEnum.getKey());
                return r;
            }).collect(Collectors.toList());
            allTagFieldInfoList.addAll(originalTagFieldInfoList);
        }
    }

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

        List<DictionaryFieldInfo> recommendedWay = new ArrayList<>();
        if (Objects.nonNull(recommendInfo)) {
            recommendedWay = recommendInfo.getRecommendedWay();
        }
        List<DictionaryFieldInfo> dictionaryFieldInfoList = new ArrayList<>();
        assemblerDictionaryFieldName(dictionaryFieldInfoList, political, DictExpertInfoTypeEnum.POLITICAL);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, expertType, DictExpertInfoTypeEnum.EXPERT_TYPE);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, edu, DictExpertInfoTypeEnum.EDU);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, degree, DictExpertInfoTypeEnum.DEGREE);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, jobStatus, DictExpertInfoTypeEnum.JOB_STATUS);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, companyAttribute, DictExpertInfoTypeEnum.COMPANY_ATTRIBUTE);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, administrativeRank, DictExpertInfoTypeEnum.ADMINISTRATIVE_RANK);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, titleLevel, DictExpertInfoTypeEnum.TITLE_LEVEL);
        assemblerDictionaryFieldName(dictionaryFieldInfoList, recommendedWay, DictExpertInfoTypeEnum.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<DictionaryFieldInfo> allDictionaryFieldInfoList, List<DictionaryFieldInfo> originalDictionaryFieldInfoList
            , DictExpertInfoTypeEnum 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, ExpertRecommendInfo recommendInfo, ExpertOtherInfo otherInfo) {
        ExpertUserFullInfoDTO expertFullInfo = new ExpertUserFullInfoDTO();

        expertFullInfo.setIsDingUser(basicInfo.getIsDingUser());
        expertFullInfo.setPhoneNo(basicInfo.getPhoneNo());
        expertFullInfo.setGender(basicInfo.getGender());
        expertFullInfo.setName(basicInfo.getName());
        if (Objects.nonNull(basicInfo.getAvatarFile())) {
            expertFullInfo.setAvatarFileId(basicInfo.getAvatarFile().getFileId());
        }
        expertFullInfo.setIdCard(basicInfo.getIdCard());
        expertFullInfo.setOfficePhone(basicInfo.getOfficePhone());
        expertFullInfo.setBirth(basicInfo.getBirth());
        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.setSchool(eduInfo.getSchool());
        expertFullInfo.setGraduatedAt(eduInfo.getGraduatedAt());
        expertFullInfo.setAcademicTitle(eduInfo.getAcademicTitle());

        if (CollectionUtils.isNotEmpty(eduInfo.getGraduationCertificateFile())) {
            expertFullInfo.setGraduationCertificateFileIdList(eduInfo.getGraduationCertificateFile().stream().map(FileBasicInfo::getFileId).collect(Collectors.toList()));
        }
        if (CollectionUtils.isNotEmpty(eduInfo.getDegreeCertificateFile())) {

            expertFullInfo.setDegreeCertificateFileIdList(eduInfo.getDegreeCertificateFile().stream().map(FileBasicInfo::getFileId).collect(Collectors.toList()));
        }

        expertFullInfo.setRetiredAt(jobInfo.getRetiredAt());
        expertFullInfo.setCompany(jobInfo.getCompany());
        expertFullInfo.setCompanyUniqCode(jobInfo.getCompanyUniqCode());
        expertFullInfo.setLegalEntityCode(jobInfo.getLegalEntityCode());
        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));
        }
        expertFullInfo.setAwards(professionalInfo.getAwards());
        expertFullInfo.setRecognitionReward(professionalInfo.getRecognitionReward());

        if (Objects.nonNull(basicInfo.getExpertRegionInfo())) {
            expertFullInfo.setRegionCode(basicInfo.getExpertRegionInfo().getRegionCode());
            expertFullInfo.setRegionLevel(basicInfo.getExpertRegionInfo().getRegionLevel());
        }
        // 推荐证明材料单独装配
        if (Objects.nonNull(recommendInfo)) {
            List<FileBasicInfo> recommendationProofFile = recommendInfo.getRecommendationProofFile();
            if (CollectionUtils.isNotEmpty(recommendationProofFile)) {
                expertFullInfo.setRecommendationProofFileIdList(CollUtils.fieldList(recommendationProofFile, FileBasicInfo::getFileId));

            }
        }
        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, recommendInfo, 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);
        return expertInfoModifyCmd;
    }


}
