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

const {ccclass, property} = cc._decorator;

export enum BlockType{
    /*普通地块 */
    Nomal = 0,  
    /*起点地块 */
    Start = 1,  
    /*湿地 */
    Nunja = 2,
    /*山峰 */
    Peak = 3,
    /*终点地块 */
    End = 4,

    /*息壤 */
    Xi_Soil = 5,
    /*加固 */
    Reinforce = 6
}

export enum PathType{
    err = "err",
    up = "up",  
    down = "down",  
    left = "left",
    right = "right",
    up_left = "up_left",
    up_right = "up_right",
    down_left = "down_left",
    down_right = "down_right",
    left_up = "left_up",
    left_down = "left_down",
    right_up = "right_up",
    right_down = "right_down",
}

@ccclass
export default class NewClass extends cc.Component {
    // LIFE-CYCLE CALLBACKS:
    @property(cc.SpriteAtlas)
    UI: cc.SpriteAtlas = null;

    pos_Shifting:number;
    block_Type:number;
    path_Type:string;
    finishi:boolean
    onLoad () {
        this.finishi = false;
        this.pos_Shifting = 0;
    }

    start () {
        
    }

    initData(type){
        this.block_Type = type;
        if(type == cc.Enum(BlockType).Start){ 
            this.node.color = cc.color(245,70,70);
        }
        else if(type == cc.Enum(BlockType).End){
            this.node.color = cc.color(20,255,0);
        }
        else if(type == cc.Enum(BlockType).Nunja){
            this.node.getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"]["e5"];
        }
        else if(type == cc.Enum(BlockType).Peak){
            let random = Math.floor(Math.random()*4+1);
            let name = "shan" + random;
            this.node.getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"][name];
        }
    }

    setPath(type){
        this.path_Type = type;
    }

    set_Xi_SoilType(direction,end){
        console.log("息壤",direction);
        if(this.block_Type == cc.Enum(BlockType).Nunja){
            this.block_Type = cc.Enum(BlockType).Nomal;
            this.node.getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"]["block_Bg"];
        }
        if(end != null){
            setTimeout(() => {
                cc.fx.Notifications.emit(cc.fx.Message.next,(end+1));
            }, 1000);
   
        }

        this.block_Type = cc.Enum(BlockType).Xi_Soil;
        let target = this.node.getChildByName("Xi_Soil"); 
        this.path_Type = direction;
        target.active = true;
        target.opacity = 0;
        cc.tween(target)
        .to(0.5,{opacity:255})
        .to(0.5,{opacity:0})
        .call(() =>{
            if(direction != null && end == null){
                let data = {
                    order:0,
                    time:0.2,
                    type:0,
                    circulate:false
                };
                this.runWater(data);
            }
        })
        .start();
    }


    //洪峰执行
    runWater(data){
        if(this.path_Type == "err"){
            setTimeout(() => {
                if(data.circulate)
                    cc.fx.Notifications.emit(cc.fx.Message.next,order);
            }, data.time);
            return;
        }
        //如果双向都占用了还进来就算死了- 
        if(this.node.getChildByName("vertical").active == true && this.node.getChildByName("horizontal").active == true){
            alert("河道已经交叉，不能再通过");
            return;
        }

        else if(this.node.getChildByName("turn").active == true && this.block_Type != cc.Enum(BlockType).Xi_Soil){
            alert("河道过弯处，不能再交叉河道");
            return;
        }
        var target = null;
        var progress = 1;
        var time = data.time;
        var order = data.order + 1;
        var random = Math.floor(Math.random()*4 + 1);
        target = this.node.getChildByName("vertical");
        let water1 = target.getChildByName("wave").getChildByName("water1");
        let water2 = target.getChildByName("wave").getChildByName("water2");
        let water3 = target.getChildByName("wave").getChildByName("water3");
        let timeDelay = 0.15;
        water1.runAction(cc.sequence(cc.moveTo(0.6,cc.v2(water1.x,120)),cc.delayTime(timeDelay),
        cc.callFunc(()=>{
            water1.y = -120
        })).repeatForever());
        water2.runAction(cc.sequence(cc.moveTo(0.6,cc.v2(water2.x,120)),cc.delayTime(timeDelay),
        cc.callFunc(()=>{
            water2.y = -120
        })).repeatForever());
        water3.runAction(cc.sequence(cc.moveTo(0.6,cc.v2(water3.x,120)),cc.delayTime(timeDelay),
        cc.callFunc(()=>{
            water3.y = -120
        })).repeatForever());
        var name = "a" + random;
        //if(this.block_Type != cc.Enum(BlockType).Reinforce)target.getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"][name];

        if(target.active == true){
            target = this.node.getChildByName("horizontal");
            name = "b" + random;
            //if(this.block_Type != cc.Enum(BlockType).Reinforce) target.getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"][name];
        } 
        // console.log(this.path_Type,data.order);
        if(this.path_Type == cc.Enum(PathType).up){
        }
        else if(this.path_Type == cc.Enum(PathType).down){
            target.angle = 180;
        }
        else if(this.path_Type == cc.Enum(PathType).left){
            target.angle = 90;
            name = "b" + random;
            //if(this.block_Type != cc.Enum(BlockType).Reinforce) target.getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"][name];
        }
        else if(this.path_Type == cc.Enum(PathType).right){
            target.angle = 270;
            name = "b" + random;
            //if(this.block_Type != cc.Enum(BlockType).Reinforce) target.getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"][name];
        }
        else{
            target = this.node.getChildByName("turn");
            let water = target.getChildByName("wave").getChildByName("water");
            water.opacity = 0
            water.runAction(cc.sequence(cc.rotateTo(7.2,-1080),cc.delayTime(0)).repeatForever());
            setTimeout(() => {
                water.opacity = 255;
            }, 200);
          
            name = "c" + random;
            //if(this.block_Type != cc.Enum(BlockType).Reinforce)target.getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"][name];
            progress = 0.25;
            if(this.path_Type == cc.Enum(PathType).up_left){
                target.setPosition(-this.pos_Shifting,-this.pos_Shifting);
            }
            else if(this.path_Type == cc.Enum(PathType).up_right){
                target.scaleX = -1;
                target.setPosition(this.pos_Shifting,-this.pos_Shifting);
            }
            else if(this.path_Type == cc.Enum(PathType).down_left){
                target.angle = 180;
                target.scaleX = -1;
                target.setPosition(-this.pos_Shifting,this.pos_Shifting);
            }
            else if(this.path_Type == cc.Enum(PathType).down_right){
                target.angle = 180;
                target.setPosition(this.pos_Shifting,this.pos_Shifting);
            }
            else if(this.path_Type == cc.Enum(PathType).left_up){
                target.angle = -90;
                target.scaleY = -1;
                target.setPosition(this.pos_Shifting,this.pos_Shifting);
            }
            else if(this.path_Type == cc.Enum(PathType).left_down){
                target.angle = 90;
                target.setPosition(-this.pos_Shifting,-this.pos_Shifting);
            }
            else if(this.path_Type == cc.Enum(PathType).right_up){
                target.angle = -90;
                target.setPosition(-this.pos_Shifting,this.pos_Shifting);
     
            }
            else if(this.path_Type == cc.Enum(PathType).right_down){
                target.angle = -90;
                target.scaleX = -1;
                target.setPosition(-this.pos_Shifting,-this.pos_Shifting);
            }
        }
        if(this.path_Type == "Reinforce"){
            console.log(data.order);
            target = this.node.getChildByName("Reinforce");
            this.block_Type = cc.Enum(BlockType).Reinforce;
            this.node.getChildByName("vertical").getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"]["e1"];
            this.node.getChildByName("turn").getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"]["e3"];
            this.node.getChildByName("horizontal").getComponent(cc.Sprite).spriteFrame = this.UI["_spriteFrames"]["e2"];
            target.active = true;
            target.opacity = 0;
            progress = 1;
            cc.tween(target)
            .to(time,{opacity:255})
            .call(() =>{
                if(data.circulate)
                    cc.fx.Notifications.emit(cc.fx.Message.changeMap,order);
                    cc.fx.Notifications.emit(cc.fx.Message.next,order);
            })
            .start();
        }
        else if(this.path_Type == "Xi_Soil"){
            target.active = false;
            this.block_Type = cc.Enum(BlockType).Xi_Soil;
            target = this.node.getChildByName("Xi_Soil"); 
            target.active = true;
            target.opacity = 0;
            cc.tween(target)
            .to(0.5,{opacity:255})
            .to(0.5,{opacity:0})
            .call(() =>{
                if(data.circulate)
                    cc.fx.Notifications.emit(cc.fx.Message.next,order);
            })
            .start();
        }
        else if(this.path_Type == "End"){
            target.active = false;
            this.block_Type = cc.Enum(BlockType).Xi_Soil;
            target = this.node.getChildByName("End"); 
            target.active = true;
            target.opacity = 0;
            cc.tween(target)
            .to(0.5,{opacity:255})
            .call(() =>{
                console.log("游戏结束");
                // if(data.circulate)
                //     cc.fx.Notifications.emit(cc.fx.Message.next,order);
            })
            .start();
        }
        else{
            target.active = true;
            if(this.block_Type != cc.Enum(BlockType).Xi_Soil){
                target.getComponent(cc.Sprite).fillRange = 0;
                cc.tween(target.getComponent(cc.Sprite))
                .to(time,{fillRange:progress})
                .call(() =>{
                    if(data.circulate)
                        cc.fx.Notifications.emit(cc.fx.Message.next,order);
                })
                .start();
            }
            else{
                target.opacity = 0;
                target.getComponent(cc.Sprite).fillRange = 1;
                cc.tween(target)
                .to(time,{opacity:255})
                .call(() =>{
                    if(data.circulate)
                        cc.fx.Notifications.emit(cc.fx.Message.next,order);
                })
                .start();
            }
        }

    }

    // update (dt) {}
}
