package com.simuwang.base.common.util;

import cn.hutool.core.util.StrUtil;
import com.simuwang.base.common.conts.ValuationConst;
import com.simuwang.base.pojo.valuation.PreAssetsValuationBase;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class ValuationDataUtils {

    /**
     * 支持正负整型或浮点型
     */
    public static boolean isNumber(String str) {
        return (StringUtils.isNotEmpty(str) && str.matches("^-?\\d+(\\.\\d+)?$"));
    }

    public static Double string2Double(String str) {
        if (StrUtil.isBlank(str) || StrUtil.isBlank(str.trim())) {
            return null;
        }

        str = str.trim().replace(",", "");
        try {
            if (str.endsWith("%")) {
                return Double.parseDouble(str.substring(0, str.length() - 1)) / 100D;
            }
            return Double.parseDouble(str);
        } catch (NumberFormatException e) {
            log.error("parse Double error");
        }
        return 0D;
    }

    public static int string2Int(String str) {
        if (str == null || "".equals(str.trim())) {
            return 0;
        }

        try {
            return Integer.parseInt(str.trim());
        } catch (NumberFormatException e) {
            log.error("parse Integer error");
        }
        return 0;
    }

    /**
     * 判断字符串是否为空或者null
     *
     * @param str 要验证的参数
     * @return boolean
     */
    public static boolean isValid(String str) {
        return null != str && str.length() != 0 && !"null".equals(str.toLowerCase());
    }

    /**
     * 判断字符串是有汉字
     *
     * @param str   待判断的字符串
     * @param isAll true:判断该字符串是否是汉字，false:判断该字符串是否包含汉字
     */
    public static boolean hasChinese(String str, boolean isAll) {
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+");
        Matcher m = pattern.matcher(str);
        if (isAll) {
            return m.find() && m.group(0).equals(str);
        } else {
            return m.find();
        }
    }

    public static String getSubjectCode(String str) {
        char[] cArr = str.toCharArray();
        if (!Character.isDigit(cArr[0])) {
            return str;
        }
        int startIndex = 0;
        for (int i = 0; i < cArr.length; i++) {
            int index = cArr[i];
            if ((index >= 48 && index <= 57) || (index >= 65 && index <= 90) || (index >= 97 && index <= 122)) {
                continue;
            }
            startIndex = i;
            break;
        }
        if (startIndex == 0) {
            return str;
        }
        str = str.substring(0, startIndex);
        return str;
    }

    public static List<PreAssetsValuationBase> removeUnderLine(List<PreAssetsValuationBase> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setOriginalSubjectCode(list.get(i).getSubjectCode());
            String subjectCode = list.get(i).getSubjectCode();
            if (subjectCode != null) {
                subjectCode = subjectCode.toUpperCase();

                //含有中文不处理
                if (subjectCode.matches(".*[\u4e00-\u9fa5]+.*")) {
                    continue;
                }
                subjectCode = subjectCode.replaceAll("[\\.\\s_-]", "");
            }

            list.get(i).setSubjectCode(subjectCode);
        }

        return list;
    }

    /**
     * 校验该科目是否是资产总值
     *
     * @param str 待校验字符串
     * @return 检验结果
     */
    public static boolean checkMarketValue(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        long count = Arrays.stream(ValuationConst.TOTAL_MARKET_VALUE).filter(p -> p.trim().equals(str)).count();
        return count > 0;
    }

    /**
     * 校验该科目是否是资产份额
     *
     * @param str 待校验字符串
     * @return 检验结果
     */
    public static boolean checkAssetShare(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        long count = Arrays.stream(ValuationConst.ASSET_SHARE_VALUE).filter(p -> p.trim().equals(str)).count();
        return count > 0;
    }

    /**
     * 校验该科目是否是资产净值
     *
     * @param str 待校验字符串
     * @return 检验结果
     */
    public static boolean checkNetAssetsValue(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        long count = Arrays.stream(ValuationConst.NET_ASSET_VALUE).filter(p -> p.trim().equals(str)).count();
        return count > 0;
    }

}
