package com.engine.recruit.constant;

import com.engine.recruit.conn.ApplicantCommonInfo;
import com.engine.recruit.exception.CustomizeRunTimeException;
import org.apache.commons.lang.StringUtils;
import weaver.general.BaseBean;

import java.net.URI;
import java.nio.charset.StandardCharsets;

/**
 * @author:dxfeng
 * @createTime: 2024/01/17
 * @version: 1.0
 */
public class RecruitConstant {
    /**
     * URL分隔符
     */
    private static final String URL_SPLIT = "/";


    public static final String HTTP_URL;
    public static final String RECRUIT_MESSAGE_TYPE;
    public static final String INTERVIEW_MESSAGE_TYPE;
    public static final String INTERVIEW_ADD_MESSAGE_TITLE;
    public static final String INTERVIEW_CANCEL_MESSAGE_TITLE;
    public static final String INTERVIEW_EVALUATE_MESSAGE_TITLE;
    public static final String INTERVIEW_ADJUSTMENT_MESSAGE_TITLE;
    public static final String JOIN_BLACKLIST_MESSAGE_TITLE;
    public static final String SCREENING_MESSAGE_TYPE;
    public static final String SCREENING_MESSAGE_TITLE;
    public static final String SCREENING_MESSAGE_URL;
    public static final String MOBILE_APPID_COLLECT;
    public static final String COLLECT_MESSAGE_LINK;
    public static final String MOBILE_APPID_OFFER;
    public static final String OFFER_MOBILE_URL;
    public static final String APPLICANTS_RESUMES_CATEGORY;
    public static final String INTERVIEW_FEEDBACK_URL;
    public static final String REMIND_SEARCH_LINK;
    public static final String OCR_TYPE;
    public static final String OCR_URL;
    public static final String APP_ID;
    public static final String APP_SECRET;

    public static final String MODE_APP_NAME;

    public static final String APPLICANT_URL;

    static BaseBean baseBean = new BaseBean();

    static {

        HTTP_URL = getRecruitPropValue("HTTP_URL");
        RECRUIT_MESSAGE_TYPE = getRecruitPropValue("RECRUIT_MESSAGE_TYPE");

        MOBILE_APPID_COLLECT = getRecruitPropValue("MOBILE_APPID_COLLECT");
        // 信息采集移动建模地址
        COLLECT_MESSAGE_LINK = getCompleteUrl(getRecruitPropValue("COLLECT_MESSAGE_LINK"));
        MOBILE_APPID_OFFER = getRecruitPropValue("MOBILE_APPID_OFFER");
        // offer反馈移动建模地址
        OFFER_MOBILE_URL = getCompleteUrl(getRecruitPropValue("OFFER_MOBILE_URL"));
        APPLICANTS_RESUMES_CATEGORY = getRecruitPropValue("APPLICANTS_RESUMES_CATEGORY");

        // 简历订阅相关
        REMIND_SEARCH_LINK = getCompleteUrl(getRecruitPropValue("REMIND_SEARCH_LINK"));
        // OCR相关
        OCR_TYPE = getRecruitPropValue("OCR_TYPE");
        OCR_URL = getRecruitPropValue("OCR_URL");
        APP_ID = getRecruitPropValue("APP_ID");
        APP_SECRET = getRecruitPropValue("APP_SECRET");

        /*处理非必填字段，为空的话，指定默认值*/

        // 面试反馈消息提醒，消息来源
        String interviewMessageType = getRecruitPropValue("INTERVIEW_MESSAGE_TYPE");
        if (StringUtils.isBlank(interviewMessageType)) {
            interviewMessageType = RECRUIT_MESSAGE_TYPE;
        }
        INTERVIEW_MESSAGE_TYPE = interviewMessageType;

        // 添加面试消息提醒标题
        String interviewAddMessageTitle = getRecruitPropValue("INTERVIEW_ADD_MESSAGE_TITLE");
        if (StringUtils.isBlank(interviewAddMessageTitle)) {
            interviewAddMessageTitle = "面试安排提醒";
        }
        INTERVIEW_ADD_MESSAGE_TITLE = interviewAddMessageTitle;

        // 取消面试消息提醒标题
        String interviewCancelMessageTitle = getRecruitPropValue("INTERVIEW_CANCEL_MESSAGE_TITLE");
        if (StringUtils.isBlank(interviewCancelMessageTitle)) {
            interviewCancelMessageTitle = "取消面试提醒";
        }
        INTERVIEW_CANCEL_MESSAGE_TITLE = interviewCancelMessageTitle;

        // 面试评价消息提醒标题
        String interviewEvaluateMessageTitle = getRecruitPropValue("INTERVIEW_EVALUATE_MESSAGE_TITLE");
        if (StringUtils.isBlank(interviewEvaluateMessageTitle)) {
            interviewEvaluateMessageTitle = "面试评价提醒";
        }
        INTERVIEW_EVALUATE_MESSAGE_TITLE = interviewEvaluateMessageTitle;

        // 调整面试消息提醒标题
        String interviewAdjustmentMessageTitle = getRecruitPropValue("INTERVIEW_ADJUSTMENT_MESSAGE_TITLE");
        if (StringUtils.isBlank(interviewAdjustmentMessageTitle)) {
            interviewAdjustmentMessageTitle = "面试调整提醒";
        }
        INTERVIEW_ADJUSTMENT_MESSAGE_TITLE = interviewAdjustmentMessageTitle;

        // 人才黑名单通知,消息提醒标题
        String joinBlacklistMessageTitle = getRecruitPropValue("JOIN_BLACKLIST_MESSAGE_TITLE");
        if (StringUtils.isBlank(joinBlacklistMessageTitle)) {
            joinBlacklistMessageTitle = "人才黑名单通知";
        }
        JOIN_BLACKLIST_MESSAGE_TITLE = joinBlacklistMessageTitle;

        // 筛选反馈消息提醒，消息来源
        String screeningMessageType = getRecruitPropValue("SCREENING_MESSAGE_TYPE");
        if (StringUtils.isBlank(screeningMessageType)) {
            screeningMessageType = RECRUIT_MESSAGE_TYPE;
        }
        SCREENING_MESSAGE_TYPE = screeningMessageType;

        // 筛选反馈，消息提醒标题
        String screeningMessageTitle = getRecruitPropValue("SCREENING_MESSAGE_TITLE");
        if (StringUtils.isBlank(screeningMessageTitle)) {
            screeningMessageTitle = "简历筛选";
        }
        SCREENING_MESSAGE_TITLE = screeningMessageTitle;

        // 筛选反馈，消息提醒链接地址
        String screeningMessageUrl = getRecruitPropValue("SCREENING_MESSAGE_URL");
        if (StringUtils.isBlank(screeningMessageUrl)) {
            // 根据customSearchCode，获取默认的查询列表
            screeningMessageUrl = "/wui/index.html#/main/cube/search?customid=" + ApplicantCommonInfo.getCustomSearchId("b746e3cf80454289a3ade2b17147e297");
        }
        SCREENING_MESSAGE_URL = screeningMessageUrl;

        // 面试反馈地址
        String interviewFeedbackUrl = getRecruitPropValue("INTERVIEW_FEEDBACK_URL");
        if (StringUtils.isBlank(interviewFeedbackUrl)) {
            interviewFeedbackUrl = "/spa/custom/static/index.html#/main/cs/app/9277c228302347dc88a958b69ee96234_Interview";
        }
        INTERVIEW_FEEDBACK_URL = getCompleteUrl(interviewFeedbackUrl);

        String modeAppName = getRecruitPropValue("MODE_APP_NAME");
        if (StringUtils.isBlank(modeAppName)) {
            modeAppName = "JCL_招聘管理";
        }
        MODE_APP_NAME = modeAppName;

        String applicantUrl = getRecruitPropValue("APPLICANT_URL");
        if (StringUtils.isBlank(applicantUrl)) {
            applicantUrl = "/spa/custom/static/index.html#/main/cs/app/d2c5417e775e4cc6b51e27aae45767c8_RecruitFlowInfo";
        }
        APPLICANT_URL = applicantUrl;
    }

    /**
     * 读取jclRecruit.properties配置文件的配置项
     *
     * @param key
     * @return
     */
    public static String getRecruitPropValue(String key) {
        String value = baseBean.getPropValue("jclRecruit", key);
        value = new String(value.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        return value;
    }


    /**
     * 对外发布的URL地址，处理域名
     *
     * @param url
     * @return
     */
    public static String getCompleteUrl(String url) {
        if (StringUtils.isBlank(url)) {
            return "";
        }
        try {
            URI u = new URI(url);
            // 如果URL对象的getHost()方法获取到了域名，则直接返回原始URL
            if (u.getHost() != null) {
                return url;
            } else {
                // 否则，判断URL是否以"/"开头，如果是则在它前面拼接上域名
                if (url.startsWith(URL_SPLIT)) {
                    return HTTP_URL + url;
                } else {
                    // 如果URL既不是完整URL也不是以"/"开头，则在URL开头添加"/"，再拼接上域名
                    return HTTP_URL + URL_SPLIT + url;
                }
            }
        } catch (Exception e) {
            throw new CustomizeRunTimeException(e);
        }
    }

}
