package com.izouma.nineth.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson15.JSONObject;
import com.izouma.nineth.config.GeneralProperties;
import com.izouma.nineth.enums.PayMethod;
import com.izouma.nineth.event.OrderNotifyEvent;
import com.izouma.nineth.service.AuctionOrderService;
import com.izouma.nineth.service.AuctionOrderService;
import com.izouma.nineth.service.GiftOrderService;
import com.izouma.nineth.service.MintOrderService;
import com.izouma.nineth.service.UserBalanceService;
import com.upay.sdk.CipherWrapper;
import com.upay.sdk.onlinepay.executer.OnlinePayOrderExecuter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/payease")
@Slf4j
@AllArgsConstructor
public class PayEaseController {
    private final GeneralProperties generalProperties;
    private final RocketMQTemplate  rocketMQTemplate;

    @PostMapping("/notify/{type}/{id}")
    public String notify(@PathVariable String type, @PathVariable Long id, @RequestHeader String encryptKey,
                         @RequestHeader String merchantId, @RequestBody JSONObject rawData) {
        rawData.put("encryptKey", encryptKey);
        rawData.put("merchantId", merchantId);
        JSONObject data = CipherWrapper.bothDecryptWrap(rawData);
        log.info("payEase回调 type={}, orderId={}, 参数={}", type, id, JSON.toJSONString(data, true));
        OnlinePayOrderExecuter executor = new OnlinePayOrderExecuter();
        executor.bothVerifyHmacOrder(data);

        String status = data.getString("status");
        if ("SUCCESS".equals(status)) {
            String serialNumber = data.getString("serialNumber");
            switch (type) {
                case "order":
                    rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                            new OrderNotifyEvent(id, PayMethod.PAYEASE, serialNumber, System.currentTimeMillis()));
                    break;
                case "gift":
                    rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                            new OrderNotifyEvent(id, PayMethod.SANDPAY, serialNumber,
                                    System.currentTimeMillis(), OrderNotifyEvent.TYPE_GIFT_ORDER));
                    break;
                case "mintOrder":
                    rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                            new OrderNotifyEvent(id, PayMethod.SANDPAY, serialNumber,
                                    System.currentTimeMillis(), OrderNotifyEvent.TYPE_MINT_ORDER));
                    break;
                case "recharge":
                    rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                            new OrderNotifyEvent(id, PayMethod.SANDPAY, serialNumber,
                                    System.currentTimeMillis(), OrderNotifyEvent.TYPE_RECHARGE));
                    break;
                case "auctionOrder":
                    rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                            new OrderNotifyEvent(id, PayMethod.SANDPAY, serialNumber,
                                    System.currentTimeMillis(), OrderNotifyEvent.TYPE_AUCTION_ORDER));
                    break;
            }
        }
        return "SUCCESS";
    }
}
