// 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;
        if (this.node.name == "boom") {
            NumberToImage.numberToImageNodes(this.time, 20, 8, "lock_", this.node.getChildByName("time"), false);
        }
        else if (this.node.name == "boom2") {
            NumberToImage.numberToImageNodes(this.time, 26, 8, "half_black_", this.node.getChildByName("time"), false);
            if (this.node.getChildByName("time").children.length > 1) {
                this.node.getChildByName("time").children[0].scale = 0.7;
                this.node.getChildByName("time").children[1].scale = 0.7;
                this.node.getChildByName("time").children[0].x -= 2;
                this.node.getChildByName("time").children[0].x -= 3;
            }
        }
        // this.node.getChildByName("time").getComponent(cc.Label).string = this.time.toString();
    }

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

    reduceTime(num) {
        if (this.node.parent) {
            if (this.node.parent.getComponent("Block").block_Info.floor) {
                return;
            }
        }
        if (this.node.name == "boom2") {
            this.time -= num;
            if (this.time <= 0) this.time = 0;
            NumberToImage.numberToImageNodes(this.time, 30, 8, "half_black_", this.node.getChildByName("time"), false);
            cc.fx.GameTool.numberScaleAction(this.node.getChildByName("time").children[0]);
            if (this.node.getChildByName("time").children.length > 1) {
                this.node.getChildByName("time").children[0].scale = 0.7;
                this.node.getChildByName("time").children[1].scale = 0.7;
                this.node.getChildByName("time").children[0].x -= 2;
                this.node.getChildByName("time").children[0].x -= 3;
            }
            if (this.time <= 0) {
                const mockTouchEvent = {
                    getLocation: () => {
                        // 获取父节点的位置作为触摸落点
                        const parentPos = this.node.parent.getPosition();
                        return parentPos;
                    }
                };

                // 触发父节点 Block 的 touchEnd 事件
                this.node.parent.getComponent("Block").touchEnd(mockTouchEvent);
                if (this.node.name == "boom2") {
                    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);
                });
            }
        }
    }

    destroyBoom(type) {
        if (this.over) return;
        this.over = true;
        if (this.node.name == "boom") {
            this.unschedule(this.updateTime);
        }
        if (this.node.parent) this.node.parent.getComponent("Block").resetFreeze();
        this.node.removeFromParent();
        cc.tween(this.node)
            .to(0.2, { opacity: 0 })
            .start();
        // this.updateCoin();
        if (this.node) {
            this.node.active = false;
            this.node.destroy();
        }
    }

    stopBoom() {
        if (this.node.name == "boom") {
            this.unschedule(this.updateTime);
        }
    }



    updateTime() {
        if (MapConroler._instance) {
            if (MapConroler._instance.iceTrue() == true) return;
            if (MapConroler._instance.pause == true) return;
        }
        else {
            return;
        }
        if (this.node.parent) {
            if (this.node.parent.getComponent("Block").block_Info.floor) {
                return;
            }
        }
        this.time--;
        if (this.node.name == "boom") {
            NumberToImage.numberToImageNodes(this.time, 20, 8, "lock_", this.node.getChildByName("time"), false);
            if (this.time < 10)
                cc.fx.GameTool.numberScaleAction(this.node.getChildByName("time"));
        }
        else if (this.node.name == "boom2") {
            NumberToImage.numberToImageNodes(this.time, 30, 8, "half_black_", this.node.getChildByName("time"), false);
            if (this.node.getChildByName("time").children.length > 1) {
                this.node.getChildByName("time").children[0].scale = 0.7;
                this.node.getChildByName("time").children[1].scale = 0.7;
                this.node.getChildByName("time").children[0].x -= 2;
                this.node.getChildByName("time").children[0].x -= 3;
            }
        }
        if (this.time <= 0) {
            // 创建模拟触摸事件对象
            const mockTouchEvent = {
                getLocation: () => {
                    // 获取父节点的位置作为触摸落点
                    const parentPos = this.node.parent.getPosition();
                    return parentPos;
                }
            };

            // 触发父节点 Block 的 touchEnd 事件
            this.node.parent.getComponent("Block").touchEnd(mockTouchEvent);
            if (this.node.name == "boom") {
                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();
        }
    }

    updateCoin() {
        var temp = MapConroler._instance.coin;
        temp += 100;
        MapConroler._instance.coin = temp;
        NumberToImage.numberToImageNodes(temp, 20, 8, "coin_", this.node.getChildByName("coin"), false);
    }

    // update (dt) {}
}
