package com.red.circle.other.adapter.app.game.party3rd;

import com.alibaba.fastjson.JSON;
import com.red.circle.framework.core.exception.ResponseException;
import com.red.circle.framework.web.annotation.IgnoreResultResponse;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.cmd.party3rd.*;
import com.red.circle.other.app.service.game.override.service.YomiGameService;
import com.red.circle.other.infra.config.YomiConfig;
import com.red.circle.other.infra.utils.AesUtils;
import com.red.circle.wallet.inner.error.WalletErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;

import java.util.Objects;

/**
 * Yomi游戏REST接口
 *
 * @eo.name Yomi游戏接口
 */
@Slf4j
@RestController
@RequestMapping("/game/yomi")
@RequiredArgsConstructor
public class YomiGameRestController extends BaseController {
    
    private final YomiGameService yomiGameService;
    private final YomiConfig yomiConfig;
    
    /**
     * 获取token
     *
     * @eo.name 获取token
     * @eo.url /api/gettoken
     * @eo.method post
     * @eo.request-type text/plain
     */
    @IgnoreResultResponse
    @PostMapping(value = "/api/gettoken")
    public YomiResponseWrapper<YomiTokenCO> getToken(@RequestBody String encryptedBody) {
        try {
            String decryptedJson = AesUtils.decryptAESWithBase64(encryptedBody, yomiConfig.getAesKey());
            YomiGetTokenCmd cmd = JSON.parseObject(decryptedJson, YomiGetTokenCmd.class);
            
            YomiTokenCO result = yomiGameService.getToken(cmd);
            return YomiResponseWrapper.success(result);
            
        } catch (Exception e) {
            log.error("yomi getToken 业务异常: {}", e.getMessage());
            return YomiResponseWrapper.fail(500, e.getMessage());
        }
    }
    
    /**
     * 获取用户信息
     *
     * @eo.name 获取用户信息
     * @eo.url /api/userinfo
     * @eo.method post
     * @eo.request-type text/plain
     */
    @IgnoreResultResponse
    @PostMapping("/api/userinfo")
    public YomiResponseWrapper<YomiUserInfoCO> getUserInfo(@RequestBody String encryptedBody) {
        try {
            String decryptedJson = AesUtils.decryptAESWithBase64(encryptedBody, yomiConfig.getAesKey());
            YomiUserInfoCmd cmd = JSON.parseObject(decryptedJson, YomiUserInfoCmd.class);
            
            // 调用Service处理
            YomiUserInfoCO result = yomiGameService.getUserInfo(cmd);
            return YomiResponseWrapper.success(result);

        } catch (Exception e) {
            log.error("yomi getUserInfo 业务异常: {}", e.getMessage());
            return YomiResponseWrapper.fail(500, e.getMessage());
        }
    }
    
    /**
     * 用户余额变动
     *
     * @eo.name 用户余额变动
     * @eo.url /api/change_balance
     * @eo.method post
     * @eo.request-type text/plain
     */
    @IgnoreResultResponse
    @PostMapping("/api/change_balance")
    public YomiResponseWrapper<YomiBalanceCO> changeBalance(@RequestBody String encryptedBody) {
        try {
            // AES解密
            String decryptedJson = AesUtils.decryptAESWithBase64(encryptedBody, yomiConfig.getAesKey());

            // 解析JSON为Cmd对象
            YomiChangeBalanceCmd cmd = JSON.parseObject(decryptedJson, YomiChangeBalanceCmd.class);
            
            // 调用Service处理
            YomiBalanceCO result = yomiGameService.changeBalance(cmd);
            return YomiResponseWrapper.success(result);

        } catch (ResponseException e) {
            log.error("yomi changeBalance 业务异常: recordId={}, error={}", 
                e.getMessage(), e.getMessage());

            if (Objects.equals(e.getErrorCode(), WalletErrorCode.INSUFFICIENT_BALANCE.getCode())) {
                return YomiResponseWrapper.fail(
                    WalletErrorCode.INSUFFICIENT_BALANCE.getCode(), 
                    WalletErrorCode.INSUFFICIENT_BALANCE.getMessage()
                );
            }
            return YomiResponseWrapper.fail(500, e.getMessage());
        } catch (Exception e) {
            log.error("yomi changeBalance 系统异常: {}", e.getMessage(), e);
            return YomiResponseWrapper.fail(500, e.getMessage());
        }
    }
    
}