package com.kmall.api.api.pingan.mpconfig;

import com.kmall.api.annotation.LoginUser;
import com.kmall.api.entity.UserVo;
import com.kmall.manager.manager.pay.wxpay.WxPayPropertiesBuilder;
import com.kmall.api.util.ApiBaseAction;
import com.kmall.manager.manager.pingan.properties.PinganPayPropertiesBuilder;
import com.kmall.manager.manager.pingan.utils.InterfaceParams;
import com.kmall.manager.manager.pingan.utils.TLinx2Util;
import com.kmall.manager.manager.pingan.utils.TLinxAESCoder;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("api/pay/pingan/mpconfig")
public class ApiPinganMpconfigController extends ApiBaseAction {

    @RequestMapping("index")
    public Object index(@LoginUser UserVo loginUser) {
        return toResponsSuccess("");
    }

    /**
     * Unicode转 汉字字符串
     * @param str
     * @return
     */
    private static String unicodeToString(String str) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            String group = matcher.group(2);
            ch = (char) Integer.parseInt(group, 16);
            String group1 = matcher.group(1);
            str = str.replace(group1, ch + "");
        }
        return str;
    }

    /**
     * 平安商户支付配置
     * @return
     */
    @GetMapping("add")
    public Object add() {
        String timestamp = new Date().getTime() / 1000 + "";    // 时间

        try {
            // 固定参数
            TreeMap<String, String> postmap = new TreeMap<String, String>();    // 请求参数的map

            postmap.put("open_id", PinganPayPropertiesBuilder.instance().getPayOpenId());
            postmap.put("timestamp", timestamp);

            TreeMap<String, String> datamap = new TreeMap<>();
            datamap.put("pmt_tag", "WeixinOL");
            datamap.put("sub_appid", WxPayPropertiesBuilder.instance().getAppId());

            //数据加密，加签
            TLinx2Util.handleEncrypt(datamap, postmap);
            TLinx2Util.handleSign(postmap);

            //请求
            String rspStr = TLinx2Util.handlePost(postmap, InterfaceParams.MPCONFIGADD);
            System.out.println("返回字符串=" + rspStr);

            JSONObject respObject = JSONObject.fromObject(rspStr);

            Object dataStr = respObject.get("data");
            System.out.println("返回data字符串="+dataStr);

            if (!rspStr.isEmpty() || (dataStr != null)) {
                if (TLinx2Util.verifySign(respObject)) {    // 验签成功
                    String respData = TLinxAESCoder.decrypt(dataStr.toString(), PinganPayPropertiesBuilder.instance().getPayOpenKey());
                    System.out.println("==================响应data内容:" + respData);
                    return toResponsSuccess("商户支付配置查询成功");
                } else {
                    System.out.println("=====验签失败=====");
                }
            } else {
                System.out.println("=====没有返回data数据=====");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return toResponsFail("商户支付配置失败");
    }

    /**
     * 平安商户支付配置
     * @return
     */
    @GetMapping("query")
    public Object query() {
        String timestamp = new Date().getTime() / 1000 + "";    // 时间

        try {
            // 固定参数
            TreeMap<String, String> postmap = new TreeMap<String, String>();    // 请求参数的map

            postmap.put("open_id", PinganPayPropertiesBuilder.instance().getPayOpenId());
            postmap.put("timestamp", timestamp);

            TreeMap<String, String> datamap = new TreeMap<>();
            datamap.put("pmt_tag", "WeixinOL");

            //数据加密，加签
            TLinx2Util.handleEncrypt(datamap, postmap);
            TLinx2Util.handleSign(postmap);

            //请求
            String rspStr = TLinx2Util.handlePost(postmap, InterfaceParams.MPCONFIGQUERY);
            System.out.println("返回字符串=" + rspStr);

            JSONObject respObject = JSONObject.fromObject(rspStr);

            Object dataStr = respObject.get("data");
            System.out.println("返回data字符串="+dataStr);

            if (!rspStr.isEmpty() || (dataStr != null)) {
                if (TLinx2Util.verifySign(respObject)) {    // 验签成功
                    String respData = TLinxAESCoder.decrypt(dataStr.toString(), PinganPayPropertiesBuilder.instance().getPayOpenKey());
                    System.out.println("==================响应data内容:" + respData);
                    return toResponsSuccess("商户支付配置查询成功");
                } else {
                    System.out.println("=====验签失败=====");
                }
            } else {
                System.out.println("=====没有返回data数据=====");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return toResponsFail("商户支付配置查询失败");
    }

}
