package com.crm.ucard.controller;

import com.alibaba.fastjson.JSON;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.wasabi.*;
import com.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.config.WasabiConfig;
import com.crm.ucard.service.SysConfigService;
import com.crm.ucard.service.WasabiCardService;
import com.crm.ucard.util.RsaUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/wasabi")
public class WasabiCardNotifyController {

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private WasabiCardService wasabiCardService;

    @PostMapping("/callback")
    public WasabiNotifyResultDto notify(@RequestHeader("x-wsb-category") String category,
                                @RequestHeader("x-wsb-signature") String signature,
                                @RequestHeader("x-wsb-request-id") String requestId,
                                @RequestBody String request) throws Exception {
        try {
            log.info("wasabi回调通知开始：" + JSON.toJSONString(request));
            WasabiConfig config = getWasabiConfig();
            // 验证签名
            if (!RsaUtils.verify(request, config.getWsbPublicKey(), signature)) {
                return WasabiNotifyResultDto.error();
            }
            switch (category) {
                case "card_transaction":
                    WasabiTransactionDto transactionNotifyDto = JSON.parseObject(request, WasabiTransactionDto.class);
                    wasabiCardService.cardTransactionNotify(transactionNotifyDto);
                    break;
                case "card_auth_transaction":
                    WasabiAuthTransactionDto authTransactionNotifyDto = JSON.parseObject(request, WasabiAuthTransactionDto.class);
                    wasabiCardService.cardAuthTransactionNotify(authTransactionNotifyDto);
                    break;
                case "card_fee_patch":
                    WasabiFeePatchNotifyDto feePatchNotifyDto = JSON.parseObject(request, WasabiFeePatchNotifyDto.class);
                    wasabiCardService.cardFeePatchNotify(feePatchNotifyDto);
                    break;
                case "card_3ds":
                    WasabiThird3dsTransactionDto card3dsNotifyDto = JSON.parseObject(request, WasabiThird3dsTransactionDto.class);
                    wasabiCardService.card3dsNotify(card3dsNotifyDto);
                    break;
                case "card_holder":
                    WasabiHolderQueryDto holderNotifyDto = JSON.parseObject(request, WasabiHolderQueryDto.class);
                    wasabiCardService.cardHolderNotify(holderNotifyDto);
                    break;
                case "physical_card":
                    WasabiPhysicalCardNotifyDto physicalCardNotifyDto = JSON.parseObject(request, WasabiPhysicalCardNotifyDto.class);
                    wasabiCardService.physicalCardNotify(physicalCardNotifyDto);
                    break;
                case "work":
                    break;
                case "gt_transaction":
                    WasabiTransactionTransferDto transferDto = JSON.parseObject(request, WasabiTransactionTransferDto.class);
                    wasabiCardService.expressRemittanceTransactionNotify(transferDto);
                    break;
                default:
                    return WasabiNotifyResultDto.error();
            }
            log.info("wasabi回调通知结束");
            return WasabiNotifyResultDto.success();
        } catch (Exception e) {
            log.error("wasabi回调通知处理失败",e);
            return WasabiNotifyResultDto.error();
        }
    }

    public WasabiConfig getWasabiConfig() throws ServiceException {
        SysConfigTable sysConfigTable = sysConfigService.getByCode(Constants.WASABI_CARD);
        if (sysConfigTable == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        WasabiConfig wasabiConfig = JSON.parseObject(sysConfigTable.getValue(), WasabiConfig.class);
        return wasabiConfig;
    }

}
