package com.engine.recruit.entity.position;

import com.alibaba.fastjson.JSON;
import com.engine.recruit.exception.CustomizeRunTimeException;
import com.weaver.rpa.sdk.clients.application.resume.ERPAResumeSDKClient;
import com.weaver.rpa.sdk.clients.application.resume.entity.ResumeJobDynamicField;
import com.weaver.rpa.sdk.clients.core.ERPASDKClients;
import weaver.formmode.recruit.modeexpand.util.RecruitPositionUtil;
import weaver.general.BaseBean;

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

/**
 * @author:dxfeng
 * @createTime: 2024/05/27
 * @version: 1.0
 */
public class PositionSdkInstance {
    private ERPAResumeSDKClient resumeSDKClient;
    // 职位类型
    private List<ResumeJobDynamicField.Item> boss_type;
    private List<ResumeJobDynamicField.Item> liepin_type;
    private List<ResumeJobDynamicField.Item> lagou_type;
    private List<ResumeJobDynamicField.Item> job51_type;
    private List<ResumeJobDynamicField.Item> zhilian_type;

    // BOSS直聘 是否外驻
    private List<ResumeJobDynamicField.Item> boss_resident;
    private List<ResumeJobDynamicField.Item> boss_resident_country;
    private List<ResumeJobDynamicField.Item> boss_resident_language;
    private List<ResumeJobDynamicField.Item> boss_resident_time;
    private List<ResumeJobDynamicField.Item> boss_resident_time_2;

    // 前程无忧-发布城市
    private List<ResumeJobDynamicField.Item> job51_city;
    // 前程无忧-所属行业
    private List<ResumeJobDynamicField.Item> job51_industry;


    private static volatile PositionSdkInstance positionSdkInstance;

    private PositionSdkInstance() {

    }


    public static PositionSdkInstance getPositionSdkInstance() {
        if (positionSdkInstance == null) {
            synchronized (PositionSdkInstance.class) {
                if (positionSdkInstance == null) {
                    ERPAResumeSDKClient client = null;
                    try {
                        client = ERPASDKClients.getResumeSDKClient();
                    } catch (Exception e) {
                        new BaseBean().writeLog("client get resumeSDKClient failed！");
                    }
                    if (!RecruitPositionUtil.checkAvailable(client)) {
                        throw new CustomizeRunTimeException("checkAvailable失败！千里聆SDK不可用");
                    }
                    ResumeJobDynamicField resumeJobDynamicField = client.getResumeJobDynamicField();
                    positionSdkInstance = new PositionSdkInstance();
                    positionSdkInstance.setResumeSDKClient(client);
                    client.addResumeSavedListener(resumeMqMessage -> {
                        // 处理获取到的简历信息
                        new BaseBean().writeLog("【简历接收回调】：" + JSON.toJSONString(resumeMqMessage));
                        //Thread thread = new Thread(new SdkResumeSavedThread(resumeMqMessage));
                        //thread.start();
                    });
                    client.start();
                    positionSdkInstance.setBoss_resident(resumeJobDynamicField.getBoss_resident());
                    // 去除千里聆返回的重复数据
                    List<ResumeJobDynamicField.Item> boss_resident_country1 = resumeJobDynamicField.getBoss_resident_country();
                    List<ResumeJobDynamicField.Item> collect = new ArrayList<>(boss_resident_country1.stream()
                            .collect(Collectors.toMap(
                                    ResumeJobDynamicField.Item::getValue,
                                    item -> item,
                                    (existing, replacement) -> existing
                            ))
                            .values());
                    positionSdkInstance.setBoss_resident_country(collect);
                    positionSdkInstance.setBoss_resident_language(resumeJobDynamicField.getBoss_resident_language());
                    positionSdkInstance.setBoss_resident_time_2(resumeJobDynamicField.getBoss_resident_time_2());
                    positionSdkInstance.setBoss_resident_time(resumeJobDynamicField.getBoss_resident_time());

                    positionSdkInstance.setJob51_city(resumeJobDynamicField.getJob51_city());
                    positionSdkInstance.setJob51_industry(resumeJobDynamicField.getJob51_industry_txt());

                    positionSdkInstance.setBoss_type(resumeJobDynamicField.getBoss_type());
                    positionSdkInstance.setLiepin_type(resumeJobDynamicField.getLiepin_type());
                    positionSdkInstance.setLagou_type(resumeJobDynamicField.getLagou_type());
                    positionSdkInstance.setJob51_type(resumeJobDynamicField.getJob51_type());
                    positionSdkInstance.setZhilian_type(resumeJobDynamicField.getZhilian_type());
                }
            }
        }
        return positionSdkInstance;
    }


    /**
     * 判断千里聆SDK是否可用
     *
     * @return
     */
    public static boolean enableSdkClient() {
        BaseBean baseBean = new BaseBean();
        try {
            ERPAResumeSDKClient client = ERPASDKClients.getResumeSDKClient();
            if (client == null || !client.available()) {
                baseBean.writeLog("checkAvailable failed！client=" + JSON.toJSONString(client));
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            baseBean.writeLog("checkAvailable failed！", e);
            return false;
        }
    }


    public ERPAResumeSDKClient getResumeSDKClient() {
        return resumeSDKClient;
    }

    public void setResumeSDKClient(ERPAResumeSDKClient resumeSDKClient) {
        this.resumeSDKClient = resumeSDKClient;
    }

    public List<ResumeJobDynamicField.Item> getBoss_type() {
        return boss_type;
    }

    public void setBoss_type(List<ResumeJobDynamicField.Item> boss_type) {
        this.boss_type = boss_type;
    }

    public List<ResumeJobDynamicField.Item> getLiepin_type() {
        return liepin_type;
    }

    public void setLiepin_type(List<ResumeJobDynamicField.Item> liepin_type) {
        this.liepin_type = liepin_type;
    }

    public List<ResumeJobDynamicField.Item> getLagou_type() {
        return lagou_type;
    }

    public void setLagou_type(List<ResumeJobDynamicField.Item> lagou_type) {
        this.lagou_type = lagou_type;
    }

    public List<ResumeJobDynamicField.Item> getJob51_type() {
        return job51_type;
    }

    public void setJob51_type(List<ResumeJobDynamicField.Item> job51_type) {
        this.job51_type = job51_type;
    }

    public List<ResumeJobDynamicField.Item> getZhilian_type() {
        return zhilian_type;
    }

    public void setZhilian_type(List<ResumeJobDynamicField.Item> zhilian_type) {
        this.zhilian_type = zhilian_type;
    }

    public List<ResumeJobDynamicField.Item> getBoss_resident() {
        return boss_resident;
    }

    public void setBoss_resident(List<ResumeJobDynamicField.Item> boss_resident) {
        this.boss_resident = boss_resident;
    }

    public List<ResumeJobDynamicField.Item> getBoss_resident_country() {
        return boss_resident_country;
    }

    public void setBoss_resident_country(List<ResumeJobDynamicField.Item> boss_resident_country) {
        this.boss_resident_country = boss_resident_country;
    }

    public List<ResumeJobDynamicField.Item> getBoss_resident_language() {
        return boss_resident_language;
    }

    public void setBoss_resident_language(List<ResumeJobDynamicField.Item> boss_resident_language) {
        this.boss_resident_language = boss_resident_language;
    }

    public List<ResumeJobDynamicField.Item> getBoss_resident_time() {
        return boss_resident_time;
    }

    public void setBoss_resident_time(List<ResumeJobDynamicField.Item> boss_resident_time) {
        this.boss_resident_time = boss_resident_time;
    }

    public List<ResumeJobDynamicField.Item> getBoss_resident_time_2() {
        return boss_resident_time_2;
    }

    public void setBoss_resident_time_2(List<ResumeJobDynamicField.Item> boss_resident_time_2) {
        this.boss_resident_time_2 = boss_resident_time_2;
    }

    public List<ResumeJobDynamicField.Item> getJob51_city() {
        return job51_city;
    }

    public void setJob51_city(List<ResumeJobDynamicField.Item> job51_city) {
        this.job51_city = job51_city;
    }

    public List<ResumeJobDynamicField.Item> getJob51_industry() {
        return job51_industry;
    }

    public void setJob51_industry(List<ResumeJobDynamicField.Item> job51_industry) {
        this.job51_industry = job51_industry;
    }
}
