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

// prefab/pop/heathpop
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.Sprite)
    share_img: cc.Sprite = null;

    @property(cc.Sprite)
    ad_img: cc.Sprite = null;
    //金币花费数
    // @property(cc.Node)
    // coin: cc.Node = null;

    btn_Touch: boolean = true;

    onLoad() {
        this.btn_Touch = true;
        if (cc.fx.GameTool.canObtainByShare("health")) {
            this.share_img.node.active = true;

            this.ad_img.node.active = false;
        } else {
            this.share_img.node.active = false;

            this.ad_img.node.active = 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, "time_", 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();
    }

    onShare() {
        if (!this.btn_Touch) {
            return;
        }
        this.btn_Touch = false;
        var self = this;
        //获取当前体力
        // let curHeath = cc.fx.GameConfig.GM_INFO.hp;
        //获取当前金币
        // let curCoin = cc.fx.GameConfig.GM_INFO.coin;
        // if (curCoin >= 1000) {
        // cc.fx.GameConfig.GM_INFO.hp = 1; // 测试时不通过微信分享使用 

        var callback = function (isType) {
            //// 分享成功后才执行以下代码
            if (isType == true) {
                console.log("分享得体力");
                self.switchButtons[1].active = false;

                cc.fx.GameConfig.GM_INFO.hp = 1;
                // cc.fx.GameConfig.GM_INFO.min_Time = 0;
                // NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.hp, 40, 20, "month_", this.heatht, true);
                const parentNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
                if (parentNode) {
                    const jiazaiComp = parentNode.getComponent(JiaZai);
                    if (jiazaiComp) {
                        jiazaiComp.setHealthInfo(false);
                    }
                }
                self.switchButtons[0].active = true;
                cc.fx.GameTool.setUserHealth(0, (data) => {
                    cc.fx.GameTool.getHealth(null);
                })

                cc.fx.GameTool.addShareCount("health")
            }

        }

        if (cc.fx.GameTool.canObtainByShare("health")) {
            cc.fx.GameTool.shareToWX(callback)
        } else {
            let data = {
                ad_placement_name: "1002", //内部广告位名称
                current_page: "HomeScene" //所在页面
            }
            cc.fx.GameTool.onShowVideo(callback, data)
        }

    }

    //关闭弹窗
    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) {}
}
