package com.jeequan.jeepay.core.model.applyment;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.MchApplyment;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Data
public class Lklsb2bpayApplymentInfo extends ApplymentBasicInfo {

    /**
     * 商户注册名称
     **/
    private String merRegName;

    /**
     * 是否为法人
     **/
    private String isLegalInfo;

    /**
     * 商户经营内容
     **/
    private String licenseBusiness;

    /**
     * 银行省市县编码 (包含省市县三级编码)
     **/
    private JSONArray mchAreaCode;

    /**
     * 经度
     **/
    private String latitude;

    /**
     * 纬度
     **/
    private String longtude;

    /**
     * 银行查询地区类型
     **/
    private String selectAreaCode;

    /**
     * 联行行号
     **/
    private String openningBankCode;

    /**
     * 清算行号
     **/
    private String clearingBankCode;

    /**
     * 其他附件
     **/
    private String otherFile;

    /**
     * 协议照片
     **/
    private String agreeMentImg;

    /**
     * 开户许可证照片,
     **/
    private String openingPermitImg;

    public String getCompanyAccountLicenseImg() {
        return openingPermitImg;
    }

    /**
     * 法人授权函照片
     **/
    private String letterOfAuthorizationImg;

    /**
     * 结算方式 D0、D1
     **/
    private String settleType;


    /**
     * 结算账户类型
     **/
    @JSONField(serialize = false)
    public String getSettAccType() {
        if ("B".equalsIgnoreCase(this.settAccountType)) {   // 对公
            return "57";
        } else if ("C".equalsIgnoreCase(this.settAccountType)) { // 对私
            return "58";
        }
        return "";
    }

    /**
     * MCC
     **/
    private String coverMcc() {
        if (StringUtils.isNotEmpty(this.mccCode)) {
            String[] split = this.mccCode.split("_");
            return split[1];
        }
        return null;
    }

    /**
     * 商户类型
     **/
    private String coverMerType() {
        if (MchApplyment.MERCHANT_TYPE_PERSONAL == this.merchantType) {
            return "TP_PERSONAL";
        } else if (MchApplyment.MERCHANT_TYPE_ENTERPRISE == this.merchantType) {
            return "TP_MERCHANT";
        }
        return "";
    }

    /**
     * 日期转换
     **/
    private String convertDateEnd(String dateEndStr) {
        if ("长期".equals(dateEndStr)) {
            return "9999-12-31";
        } else {
            return dateEndStr;
        }
    }

    /**
     * 类型转换：标准格式 ---》 接口自定义格式
     **/
    private Map<String, Object> convertPaywayFeeList() {

        if (this.paywayFeeList == null) {
            return null;
        }

        boolean wxConfig = false;
        boolean aliConfig = false;
        boolean upConfig = false;

        JSONArray result = new JSONArray();
        Map<String, Object> mapResult = new HashMap<>();
        for (PaywayFee paywayFee : this.paywayFeeList) {

            if ((paywayFee.getWayCode().startsWith("UP_") || paywayFee.getWayCode().startsWith("YSF_"))) {

                // 阶梯费率
                if (PaywayFee.FEE_TYPE_LEVEL.equals(paywayFee.getFeeType())) {
                    if (upConfig) continue;

                    // 借记卡费率
                    if (CollUtil.isNotEmpty(paywayFee.getLevelList())) {
                        paywayFee.getLevelList().forEach(item -> {
                            String feeValue = convertRate(item.getFeeRate());
                            if (item.getMaxFee() == 100000L) {
                                // 借记卡 < 1000
                                JSONObject rateItem = new JSONObject();
                                rateItem.put("feeValue", feeValue);
                                rateItem.put("feeCode", "YSF_DISCOUNT_DEBIT_FEE");
                                mapResult.put("YSF_DISCOUNT_DEBIT_FEE", rateItem);

                                rateItem = new JSONObject();
                                rateItem.put("feeValue", feeValue);
                                rateItem.put("feeCode", "YL_DISCOUNT_DEBIT_FEE");
                                mapResult.put("YL_DISCOUNT_DEBIT_FEE", rateItem);
                            } else {
                                // 借记卡 >= 1000
                                JSONObject rateItem = new JSONObject();
                                rateItem.put("feeValue", feeValue);
                                rateItem.put("feeCode", "DEBIT_CARD");
                                rateItem.put("topFee", "20d");
                                mapResult.put("DEBIT_CARD", rateItem);

                                rateItem = new JSONObject();
                                rateItem.put("feeValue", feeValue);
                                rateItem.put("feeCode", "UNIONPAY_WALLET_DEBIT_FEE");
                                rateItem.put("topFee", "20d");
                                mapResult.put("UNIONPAY_WALLET_DEBIT_FEE", rateItem);
                            }
                        });
                    }

                    if (paywayFee.getCreditCardPaywayFee() != null && CollUtil.isNotEmpty(paywayFee.getCreditCardPaywayFee().getLevelList())) {
                        paywayFee.getCreditCardPaywayFee().getLevelList().forEach(item -> {
                            String feeValue = convertRate(item.getFeeRate());
                            if (item.getMaxFee() == 100000L) {
                                // 贷记卡 < 1000
                                JSONObject rateItem = new JSONObject();
                                rateItem.put("feeValue", feeValue);
                                rateItem.put("feeCode", "YSF_DISCOUNT_DEBIT_FEE");
                                mapResult.put("YSF_DISCOUNT_CREDIT_FEE", rateItem);

                                rateItem = new JSONObject();
                                rateItem.put("feeValue", feeValue);
                                rateItem.put("feeCode", "YL_DISCOUNT_CREDIT_FEE");
                                mapResult.put("YL_DISCOUNT_CREDIT_FEE", rateItem);
                            } else {
                                // 贷记卡 >= 1000
                                JSONObject rateItem = new JSONObject();
                                rateItem.put("feeValue", feeValue);
                                rateItem.put("feeCode", "DEBIT_CARD");
                                rateItem.put("topFee", "20d");
                                mapResult.put("DEBIT_CARD", rateItem);

                                rateItem = new JSONObject();
                                rateItem.put("feeValue", feeValue);
                                rateItem.put("feeCode", "UNIONPAY_WALLET_CREDIT_FEE");
                                rateItem.put("topFee", "20d");
                                mapResult.put("UNIONPAY_WALLET_CREDIT_FEE", rateItem);
                            }
                        });
                    }

                    upConfig = true;

                }

            }

            if ((paywayFee.getWayCode().startsWith("WX_"))) {
                if (wxConfig) continue;

                JSONObject json1 = new JSONObject();
                // 微信费率
                json1.put("feeCode", "WECHAT");
                json1.put("feeValue", convertRate(paywayFee.getFeeRate()));
                mapResult.put("WECHAT", json1);

//                JSONObject json2 = new JSONObject();
//                // 微信线上费率
//                json2.put("feeCode", "LINE_WECHAT");
//                json2.put("feeValue", convertRate(paywayFee.getFeeRate()));
//                result.add(json2);

                wxConfig = true;
            }

            if ((paywayFee.getWayCode().startsWith("ALI_"))) {
                if (aliConfig) continue;

                JSONObject json1 = new JSONObject();
                // 支付宝费率
                json1.put("feeCode", "ALIPAY");
                json1.put("feeValue", convertRate(paywayFee.getFeeRate()));
                mapResult.put("ALIPAY", json1);

                aliConfig = true;
            }

            if (CS.PAY_WAY_CODE.D0.equals(paywayFee.getWayCode())) {
                String feeRate = convertRate(paywayFee.getFeeRate());
                // TODO 特殊支付费率 暂固定
                JSONObject scanPaySecond = new JSONObject();
                // 扫码d0费率
                scanPaySecond.put("feeCode", "SCAN_PAY_SECOND");
                scanPaySecond.put("feeValue", feeRate);
                mapResult.put("SCAN_PAY_SECOND", scanPaySecond);

                JSONObject cardSecond = new JSONObject();
                // 刷卡D0费率
                cardSecond.put("feeCode", "CARD_SECOND");
                cardSecond.put("feeValue", feeRate);
                mapResult.put("CARD_SECOND", cardSecond);
            }
        }

        return mapResult;

    }

    private static String convertRate(BigDecimal rate) {
        return rate == null ? null : String.valueOf(rate.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP));
    }

    @Override
    public String bankBranchNo() {
        return openningBankCode;
    }

    @Override
    public String bankAccountName() {
        return super.bankAccountName();
    }
}
