package com.crm.ucard.controller;

import com.alibaba.fastjson.JSON;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.ucard.*;
import com.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.config.UcardConfig;
import com.crm.ucard.service.SysConfigService;
import com.crm.ucard.service.UcardService;
import com.crm.ucard.util.PayouCardUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/ucard/manager/callback")
public class UcardNotifyController {

    @Autowired
    private SysConfigService sysConfigService;

   @Autowired
    private UcardService ucardService;

    @PostMapping("/notify")
    @FeignClientAnnotation
    public BaseResultDto notify(@RequestBody UcardBaseRequest request) throws Exception {
        log.info("Ucard回调通知开始："+JSON.toJSONString(request));

        UcardConfig ucardConfig = getUcardConfig();
        // 验证签名
        if (!PayouCardUtil.verifySignature(ucardConfig, JSON.toJSONString(request))) {
            return BaseResultDto.error("签名验证失败");
        }

        switch (request.getNotifyType()){
            case 1:
                handleGlobalTransferNotify(request,ucardConfig);
                break;
            case 2:
                handleGlobalTransferSubmitNotify(request,ucardConfig);
                break;
            case 3:
                handleKycNotify(request,ucardConfig);
                break;
            case 4:
                handleCardRecharge(request,ucardConfig);
                break;
            case 5:
                handleCardActivate(request);
                break;
            case 6:
                handleCardFreeze(request);
                break;
            case 7:
                handleVerify3DS(request);
                break;
            case 8:
                handCardBillStatement(request);
                break;
            default:
                return BaseResultDto.error();
        }

        log.info("Ucard回调通知结束");
        return BaseResultDto.result(0);
    }

    public UcardConfig getUcardConfig() throws ServiceException {
        SysConfigTable sysConfigTable = sysConfigService.getByCode(Constants.UCARD_CARD);
        if (sysConfigTable == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        UcardConfig ucardConfig = JSON.parseObject(sysConfigTable.getValue(), UcardConfig.class);
        return ucardConfig;
    }

    /**
     * 全球速汇-支付结果回调通知
     */
    public void handleGlobalTransferNotify(@RequestBody UcardBaseRequest request,UcardConfig ucardConfig) throws ServiceException {
        try {
            GlobalTransferNotifyDto notify = JSON.parseObject(JSON.toJSONString(request.getData()), GlobalTransferNotifyDto.class);

            ucardService.handleGlobalTransferNotify(notify,ucardConfig);
        } catch (Exception e) {
            log.error("全球速汇-支付结果回调失败", e);
        }
    }

    /**
     * 全球速汇-调单回调通知
     */
    public void handleGlobalTransferSubmitNotify(UcardBaseRequest request,UcardConfig ucardConfig) throws ServiceException {
        try {
            GlobalTransferSubmitNotifyDto notify = JSON.parseObject(JSON.toJSONString(request.getData()), GlobalTransferSubmitNotifyDto.class);

            ucardService.handleGlobalTransferSubmitNotify(notify,ucardConfig);
        } catch (Exception e) {
            log.error("全球速汇-调单回调失败", e);
        }
    }

    /**
     * 用户KYC回调通知
     */
    public void handleKycNotify(UcardBaseRequest request,UcardConfig ucardConfig) throws ServiceException {
        try {
            KycNotifyDto notify = JSON.parseObject(JSON.toJSONString(request.getData()), KycNotifyDto.class);

            ucardService.handleKycNotify(notify,ucardConfig);
        } catch (Exception e) {
            log.error("用户KYC回调失败", e);
        }
    }

    /**
     *  银行卡-卡片充值结果回调通知
     */
    public void handleCardRecharge(UcardBaseRequest request, UcardConfig ucardConfig) throws ServiceException {
        try {
            CardRechargeNotifyDto notify = JSON.parseObject(JSON.toJSONString(request.getData()), CardRechargeNotifyDto.class);

            ucardService.handleCardRecharge(notify,ucardConfig);
        } catch (Exception e) {
            log.error("卡片充值结果回调失败", e);
        }
    }

    /**
     * 银行卡-卡片激活结果回调通知
     */
    public void handleCardActivate(UcardBaseRequest request) throws ServiceException {
        try {
            // 处理充值回调
            CardActivateNotifyDto notify = JSON.parseObject(JSON.toJSONString(request.getData()), CardActivateNotifyDto.class);

            ucardService.handleCardActivate(notify);
        } catch (Exception e) {
            log.error("卡片激活结果回调失败", e);
        }
    }

    /**
     * 银行卡-卡片冻结、解冻、重发PIN处理状态回调通知
     */
    public void handleCardFreeze(UcardBaseRequest request) throws ServiceException {
        try {
            CardFreezeNotifyDto notify = JSON.parseObject(JSON.toJSONString(request.getData()), CardFreezeNotifyDto.class);

            ucardService.handleCardFreeze(notify);
        } catch (Exception e) {
            log.error("卡片冻结、解冻、重发PIN处理结果回调失败", e);
        }
    }

    /**
     * 银行卡-3DS校验
     */
    public void handleVerify3DS(UcardBaseRequest request) throws ServiceException {
        try {
            CardVerify3DSNotifyDto notify = JSON.parseObject(JSON.toJSONString(request.getData()), CardVerify3DSNotifyDto.class);

            ucardService.handleVerify3DS(notify);
        } catch (Exception e) {
            log.error("3DS校验处理结果回调失败", e);
        }
    }

    /**
     * 银行卡-消费账单事件
     */
    public void handCardBillStatement(UcardBaseRequest request) throws ServiceException {
        try {
            CardTransacOrderNotifyDto notify = JSON.parseObject(JSON.toJSONString(request.getData()), CardTransacOrderNotifyDto.class);

            ucardService.handCardBillStatement(notify);
        } catch (Exception e) {
            log.error("消费账单事件处理结果回调失败", e);
        }
    }

}
