import NumberToImage from "./NumberToImage";
import JiaZai from "./JiaZai";
import { MiniGameSdk } from "./Sdk/MiniGameSdk";


const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    //商店界面
    @property(cc.Node)
    heath: cc.Node = null;
    //体力数量
    @property(cc.Node)
    heatht: cc.Node = null;
    //切换的节点
    @property([cc.Node])
    switchNode: cc.Node[] = [];
    //倒计时健康回复
    @property(cc.Node)
    timeNode: cc.Node = null;
    //切换的按钮数组
    @property([cc.Node])
    switchButtons: cc.Node[] = [];
    //金币花费数
    @property(cc.Node)
    coin: cc.Node = null;

    btn_Touch: boolean = true;

    onLoad() {
        this.btn_Touch = true;
    }
    start() {
        this.btn_Touch = true;
        this.openPop();
    }
    //打开商店界面
    openPop() {
        // console.log("打开heath弹窗", cc.fx.GameConfig.GM_INFO.hp);

        //NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.hp, 30, 15, "tili_x_", this.coin, true);
    }
    startGame() {
        // 获取场景中的 JiaZai 组件
        const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
        if (jiazaiNode) {
            const jiazaiComp = jiazaiNode.getComponent(JiaZai);
            if (jiazaiComp) {
                jiazaiComp.startGame();
            } else {
                console.warn("JiaZai 组件未找到");
            }
        } else {
            console.warn("JiaZai 节点未找到");
        }
        this.closePop();
    }

    //购买体力
    buyHeath() {
        if (!this.btn_Touch) {
            return;
        }
        this.btn_Touch = false;
        //获取当前体力
        let curHeath = cc.fx.GameConfig.GM_INFO.hp;
        //获取当前金币
        let curCoin = cc.fx.GameConfig.GM_INFO.coin;
        if (curCoin >= 1000) {
            this.switchButtons[1].active = false;
            cc.fx.GameTool.changeCoin(-1000);
            const data = {
                change_reason: "首页购买体力",
                id: (1001 + ""),
                num: -1000
            }
            cc.fx.GameTool.shushu_Track("resource_cost", data);
            cc.fx.GameConfig.GM_INFO.hp = cc.fx.GameConfig.GM_INFO.hp_Max;
            cc.fx.GameConfig.GM_INFO.min_Time = 0;
            const parentNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
            if (parentNode) {
                const jiazaiComp = parentNode.getComponent(JiaZai);
                if (jiazaiComp) {
                    jiazaiComp.setHealthInfo(false);
                    jiazaiComp.updateCoin();
                }
            }
            this.switchButtons[0].active = true;
            cc.fx.GameTool.setUserHealth(0, (data) => {
                cc.fx.GameTool.getHealth(null);
            })
            const buyData = {
                item_id: "refill_health",
                item_num: 3,
                item_price: 1000,
                cost_type: "gold"
            }
            // console.log("____________即将上传Shop_buy", buyData);
            cc.fx.GameTool.shushu_Track("shop_buy", buyData);

        }
        else {
            MiniGameSdk.API.showToast("金币不足，无法购买体力");
            setTimeout(() => {
                this.btn_Touch = true;
                this.openShop();
            }, 500);
        }
    }

    //金币不够购买金币
    openShop() {
        //获取场景中的 JiaZai 组件
        const parentNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
        if (parentNode) {
            const jiazaiComp = parentNode.getComponent(JiaZai);
            if (jiazaiComp) {
                jiazaiComp.openShop();
            }
        }
    }

    //关闭弹窗
    closePop() {
        this.btn_Touch = true;
        //销毁预制体
        // console.log("关闭heath弹窗");
        //关闭计时器
        // 获取场景中的 JiaZai 组件
        const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
        if (jiazaiNode) {
            const jiazaiComp = jiazaiNode.getComponent(JiaZai);
            if (jiazaiComp) {
                jiazaiComp.stopHeathTimeCutDown();
            } else {
                console.warn("JiaZai 组件未找到");
            }
        } else {
            console.warn("JiaZai 节点未找到");
        }
        this.heath.destroy();
    }

    // update (dt) {}
}
