package com.brilliance.mda.runtime.mda.util;

import com.brilliance.mda.runtime.mda.IStream;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.ibatis.annotations.Case;

import java.util.Locale;

import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;

public class Systems {

    private static Locale locale = Locale.SIMPLIFIED_CHINESE;

    public static String getLang() {
        return "EN";
    }


    public static Locale getLocale() {
        if (locale == null)
            locale = Locale.SIMPLIFIED_CHINESE;
        return locale;
    }

    public static String macGen(String text, int startval) {
        String data = startval + text;
        String s = DigestUtils.sha1Hex(DigestUtils.md5(data)).substring(0, 8);
        return s;
    }

    public static String macGen(IStream str, int startval) {

        String data = str.getRows().stream().reduce("", (acc, item) -> acc + item);
        return macGen(data, startval);
    }

    public static String moneycnsmalltobig(String number) {
        //金额小写转换为中文大写
        //$number = str( number )
        //if the number is empty,return the null
        if (MdaUtils.isEmpty(number)) {
            return ("");
        }
        String vnumber = number;
        String l1 = "";
        String l2 = "";
        String l3 = "";
        String l4 = "";
        String rtn = "";
        String up = NULLSTR;
        String dn = NULLSTR;
        if ((Strings.pos(vnumber, ".") > 0)) {
            up = Strings.mid(vnumber, 1, Strings.pos(vnumber, ".") - 1);
            dn = Strings.mid(vnumber, Strings.pos(vnumber, ".") + 1, Strings.len(vnumber) - Strings.pos(vnumber, "."));
        } else {
            up = vnumber;
            // add by wwj for error handled
            dn = "";
        }
        if ((Strings.len(up) > 16)) {
            return MdaUtils.getI18NString("sysmod", "CT000565");
        }
        for (int i = 0; i <= 3; i++) {
            if ((Strings.len(up) - i > 0)) {
                l1 = Strings.mid(vnumber, Strings.len(up) - i, 1) + l1;
            }
            if ((Strings.len(up) - 4 - i > 0)) {
                l2 = Strings.mid(vnumber, Strings.len(up) - 4 - i, 1) + l2;
            }
            if ((Strings.len(up) - 8 - i > 0)) {
                l3 = Strings.mid(vnumber, Strings.len(up) - 8 - i, 1) + l3;
            }
            if ((Strings.len(up) - 12 - i > 0)) {
                l4 = Strings.mid(vnumber, Strings.len(up) - 12 - i, 1) + l4;
            }
        }
        if (!MdaUtils.isEmpty(l4)) {
            if ((MdaUtils.compareTo(ntOC(l4), "") != 0) && (MdaUtils.compareTo(ntOC(l4), MdaUtils.getI18NString("sysmod", "CT000566")) != 0)) {
                rtn = rtn + ntOC(l4) + "兆";
            }
        }
        if (!MdaUtils.isEmpty(l3)) {
            if ((MdaUtils.compareTo(ntOC(l3), "") != 0) && (MdaUtils.compareTo(ntOC(l3), MdaUtils.getI18NString("sysmod", "CT000567")) != 0)) {
                rtn = rtn + ntOC(l3) + "亿";
            }
        }
        if (!MdaUtils.isEmpty(l2)) {
            if ((MdaUtils.compareTo(ntOC(l2), "") != 0) && (MdaUtils.compareTo(ntOC(l2), MdaUtils.getI18NString("sysmod", "CT000568")) != 0)) {
                rtn = rtn + ntOC(l2) + "万";
            }
        }
        if (!MdaUtils.isEmpty(l1)) {
            if ((MdaUtils.compareTo(ntOC(l1), "") != 0) && (MdaUtils.compareTo(ntOC(l1), MdaUtils.getI18NString("sysmod", "CT000569")) != 0)) {
                rtn = rtn + ntOC(l1);
            }
        }
        //if not IsEmpty( Val( $dn ) ) then
        //why use the function Val,if the variable $dn is null,error display
        int num = 0;
        if (!MdaUtils.isEmpty(dn)) {
            num = Strings.parseInt(dn);
        } else {
            num = 0;
        }
        if (num != 0) {
            if (!MdaUtils.isEmpty(rtn)) {
                rtn = rtn + "元";
            }
            if ((MdaUtils.compareTo(dtOC(up, dn), "整") == 0)) {
                rtn = "零元整";
            } else {
                rtn = rtn + dtOC(up, dn);
            }
        } else {
            if (!MdaUtils.isEmpty(rtn)) {
                rtn = rtn + "元整";
            } else {
                rtn = "零元整";
            }
        }
        return rtn;
    }

    public static String moneycnsmalltobig(String number, String cur) {
        String rtn = moneycnsmalltobig(number);
        // 其他币种的大写转化
        if (null != cur && !"CNY".equals(cur)) {
            //  rtn = rtn.replace("零角", "零").replace("角", "拾");
            rtn = rtn.replace("零角", "零");
        }
        if (rtn.contains("角") && !rtn.contains("分")) {
            rtn = rtn + "整";
        }
        return rtn;
    }


    public static String ntOC(String number) {
        // 整数部分转成汉字
        boolean error = false;
        if ((Strings.len(number) > 4)) {
            error = true;
        }
        String rtn = "";
        boolean iszero = true;
        int len = Strings.len(number);
        for (int i = len + 1; i <= 4; i++) {
            number = "0" + number;
        }
        //ISZERO为真则说明循环的数字若是0则可以在返回串中写零否则不能加零
        String a1 = NULLSTR;
        for (int i = 4 - len + 1; i <= 4; i++) {
            a1 = Strings.mid(number, i, 1);
            switch (a1) {
                case "1":
                    iszero = true;
                    rtn = rtn + "壹";
                    break;
                case "2":
                    iszero = true;
                    rtn = rtn + "贰";
                    break;
                case "3":
                    iszero = true;
                    rtn = rtn + "叁";
                    break;
                case "4":
                    iszero = true;
                    rtn = rtn + "肆";
                    break;
                case "5":
                    iszero = true;
                    rtn = rtn + "伍";
                    break;
                case "6":
                    iszero = true;
                    rtn = rtn + "陆";
                    break;
                case "7":
                    iszero = true;
                    rtn = rtn + "柒";
                    break;
                case "8":
                    iszero = true;
                    rtn = rtn + "捌";
                    break;
                case "9":
                    iszero = true;
                    rtn = rtn + "玖";
                    break;
                case "0":
                    if ((iszero)) {
                        rtn = rtn + "零";
                        iszero = false;
                    }
                    break;
                default:
                    error = true;
            }
            if (iszero) {
                switch (i) {
                    case 1:
                        rtn = rtn + "仟";
                        break;
                    case 2:
                        rtn = rtn + "佰";
                        break;
                    case 3:
                        rtn = rtn + "拾";
                        break;
                }
            }
        }
        if ((MdaUtils.compareTo(Strings.mid(rtn, Strings.len(rtn) - 1, 2), "零") == 0)) {
            rtn = Strings.mid(rtn, 0, Strings.len(rtn) - 2);
        }
        if ((error)) {
            return MdaUtils.getI18NString("sysmod", "CT000570");
        }
        return rtn;

    }

    public static String dtOC(String up, String number) {
        // 小数部分转成汉字
        int n3 = 0;
        //新国结保留两位小数，不需要四舍五入
        /*if (Strings.parseInt(Strings.mid(number, 3, 1)) > 4) {
            n3 = 1;
        } else {
            n3 = 0;
        }*/
        String num = Strings.mid(number, 1, 1) + MdaUtils.toString(Strings.parseInt(Strings.mid(number, 2, 1)) + n3);
        String rtn = "";
        boolean iszero = true;
        int len = Strings.len(num);
        for (int i = len + 1; i <= 2; i++) {
            num = "0" + num;
        }
        if ((MdaUtils.compareTo(up, "0") == 0) && MdaUtils.compareTo(Strings.mid(num, 1, 1), "0") == 0) {
            iszero = false;
        }
        String a1 = NULLSTR;
        boolean error = false;
        for (int i = 2 - len + 1; i <= 2; i++) {
            a1 = Strings.mid(num, i, 1);
            switch (a1) {
                case "1":
                    iszero = true;
                    rtn = rtn + "壹";
                    break;
                case "2":
                    iszero = true;
                    rtn = rtn + "贰";
                    break;
                case "3":
                    iszero = true;
                    rtn = rtn + "叁";
                    break;
                case "4":
                    iszero = true;
                    rtn = rtn + "肆";
                    break;
                case "5":
                    iszero = true;
                    rtn = rtn + "伍";
                    break;
                case "6":
                    iszero = true;
                    rtn = rtn + "陆";
                    break;
                case "7":
                    iszero = true;
                    rtn = rtn + "柒";
                    break;
                case "8":
                    iszero = true;
                    rtn = rtn + "捌";
                    break;
                case "9":
                    iszero = true;
                    rtn = rtn + "玖";
                    break;
                case "0":
                    if ((iszero)) {
                        rtn = rtn + "零";
                        iszero = false;
                    }
                    break;
                default:
                    error = true;
            }
            if (iszero) {
                switch (i) {
                    case 1:
                        rtn = rtn + "角";
                        break;
                    case 2:
                        rtn = rtn + "分";
                        break;
                }
            }
        }
        if ((MdaUtils.compareTo(Strings.mid(up, Strings.len(up), 1), "0") == 0) && Strings.len(up) != 1 && MdaUtils.isEmpty(rtn)) {
            rtn = "零" + rtn;
        }
        if ((MdaUtils.compareTo(Strings.mid(rtn, Strings.len(rtn) - 1, 2), "零") == 0)) {
            rtn = Strings.mid(rtn, 0, Strings.len(rtn) - 2);
        }
        return rtn;

    }

    public static String getCurName(String cur) {
        String curName = "元";
        switch (cur) {
            case "AUD":
                curName = "澳元";
                break;
            case "CAD":
                curName = "加元";
                break;
            case "CHF":
                curName = "法郎";
                break;
            case "DKK":
                curName = "丹麦克郎";
                break;
            case "EUR":
                curName = "欧元";
                break;
            case "FIM":
                curName = "马克";
                break;
            case "FRF":
                curName = "法郎";
                break;
            case "GBP":
                curName = "英镑";
                break;
            case "HKD":
                curName = "港币";
                break;
            case "JPY":
                curName = "日元";
                break;
            case "KRW":
                curName = "韩元";
                break;
            case "MOP":
                curName = "澳门元";
                break;
            case "NOK":
                curName = "挪威克郎";
                break;
            case "NZD":
                curName = "新西兰元";
                break;
            case "SEK":
                curName = "瑞典克郎";
                break;
            case "SGD":
                curName = "新加坡元";
                break;
            case "TWD":
                curName = "新台元";
                break;
            case "USD":
                curName = "美元";
                break;
            default:
        }
        return curName;
    }

}
