// 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 Floor extends cc.Component {
    static _instance: any;
    time: number = 60;

    @property(cc.Material)
    floor: cc.Material = null;

    @property(cc.Prefab)
    ice: cc.Prefab = null;

    @property({ type: [cc.SpriteFrame], tooltip: "可移动地板图片" })
    FloorSpriteFrame: Array<cc.SpriteFrame> = [];
    posX: any;
    posY: any;

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

    onLoad() {

    }

    start() {

    }

    init(time, posX, posY) {
        if (time) this.time = time;
        this.node.getChildByName("time").active = true;
        NumberToImage.numberToImageNodes(this.time, 30, 15, "level_", this.node.getChildByName("time"), false);
        // this.node.getChildByName("time").getComponent(cc.Label).string = this.time.toString();
        if (posX) {
            this.posX = posX;
        }
        if (posY) {
            this.posY = posY;
        }

    }

    setSpriteFrame(number) {
        if (this.FloorSpriteFrame.length > 0) {
            this.node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.FloorSpriteFrame[number];
        }
    }

    reduce(number) {
        if (this.node == null || this.node == undefined) {
            return;
        }
        // debugger;
        this.time -= number;
        if (this.time <= 0) this.time = 0
        NumberToImage.numberToImageNodes(this.time, 30, 15, "level_", this.node.getChildByName("time"), false);
        cc.fx.GameTool.numberScaleAction(this.node.getChildByName("time"));
        // this.node.getChildByName("time").getComponent(cc.Label).string = this.time.toString();
        if (this.time <= 0) {
            if (this.node.parent) {
                this.node.parent.getComponent("Block").resetFloor();
                // console.log("恢复成普通块", this.node.uuid);
            }
            this.node.getChildByName("bingkuai").active = true;
            const skeleton = this.node.getChildByName("bingkuai").getComponent(sp.Skeleton);
            skeleton.setAnimation(1, "bingkuai", false);
            this.node.getChildByName("icon").active = true;
            this.node.getChildByName("time").active = false;
            cc.tween(this.node.getChildByName("icon"))
                .to(0.5, { opacity: 0 })
                .start();
            // this.node.getChildByName("icon").getComponent(cc.Sprite).setMaterial(0,this.Floor);
            // this.node.children.forEach(element => {
            //     element.destroy();
            // });
            // setTimeout(() => {

            // }, 1);

            setTimeout(() => {
                if (this.node == null || this.node == undefined) {
                    return;
                }
                // this.node.destroy();
                // this.node.removeFromParent();


            }, 600);
        }
    }


    // update (dt) {}
}
