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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.MchApplyment;
import com.jeequan.jeepay.core.utils.AmountUtil;
import com.jeequan.jeepay.core.utils.JsonKit;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;

@Data
public class ShengpayApplymentInfo extends ApplymentBasicInfo {

    /** 分账费用费率, 单位： 百分比， 传入的为实际费率值  1% 传入的为 0.01  */
    private BigDecimal divisionFeeRate;

    /** pos信息集合  */
    private String posListStr;

    /** 类型转换：标准格式 ---》 接口自定义格式 **/
    public String convertMerchantType(){

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

        if(this.merchantType == MchApplyment.MERCHANT_TYPE_PERSONAL){
            return "PERSONAL";
        }else if(this.merchantType == MchApplyment.MERCHANT_TYPE_INDIVIDUAL){
            return "INDIVIDUAL";
        }else if(this.merchantType == MchApplyment.MERCHANT_TYPE_ENTERPRISE){
            return "ENTERPRISE";
        }
        return "";
    }

    /** 类型转换：标准格式 ---》 接口自定义格式 **/
    public String convertLicenseEffectEnd(){
        return DATE_FOREVER_VAL.equals(this.licenseEffectEnd) ? "2199-12-31" : this.licenseEffectEnd; // 盛付通 长期需要传入： 2199-12-31
    }

    /** 类型转换：标准格式 ---》 接口自定义格式 **/
    public String convertIdcardEffectEnd(){
        return DATE_FOREVER_VAL.equals(this.idcardEffectEnd) ? "2199-12-31" : this.idcardEffectEnd; // 盛付通 长期需要传入： 2199-12-31
    }

    /** 类型转换：标准格式 ---》 接口自定义格式 **/
    public String convertSettAccountIdcardEffectEnd(){
        return DATE_FOREVER_VAL.equals(this.settAccountIdcardEffectEnd) ? "2199-12-31" : this.settAccountIdcardEffectEnd; // 盛付通 长期需要传入： 2199-12-31
    }


    /** 类型转换：标准格式 ---》 接口自定义格式 **/
    public String convertPaywayFeeList2String(){

        // 签约产品不存在 && 不包含分账产品
        if(this.paywayFeeList == null && (this.getIsDivision() == null || this.getIsDivision() != CS.YES) ){
            return null;
        }

        JSONArray result = new JSONArray();

        // pos产品的费率信息
        PaywayFee posPaywayFee = null;

        // 签约收单产品 集合
        if(this.paywayFeeList != null){

            for (PaywayFee paywayFee : this.paywayFeeList) {

                if(CS.PAY_WAY_CODE.AUTO_POS.equals(paywayFee.getWayCode())){
                    posPaywayFee = paywayFee;
                }

                JSONObject json = new JSONObject();

                // 第一步： 转换产品ID ( 本系统 --》 盛付通 )
                String bizProductCode = null;
                switch (paywayFee.getWayCode()){
                    case CS.PAY_WAY_CODE.WX_BAR: bizProductCode = "OFFLINE_WX_SCAN"; break;
                    case CS.PAY_WAY_CODE.WX_JSAPI: bizProductCode = "OFFLINE_WX_JSAPI"; break;
                    case CS.PAY_WAY_CODE.WX_H5: bizProductCode = null; break;
                    case CS.PAY_WAY_CODE.WX_LITE: bizProductCode = "OFFLINE_WX_LITE"; break;
                    case CS.PAY_WAY_CODE.WX_NATIVE: bizProductCode = null; break;
                    case CS.PAY_WAY_CODE.ALI_BAR: bizProductCode = "OFFLINE_ALIPAY_SCAN"; break;
                    case CS.PAY_WAY_CODE.ALI_JSAPI: bizProductCode = "OFFLINE_ALIPAY_JSAPI"; break;
                    case CS.PAY_WAY_CODE.ALI_LITE: bizProductCode = "OFFLINE_ALIPAY_LITE"; break;
                    case CS.PAY_WAY_CODE.ALI_PC: bizProductCode = ""; break;
                    case CS.PAY_WAY_CODE.ALI_QR: bizProductCode = "OFFLINE_ALIPAY_QR"; break;
                    case CS.PAY_WAY_CODE.ALI_WAP: bizProductCode = ""; break;
                    case CS.PAY_WAY_CODE.UP_BAR: bizProductCode = "OFFLINE_UNIONPAY_SCAN_D"; break; //银联条码借记卡支付 (付款码  用户被扫)
                    case CS.PAY_WAY_CODE.UP_QR: bizProductCode = "OFFLINE_UNIONPAY_QR_D"; break; //银联扫码借记卡支付 (二维码  用户主扫)
                    case CS.PAY_WAY_CODE.UP_JSAPI: bizProductCode = "OFFLINE_UNIONPAY_QUICK_D"; break; //银联行业码借记卡支付 (jsapi)

                    case CS.PAY_WAY_CODE.YSF_BAR: bizProductCode = "OFFLINE_UNIONPAY_SCAN_D"; break; //银联条码借记卡支付 (付款码  用户被扫)
                    case CS.PAY_WAY_CODE.YSF_JSAPI: bizProductCode = "OFFLINE_UNIONPAY_QUICK_D"; break; //银联行业码借记卡支付 (jsapi)

                }

                // 盛付通不存在该产品
                if(StringUtils.isEmpty(bizProductCode)){
                    continue;
                }

                // 产品编码
                json.put("bizProductCode", bizProductCode);

                // 单笔费率
                if(PaywayFee.FEE_TYPE_SINGLE.equals(paywayFee.getFeeType())){
                    json.put("feeType", "SINGLE_FEE");
                    JSONObject feeInfo = JsonKit.newJson("feeValue", convertRate(paywayFee.getFeeRate()));
                    json.put("feeInfos", Arrays.asList(feeInfo));
                    result.add(json);
                    continue;
                }

                // 以下为阶梯费率
                json.put("feeType", "STAIR_FEE");
                json.put("minFee", convertFee(paywayFee.getMinFee()));
                json.put("maxFee", convertFee(paywayFee.getMaxFee()));
                JSONArray feeInfos = new JSONArray();

                if(paywayFee.getLevelList() != null){
                    for (PaywayFee feeItem : paywayFee.getLevelList()) {
                        JSONObject feeInfo = new JSONObject();
                        feeInfo.put("feeValue",convertRate(feeItem.getFeeRate()));
                        feeInfo.put("minValue",convertFee(feeItem.getMinFee()));
                        feeInfo.put("maxValue",convertFee(feeItem.getMaxFee()));
                        feeInfos.add(feeInfo);
                    }
                }

                json.put("feeInfos", feeInfos);

                result.add(json);

                // 设置银联贷记卡相关参数
                if("OFFLINE_UNIONPAY_SCAN_D".equals(bizProductCode) || "OFFLINE_UNIONPAY_QR_D".equals(bizProductCode) || "OFFLINE_UNIONPAY_QUICK_D".equals(bizProductCode)){
                    if(paywayFee.getCreditCardPaywayFee() != null){
                        JSONObject jsonByCredit = new JSONObject();
                        // 产品编码
                        if("OFFLINE_UNIONPAY_SCAN_D".equals(bizProductCode)) jsonByCredit.put("bizProductCode", "OFFLINE_UNIONPAY_SCAN_C");
                        if("OFFLINE_UNIONPAY_QR_D".equals(bizProductCode)) jsonByCredit.put("bizProductCode", "OFFLINE_UNIONPAY_QR_C");
                        if("OFFLINE_UNIONPAY_QUICK_D".equals(bizProductCode)) jsonByCredit.put("bizProductCode", "OFFLINE_UNIONPAY_QUICK_C");

                        jsonByCredit.put("feeType", "STAIR_FEE");
                        jsonByCredit.put("minFee", convertFee(paywayFee.getCreditCardPaywayFee().getMinFee()));
                        jsonByCredit.put("maxFee", convertFee(paywayFee.getCreditCardPaywayFee().getMaxFee()));
                        JSONArray feeInfosByCredit = new JSONArray();

                        if(paywayFee.getCreditCardPaywayFee().getLevelList() != null){
                            for (PaywayFee feeItem : paywayFee.getCreditCardPaywayFee().getLevelList()) {
                                JSONObject feeInfo = new JSONObject();
                                feeInfo.put("feeValue",convertRate(feeItem.getFeeRate()));
                                feeInfo.put("minValue",convertFee(feeItem.getMinFee()));
                                feeInfo.put("maxValue",convertFee(feeItem.getMaxFee()));
                                feeInfosByCredit.add(feeInfo);
                            }
                        }

                        jsonByCredit.put("feeInfos", feeInfosByCredit);
                        result.add(jsonByCredit);
                    }
                }

            }
        }

        // 分账
        if(this.getIsDivision() != null && this.getIsDivision() == CS.YES){

            JSONObject json = new JSONObject();
            json.put("bizProductCode", "SHARING");
            json.put("feeType", "SINGLE_FEE"); // SINGLE_FEE单笔费率，0.0075，千分之7.5  ;  SINGLE_QUOTA单笔定额，0.01，一笔固定收取0.01元
            json.put("feeInfos", Arrays.asList(JsonKit.newJson("feeValue", this.divisionFeeRate)));
            result.add(json);
        }


        // pos产品
        if(posPaywayFee != null){

            // --------  POS银联借记卡支付  --------
            JSONObject posDebitCard = new JSONObject();
            posDebitCard.put("bizProductCode", "POS_DEBIT_CARD");
            posDebitCard.put("feeType", "STAIR_FEE");

            JSONArray feeInfos = new JSONArray();
            if(posPaywayFee.getLevelList() != null){
                for (PaywayFee feeItem : posPaywayFee.getLevelList()) {
                    JSONObject feeInfo = new JSONObject();
                    feeInfo.put("feeValue",convertRate(feeItem.getFeeRate()));
                    feeInfo.put("minValue",convertFee(feeItem.getMinFee()));
                    feeInfo.put("maxValue",convertFee(feeItem.getMaxFee()));
                    feeInfos.add(feeInfo);
                }
            }
            posDebitCard.put("feeInfos", feeInfos);
            posDebitCard.put("minFee", convertFee(posPaywayFee.getMinFee()));
            posDebitCard.put("maxFee", convertFee(posPaywayFee.getMaxFee()));
            result.add(posDebitCard);

            // --------  POS银联借记卡支付  --------


            // --------  POS银联借记卡支付优惠  --------
            JSONObject posDebitCardDis = new JSONObject();
            posDebitCardDis.put("bizProductCode", "POS_DEBIT_CARD_DIS");
            posDebitCardDis.put("feeType", "SINGLE_FEE");
            posDebitCardDis.put("feeInfos", Arrays.asList(JsonKit.newJson("feeValue", convertRate(posPaywayFee.getLevelList().get(0).getFeeRate()))));
            result.add(posDebitCardDis);

            // --------  POS银联借记卡支付优惠  --------

            // --------  POS银联贷记卡支付  --------
            JSONObject posCreditCard = new JSONObject();
            posCreditCard.put("bizProductCode", "POS_CREDIT_CARD");
            posCreditCard.put("feeType", "SINGLE_FEE");
            posCreditCard.put("feeInfos", Arrays.asList(JsonKit.newJson("feeValue", convertRate(posPaywayFee.getCreditCardPaywayFee().getLevelList().get(0).getFeeRate()))));
            result.add(posCreditCard);

            // --------  POS银联贷记卡支付  --------

            // --------  POS银联贷记卡支付优惠  --------
            JSONObject posCreditCardDis = new JSONObject();
            posCreditCardDis.put("bizProductCode", "POS_CREDIT_CARD_DIS");
            posCreditCardDis.put("feeType", "SINGLE_FEE");
            posCreditCardDis.put("feeInfos", Arrays.asList(JsonKit.newJson("feeValue", convertRate(posPaywayFee.getCreditCardPaywayFee().getLevelList().get(0).getFeeRate()))));
            result.add(posCreditCardDis);

            // --------  POS银联贷记卡支付优惠  --------
        }

        return result.toJSONString();

    }


    private BigDecimal convertRate(BigDecimal rate){
        return rate == null ? null : rate.setScale(4, BigDecimal.ROUND_HALF_UP);
    }

    private BigDecimal convertFee(Long fee){

        if(fee == null){
            return null;
        }

        return new BigDecimal(AmountUtil.convertCent2Dollar(fee)).setScale(2, BigDecimal.ROUND_HALF_UP);
    }


    public String convertTerminalInfos(){

        if(StringUtils.isEmpty(this.getPosListStr())){
            return null;
        }

        JSONArray result = new JSONArray();

        JSONArray jsonArray = JSONArray.parseArray(this.getPosListStr());
        for (Object o : jsonArray) {
            JSONObject item = (JSONObject) o;

            JSONObject itemNew = new JSONObject();
            itemNew.put("ticketName", item.getString("ticketName"));
            itemNew.put("terminalCount", item.getIntValue("terminalCount"));
            itemNew.put("terminalCityId", item.getJSONArray("terminalCityId").get(1).toString());  //城市编号
            itemNew.put("terminalDetailAddress", item.getString("terminalDetailAddress"));
            result.add(itemNew);
        }

        return result.toJSONString();
    }

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