package com.malk.lianan.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.malk.lianan.utils.AesUtil;
import com.malk.lianan.utils.RSASignatureUtil;
import com.malk.lianan.utils.RsaUtil;
import com.malk.lianan.service.LianAnGyPayService;
import com.malk.server.common.McR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/gypay/")
@Slf4j
public class LianAnGyPayController {
    @Autowired
    private LianAnGyPayService lianAnGyPayService;

    private final static String GYPAY_API_AES_KEY="d2a1b3173e494c38";

    private final static String GYPAY_API_PRI_KEY="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKez/gacR9NC0qE4HFnPnsXwhm8pqWG9fuI5nPYztqZ+w7iOyI801uAeoqNfiHPIh4CYKYWosNkwMgzZrjL+g0L+KRmXbIdxh7FFP/FOBtH4QzZ0NCp9SMMYMPdexdFuZVtK+8qDw6EvB3iNFMc4aih62Dwg/hILrp0+t4mU0FXLAgMBAAECgYBcRYmgY36Ve6/eqrvWoojOig+9vCOJNrVIjcCSKlP1+cuAkXdCbCVKPTDpSRgP9Kaxl4dJFk7vwTfYoDDbt/PKFX277V6bdkjMwNclwniilSU+QMEpmu71HeaXBAiRm5TLEflhcxV2Wy5D0VyBLnPWodH1rGjtfiO6V9SbQf8bGQJBANF5RJ+Ie/+dgAYm/L4Mb3f+Pb/yipUuHcqSZlfd/eMJDtEcp7xcvNFRivbl6IcXvncvL1uInkGdriOe3nmqti0CQQDM86LS8PsseuaVdpt7HCKivub0mI/wcvZ8mUVZZ4jFibPNsSKl1+QreGk/e2QrkyOySC5e2lf5HOEGC/lREm7XAkAL5K/BwxUPQVkIgGQvQjN+A6Br6pB1weWNH9zRtlf48QW3V8IqTrWlyNR0Cj+2k+GLkNFGiC48GYDOovGMxOWNAkEAqCDNV4H8ana/pajQz2M7PoqgZ9Kncba9YWVSsI7wciwOZhSDucyrDsYOgNc2S1C/Hcd80q8HMAJyzkUQV2NprQJACwsH8IEEE/kwSesQlb8fa+ugr+tNnFVPj1xqM4kPD5Dj3XP7B8KWUwL4a4/Y1S6BtoqCL4CAfuEHTw7MlUA2NA==";

    @PostMapping("callback")
    public JSONObject callback(@RequestBody String param) throws Exception {
        log.info("callback:{}",param);

        if ("{}".equals(param)){
            return null;
        }

        JSONObject resultjson=JSONObject.parseObject(param);
        JSONObject resultBody = resultjson.getJSONObject("Cmscloud").getJSONObject("Body");
        String resultData= AesUtil.decrypt(resultBody.getString("Data"),GYPAY_API_AES_KEY);
        System.out.println(resultData);
//        Boolean flag= RSASignatureUtil.verifySafe(resultData,resultBody.getString("Sign"),GYPAY_API_SIGN_PUB_KEY);
        boolean flag = RsaUtil.verifySign(resultData.getBytes(), resultBody.getString("Sign"), "cer/ICBC_CZY__SIGN_TEST.cer");
//        System.out.println(flag);
        System.out.println("签名验证结果："+flag);

        Map result = (Map) JSONObject.parse(resultData);

        lianAnGyPayService.callback(result);

        JSONObject responseJson = responseJson(resultjson.getJSONObject("Cmscloud").getJSONObject("Head"));

        return responseJson;
    }

    private JSONObject responseJson(JSONObject head){
        Map result = new HashMap();
        result.put("ResultMsg","成功");
        result.put("ResultCode","0000");

        JSONObject resultJson = new JSONObject();
        resultJson.put("Result",result);

        String encrypt = AesUtil.encrypt(JSONObject.toJSONString(resultJson), GYPAY_API_AES_KEY);

        String signed= RsaUtil.sign(JSONObject.toJSONString(resultJson).getBytes(),GYPAY_API_PRI_KEY);

        JSONObject resultBody = new JSONObject();

        resultBody.put("Data", encrypt);
        resultBody.put("Sign", signed);

        JSONObject Cmscloud = new JSONObject();
        Cmscloud.put("Head", head);
        Cmscloud.put("Body", resultBody);

        JSONObject resultjson = new JSONObject();
        resultjson.put("Cmscloud", Cmscloud);

        log.info("返回响应：{}",resultjson);

        return resultjson;
    }

    @GetMapping("test")
    public McR test(String id){
        log.info("test:{}",id);
        return McR.success();
    }

    //通用付款
    @PostMapping("pay")
    public McR pay(@RequestBody Map map){
        return lianAnGyPayService.pay(map);
    }

    //重试付款
    @PostMapping("retryPay")
    public McR retryPay(@RequestBody Map map){
        return lianAnGyPayService.retryPay(map);
    }

    //工资代发
    @PostMapping("salaryPay")
    public McR salaryPay(@RequestBody Map map){
        return lianAnGyPayService.salaryPay(map);
    }

    //通用报销
    @PostMapping("reimburse")
    public McR reimburse(@RequestBody Map map){
        return lianAnGyPayService.reimburse(map);
    }

}
