// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import MapConroler from "../Map";
import NumberToImage from "../NumberToImage";

const { ccclass, property } = cc._decorator;



@ccclass
export default class Boom extends cc.Component {
    static _instance: any;
    time: number = 60;
    over: boolean = false;

    // mapInfo: number[][] = [];

    onLoad() {
        this.over = false;
    }

    start() {
        this.over = false;
    }

    init(time) {
        if (time) this.time = time;
        this.node.getChildByName("time").active = true;
        NumberToImage.numberToImageNodes(this.time, 20, 8, "lock_", this.node.getChildByName("time"), false);
        // this.node.getChildByName("time").getComponent(cc.Label).string = this.time.toString();
    }

    startBoom() {
        if (MapConroler._instance.iceTrue() == true) return;
        if (MapConroler._instance.pause == true) return;
        this.schedule(this.updateTime, 1);
    }

    destroyBoom(type) {
        if (this.over) return;
        this.over = true;
        this.unschedule(this.updateTime);
        if (this.node.parent) this.node.parent.getComponent("Block").resetFreeze();
        this.node.active = false;
        this.node.destroy();
    }

    stopBoom() {
        this.unschedule(this.updateTime);
    }



    updateTime() {
        if (MapConroler._instance.iceTrue() == true) return;
        if (MapConroler._instance.pause == true) return;
        this.time--;
        NumberToImage.numberToImageNodes(this.time, 20, 8, "lock_", this.node.getChildByName("time"), false);
        if (this.time <= 0) {
            // 创建模拟触摸事件对象
            const mockTouchEvent = {
                getLocation: () => {
                    // 获取父节点的位置作为触摸落点
                    const parentPos = this.node.parent.getPosition();
                    return parentPos;
                }
            };

            // 触发父节点 Block 的 touchEnd 事件
            this.node.parent.getComponent("Block").touchEnd(mockTouchEvent);
            this.unschedule(this.updateTime);
            this.node.getChildByName("time").active = false;
            MapConroler._instance.failLevel("boom");
            this.node.getChildByName("zhandan").active = true;
            this.node.getChildByName("bg").active = false;
            const skeleton = this.node.getChildByName("zhandan").getComponent(sp.Skeleton);
            skeleton.setAnimation(1, "eff", false);
            // 监听动画完成事件,销毁自身爆炸节点
            skeleton.setCompleteListener(() => {
                // 动画播放完成后销毁节点
                this.destroyBoom(true);
            });

            // this.node.destroy();
        }
    }

    // update (dt) {}
}
