// 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;

export enum WallSpecial {
    /*普通门*/
    "普通门" = 0,
    /*星星门*/
    "星星门" = 1,
    /*开关门*/
    "开关门" = 2,
    /*冻结门 */
    "冻结门" = 3,
    /*冻结门 */
    "旋转门顺时针" = 4,
    /*旋转门逆时针*/
    "旋转门逆时针" = 5,
}

export enum WallType {

    /*普通地块 */
    "门横向下" = 0,
    /*起点地块 */
    "门横向上" = 1,
    /*湿地 */
    "门竖向右" = 2,
    /*山峰 */
    "门竖向左" = 3,
    /*终点地块 */
    "墙横向下" = 4,
    /*息壤 */
    "墙横向上" = 5,
    /*加固 */
    "墙竖向右" = 6,
    /*加固 */
    "墙竖向左" = 7,
}

export enum WallColor {
    /*普通地块 */
    "紫色" = 0,
    /*湿地 */
    "黄色" = 1,
    /*山峰 */
    "绿色" = 2,
    /*终点地块 */
    "蓝色" = 3,
    /*息壤 */
    "粉色" = 4,
    /*加固 */
    "橘黄色" = 5,
    /*加固 */
    "青色" = 6,
    /*加固 */
    "白色" = 7,
    /*普通地块 */
    "红色" = 8,
    /*普通地块 */
    "灰色" = 9,
}

@ccclass
export default class Wall extends cc.Component {

    @property(cc.Label)
    number: cc.Label = null;

    @property({
        tooltip: '墙或者门的方向',
        type: cc.Enum(WallType),
    })
    type: WallType = WallType.墙横向下;

    @property({
        tooltip: '墙或者门的方向',
        type: cc.Enum(WallSpecial),
    })
    special: WallSpecial = WallSpecial.普通门;

    @property({
        tooltip: '门的颜色',
        type: cc.Enum(WallColor),
    })
    color: WallColor = WallColor.紫色;

    @property(cc.SpriteAtlas)
    wall_SpriteFrames: cc.SpriteAtlas = null;

    @property(cc.SpriteAtlas)
    down_SpriteFrames: cc.SpriteAtlas = null;

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


    posX: number;
    posY: number;
    direction: any;
    wall_Info: any;
    openNode: cc.Node;
    revolvingNode: cc.Node;
    freezeNode: cc.Node;
    open: boolean;
    freezeNumber: number;
    num: number;
    leftTween: any;
    rightTween: any;


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        // console.log(this.type);
    }

    jsonDeepClone<T>(obj: T): T {
        return JSON.parse(JSON.stringify(obj));
    }

    init(wall_Info, posX: number, posY: number, direction: any) {

        this.wall_Info = this.jsonDeepClone(wall_Info);
        // this.open = true;
        if (wall_Info == null) {
            this.posX = posX;
            this.posY = posY;

            if (direction) this.direction = direction;
            // console.log("门方向赋值",direction);
            if (direction == "up") {
                this.node.parent.zIndex = 100 + this.posX - this.posY * 3;
            }
            else if (direction == "down" || direction == "right") {
                this.node.parent.zIndex = 20 + this.posX - this.posY * 3;
            }
            else if (direction == "downleft" || direction == "downright" || direction == "rightup"
                || direction == "rightdown") {
                this.node.parent.zIndex = 20 + this.posX - this.posY * 3;
            }
            else if (direction == "left" || direction == "leftup" || direction == "upleft") {
                this.node.parent.zIndex = 70 + this.posX - this.posY * 3;
            }
            else if (direction == "leftdown" || direction == "upright") {
                this.node.parent.zIndex = 100 + this.posX - this.posY * 3;
            }
            else this.node.parent.zIndex = 70 + this.posX - this.posY * 3;

            MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id = "Wall";
            //console.log(this.posX,this.posY,MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id);
            // console.log(this.posX,this.posY,this.node.zIndex);
            //this.node.getChildByName("num").getComponent(cc.Label).string = direction;
            //this.node.getChildByName("num").getComponent(cc.Label).string = ":" + this.node.parent.zIndex;
        }
        if (wall_Info != null) {
            this.color = this.wall_Info.color;
            this.special = this.wall_Info.special;
            this.num = this.wall_Info.num;
            let name2 = "rotate" + this.wall_Info.length;
            this.revolvingNode = this.node.parent.getChildByName("revolving").getChildByName(name2);
            if (this.revolvingNode) {
                this.revolvingNode.parent.zIndex = 999;
                this.revolvingNode.active = false;
            }
            this.initType();
            if (this.wall_Info.length > 0) {
                this.initColor(this.wall_Info.length);
                MapConroler._instance.wall_Pass.push(this);
            }
            else this.node.removeComponent("cc.Sprite");
            if (this.posX != null) {
                MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id = "Wall";
                //console.log(this.posX,this.posY,MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id);
            }
        }
        // setTimeout(() => {
        //     this.node.getChildByName("num").getComponent(cc.Label).string = ":" + this.node.parent.zIndex;
        // }, 1000);

    }

    //创建门的颜色
    initColor(length: number) {
        let direction = this.node.parent.name;
        let double = 0;
        if (direction == "left" || direction == "right") {
            double = 3;
        }

        // debugger;
        if (this.wall_SpriteFrames) {
            let name = this.color + "color" + (length + double);
            var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
            if (this.node.getComponent(cc.Sprite)) this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            if (this.node.getChildByName("icon")) this.node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
        }
        if (this.down_SpriteFrames) {
            let name2 = this.color + "down" + (length + double);
            var downFrame = this.down_SpriteFrames._spriteFrames[name2];
            this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = downFrame;
        }
    }

    //创建特殊类型门
    initType() {
        switch (this.special) {
            case WallSpecial.星星门:
                let star = cc.instantiate(MapConroler._instance.Block_Prop[this.special]);
                star.parent = this.node.parent;
                // console.log("门的方向",this.direction,"长度",this.wall_Info.length);
                // star.scaleX = star.scaleY = 0.5;
                if (this.wall_Info.length > 0) {
                    if (this.direction == "right" || this.direction == "left") {
                        star.children[this.wall_Info.length + 2].active = true;
                    }
                    else if (this.direction == "up" || this.direction == "down") {
                        star.children[this.wall_Info.length - 1].active = true;
                    }
                }
                star.setPosition(this.node.width / 2 + this.node.x, this.node.height / 2 + this.node.y);
                break;
            case WallSpecial.开关门:
                let name = "open" + this.wall_Info.length;
                this.openNode = this.node.parent.getChildByName("open").getChildByName(name);
                this.openNode.active = true;
                if (this.wall_Info.lock == false) {
                    this.open = true;
                    this.openNode.children[0].scaleX *= 0.01;
                    this.openNode.children[1].scaleX *= 0.01;
                }
                else {
                    this.open = false;
                }
                break;
            case WallSpecial.冻结门:
                let freeze = "freeze" + this.wall_Info.length;
                this.freezeNode = this.node.parent.getChildByName("freeze").getChildByName(freeze);
                this.freezeNode.active = true;

                if (this.wall_Info.freeze) {
                    this.freezeNumber = this.wall_Info.freeze;
                    NumberToImage.numberToImageNodes(this.freezeNumber, 20, 8, "lock_", this.freezeNode.getChildByName("num"), false);
                }
                break;
            case WallSpecial.旋转门顺时针: case WallSpecial.旋转门逆时针:
                if (this.wall_Info.length > 0) {
                    // let name2 = "rotate" + this.wall_Info.length;
                    // this.revolvingNode = this.node.parent.getChildByName("revolving").getChildByName(name2);
                    this.revolvingNode.active = true;
                    // if (this.special == WallSpecial.旋转门逆时针) {
                    //     this.revolvingNode.children[0].scaleX = -this.revolvingNode.children[0].scaleX;
                    // }
                }

                break;
        }
    }

    //播放星星门通过
    playStarDoor() {
        if (this.node.parent.getChildByName("star")) {
            let star = this.node.parent.getChildByName("star");
            for (let i = 0; i < star.children.length; i++) {
                if (star.children[i].active == true) {
                    let starChild = star.children[i];
                    for (let j = 0; j < starChild.children.length; j++) {
                        starChild.children[j].active = true
                        starChild.children[j].getComponent(sp.Skeleton).setAnimation(1, "taopao1", false);
                    }
                }
            }
        }
    }


    //改变开关门状态
    changeLock() {
        this.open = !this.open;
        console.log("开关门状态改变", this.open);
        if (!this.openNode.active) {
            this.openNode.active = true;
        }


        // 停止之前的动画
        if (this.leftTween) {
            this.leftTween.stop();
            this.leftTween = null;
        }
        if (this.rightTween) {
            this.rightTween.stop();
            this.rightTween = null;
        }
        if (this.open == true) {
            if (this.openNode.children[0].scaleX < 1 && this.openNode.children[0].scaleX > 0.01) {
                this.openNode.children[0].scaleX = 1;
            }
        }
        else {
            if (this.openNode.children[0].scaleX < 1 && this.openNode.children[0].scaleX > 0.01) {
                this.openNode.children[0].scaleX = 0.01;
            }
        }


        let fill = this.openNode.children[0].scaleX == 1 ? 0.01 : 1;
        if (this.openNode.children[0].scaleX < 0) fill = -fill;
        // console.log("目标",fill);

        // 存储动画实例以便后续停止
        this.leftTween = cc.tween(this.openNode.children[0])
            .to(0.3, { scaleX: this.openNode.children[0].scaleX < 0 ? -fill : fill })
            .call(() => {
                // console.log("左边完成");
                this.leftTween = null; // 动画完成后清除引用
            })
            .start();

        this.rightTween = cc.tween(this.openNode.children[1])
            .to(0.3, { scaleX: this.openNode.children[1].scaleX < 0 ? -fill : fill })
            .call(() => {
                // console.log("右边完成");
                this.rightTween = null; // 动画完成后清除引用
            })
            .start();
    }

    changeFreeze() {
        this.freezeNumber -= 1;
        if (this.freezeNumber == 0) {
            console.log(this.node.uuid);
            // this.freezeNode.parent.active = false;
            this.resetFreeze();
        }
        else {
            NumberToImage.numberToImageNodes(this.freezeNumber, 20, 8, "lock_", this.freezeNode.getChildByName("num"), false);
        }
    }

    downDoor() {
        if (this.openNode) {
            if (this.openNode.children[0].scaleX == 1)
                return;
        }
        if (this.freezeNode) {
            if (this.freezeNode.active == true)
                return;
        }
        if (this.special == 4 || this.special == 5) {
            return;
        }

        this.node.opacity = 0;
        if (this.special == WallSpecial.星星门) {
            this.node.parent.getChildByName("star").y -= 10;
        }
        this.node.parent.getChildByName("down").active = true;
    }

    upDoor() {
        if (this.special == WallSpecial.星星门) {
            this.node.parent.getChildByName("star").y += 10;
        }
        this.node.parent.getChildByName("down").active = false;
        this.node.opacity = 250;
    }

    resetFreeze() {
        this.special = 0;
        if (this.freezeNode) {
            cc.fx.AudioManager._instance.playEffect("freezeDoor", null);
            this.freezeNode.active = false;
        }
        // if (this.wall_Info.length != 0) {
        //     let spine = cc.instantiate(this.freezeSpine);
        //     spine.parent = this.node.parent;
        //     spine.setPosition(this.node.width / 2 + this.node.x + 60 / 2 * (this.wall_Info.length - 1), this.node.height / 2 + this.node.y);
        //     spine.getComponent(sp.Skeleton).setAnimation(1, "bingkuai", false);
        //     console.log("添加一个动画", spine.getPosition());
        // }
    }

    changeRevolvingWall() {
        if (this.revolvingNode) {
            // console.log("自身编号", this.wall_Info.num, this.special);
            if (this.special == WallSpecial.旋转门顺时针 || this.special == WallSpecial.旋转门逆时针) {
                this.revolvingNode.active = true;
            }
            else {
                this.revolvingNode.active = false;
            }
        }

    }

    // update (dt) {}
}
