package com.ywt.mg.core.utils;


import com.google.gson.JsonObject;
import com.ywt.mg.configs.Constants;
import com.ywt.mg.domain.models.enums.TerminalEnum;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class BizUtil {

    /**
     * 获取对应的患者客户端terminal值
     * <p>
     * 根据当前客户端，判断是否是医生版，若是则返回与该医生版对应的患者客户端terminal值，否则返回0
     *
     * @param currentTerminal 当前客户端terminal值
     * @return 对应的患者客户端terminal值, 0: 找不到对应值
     */
    public static int getAssociatedPatientTerminalValue(int currentTerminal) {
        if (!TerminalEnum.isDoctorTerminal(currentTerminal)) return 0;
        switch (TerminalEnum.valueOf(currentTerminal)) {
            case DoctorWxOfficial:
                return TerminalEnum.PatientWxOfficial.getValue();
            case TAI_HE_INTERNET_HOSPITAL_DOCTOR_WX_OFFICIAL:
                return TerminalEnum.TAI_HE_INTERNET_HOSPITAL_PATIENT_WX_OFFICIAL.getValue();
            default:
                return 0;
        }
    }

    /**
     * 获取对应的医生客户端terminal值
     * <p>
     * 根据当前客户端，判断是否是患者版，若是则返回与该医生版对应的医生客户端terminal值，否则返回0
     *
     * @param currentTerminal 当前客户端terminal值
     * @return 对应的医生客户端terminal值, 0: 找不到对应值
     */
    public static int getAssociatedDoctorTerminalValue(int currentTerminal) {
        if (!TerminalEnum.isPatientTerminal(currentTerminal)) return 0;
        switch (TerminalEnum.valueOf(currentTerminal)) {
            case PatientWxOfficial:
            case PatientWxApp:
                return TerminalEnum.DoctorWxOfficial.getValue();
            case TAI_HE_INTERNET_HOSPITAL_PATIENT_MINI_PROGRAM:
            case TAI_HE_INTERNET_HOSPITAL_PATIENT_WX_OFFICIAL:
                return TerminalEnum.TAI_HE_INTERNET_HOSPITAL_DOCTOR_WX_OFFICIAL.getValue();
            default:
                return 0;
        }
    }

    /**
     * 获取对应的患者客户端terminal
     * <p>
     * 根据当前客户端，判断是否是医生版，若是则返回与该医生版对应的患者客户端terminal值，否则返回0
     *
     * @param currentTerminal 当前客户端terminal值
     * @return 对应的患者客户端terminal
     */
    public static TerminalEnum getAssociatedPatientTerminal(int currentTerminal) {
        if (!TerminalEnum.isDoctorTerminal(currentTerminal)) return TerminalEnum.UNKNOWN;
        switch (TerminalEnum.valueOf(currentTerminal)) {
            case DoctorWxOfficial:
                return TerminalEnum.PatientWxOfficial;
            case TAI_HE_INTERNET_HOSPITAL_DOCTOR_WX_OFFICIAL:
                return TerminalEnum.TAI_HE_INTERNET_HOSPITAL_PATIENT_WX_OFFICIAL;
            default:
                return TerminalEnum.UNKNOWN;
        }
    }

    /**
     * 生成模板消息跳转小程序所需的参数
     *
     * @param appId    跳转的小程序appId
     * @param pagePath 页面
     * @return param string
     */
    public static String getMiniProgramParam(String appId, String pagePath) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("appid", appId);
        jsonObject.addProperty("pagepath", pagePath);
        return jsonObject.toString();
    }

    /**
     * 对医学界文章内容根据字数进行截取
     *
     * @param rawText  原始文本
     * @param splitLen 截取长度
     * @return 截取后的文本
     */
    public static String getArticleContentDigest(String rawText, int splitLen) {
        rawText = removeHTMLTags(rawText);
        if (StringHelper.isNullOrEmpty(rawText)) return "";
        if (rawText.length() < splitLen) return rawText;
        return rawText.substring(0, splitLen);
    }

    /**
     * 对医学界文章内容根据百分比进行截取
     *
     * @param rawText 原始文本
     * @param percent 截取的百分比，range 0.0F~1.0F
     * @return 截取后的文本
     */
    public static String getArticleContentDigest(String rawText, float percent) {
        rawText = removeHTMLTags(rawText);
        if (StringHelper.isNullOrEmpty(rawText)) return "";
        if (percent < 0) percent = 0F;
        if (percent > 1F) percent = 1F;
        int splitLen = (int) Math.floor(rawText.length() * percent);
        if (rawText.length() < splitLen) return rawText;
        return rawText.substring(0, splitLen);
    }

    private static String removeHTMLTags(String htmlStr) {
        String RegExScript = "<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String RegExStyle = "<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String RegExHtml = "<[^>]+>"; //定义HTML标签的正则表达式
        String regexNBSP = "&nbsp;";

        Pattern pScript = Pattern.compile(RegExScript, Pattern.CASE_INSENSITIVE);
        Matcher mScript = pScript.matcher(htmlStr);
        htmlStr = mScript.replaceAll(""); //过滤script标签

        Pattern pStyle = Pattern.compile(RegExStyle, Pattern.CASE_INSENSITIVE);
        Matcher mStyle = pStyle.matcher(htmlStr);
        htmlStr = mStyle.replaceAll(""); //过滤style标签

        Pattern pHtml = Pattern.compile(RegExHtml, Pattern.CASE_INSENSITIVE);
        Matcher mHtml = pHtml.matcher(htmlStr);
        htmlStr = mHtml.replaceAll(""); //过滤html标签

        Pattern pNbsp = Pattern.compile(regexNBSP, Pattern.CASE_INSENSITIVE);
        Matcher mNbsp = pNbsp.matcher(htmlStr);
        htmlStr = mNbsp.replaceAll(" ");

        return htmlStr.trim(); //返回文本字符串
    }

    public static boolean containsEmoji(String source) {
        return Pattern.compile("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]").matcher(source).find();
    }

    public static int getIntFromMapSafely(Map<String, Object> map, String key, int defaultValue) {
        return map == null ? defaultValue : Checker.getIntegerValue((Integer) map.getOrDefault(key, defaultValue), defaultValue);
    }

    public static int getIntFromMapSafely(Map<String, Object> map, String key) {
        return getIntFromMapSafely(map, key, 0);
    }

    public static String getStringFromMapSafely(Map<String, Object> map, String key, String defaultValue) {
        return map == null ? defaultValue : Checker.getStringValue((String) map.getOrDefault(key, defaultValue), defaultValue);
    }

    public static String getStringFromMapSafely(Map<String, Object> map, String key) {
        return getStringFromMapSafely(map, key, "");
    }

    public static boolean endsWithIgnoreCase(String str, String suffix) {
        int suffixLength = suffix.length();
        return str.regionMatches(true, str.length() - suffixLength, suffix, 0, suffixLength);
    }


    public static String getHospitalNameByHospitalId(int hospitalId) {
        switch (hospitalId) {
            default:
                return "";
            case Constants.HOSPITAL_ID_TAIHE:
                return Constants.HOSPITAL_NAME_TAIHE;
            case Constants.HOSPITAL_ID_NFYYBYFY:
                return Constants.HOSPITAL_NAME_NFYYBYFY;
        }
    }
}
