import Utils from "./module/Pay/Utils";
import { MiniGameSdk } from "./Sdk/MiniGameSdk";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RedeemCode extends cc.Component {
    @property(cc.Button)
    onBtn: cc.Button = null;

    @property(cc.EditBox)
    editBoxCode: cc.EditBox = null;


    start() {

    }

    onConfirm() {
        const account = this.editBoxCode ? this.editBoxCode.string.trim() : "";

        // 判断用户输入的值是否为空
        if (!account) {
            console.log("兑换码不能为空");
            return;
        }

        // 如果不为空，发送给服务器
        console.log("发送兑换码到服务器:", account);
        // let res = { code: 1, data: { reward: { 1001: 1000, 2001: 1, 2002: 1, 2003: 1 } } }
        Utils.getRedeemInfo((res) => {
            if (res.code === 1) {
                let rewards = res.data.reward;
                const rewardData = [];
                const canvasTemp = cc.find("Canvas");
                let JiaZai = null;

                if (canvasTemp) {
                    JiaZai = canvasTemp.getComponent("JiaZai");
                }
                for (let itemId in rewards) {
                    let itemNum = rewards[itemId];
                    cc.fx.GameTool.getPassCheckPorp(parseInt(itemId), itemNum, false, true);

                    let typeStr = '';
                    let finalItemNum = itemNum;
                    switch (parseInt(itemId)) {
                        case 1001: // 金币
                            typeStr = 'coin';
                            break;
                        case 2001: // 冰冻道具
                            typeStr = 'freeze';
                            break;
                        case 2002: // 锤子道具
                            typeStr = 'hammer';
                            break;
                        case 2003: // 魔棒道具
                            typeStr = 'magic';
                            break;
                        case 3001: // 无限体力 900秒
                            typeStr = 'infinite_health';
                            finalItemNum = 900;
                            break;
                        case 3002: // 无限体力 1800秒
                            typeStr = 'infinite_health';
                            finalItemNum = 1800;
                            break;
                        case 3003: // 无限体力 3600秒
                            typeStr = 'infinite_health';
                            finalItemNum = 3600;
                            break;
                        default:
                            typeStr = 'infinite_health'; // 默认类型
                            finalItemNum = itemNum * 900;
                            break;
                    }

                    // let itemIdType = this.getItemIdType(itemId, itemNum);
                    rewardData.push({ type: typeStr, count: finalItemNum });
                }
                // 领取物品弹窗
                console.log("rewardData", rewardData)
                if (JiaZai && rewardData.length > 0) {
                    JiaZai.openRewardWindow(rewardData);
                }
                JiaZai.updateCoin();

                const data = {
                    code: account,
                }
                cc.fx.GameTool.shushu_Track("redeem", data);

                setTimeout(() => {
                    this.onClose();
                }, 1000);
            } else {
                MiniGameSdk.API.showToast(res.msg);
            }
        }, account)
    }

    onClose() {
        this.node.destroy();
    }
    // update (dt) {}
}
