cc.Class({
    extends: cc.Component,

    properties: {
        progressbar:null,
        progress:1,
        STOP:false,
    },

    // use this for initialization
    onLoad: function () {
        this.progressbar = cc.find('Canvas/ProgressBar');
        this.progress =  this.progressbar.getComponent(cc.ProgressBar);
    },
    start:function(){
        
    },
    // called every frame
    update: function (dt) {
        var self = this;
        if(Global.end != null && !self.STOP){
            if(Global.start.Type == Global.end.Type){
                //类型一样
                //先情况Global.LLKPath,因为检测死局的函数会生成一些脏数据，清除一次，确保万一
                Global.LLKPath = [];
                var CheckRes = CheckPath.Check(Global.start.ad,Global.end.ad,Global.TwoArrMap);
                if(CheckRes){
                    console.log(Global.LLKPath);
                    // Global.LLKPath = [];
                    self.UpdateMapArr();//先update，再绘图，因为绘图会导致global。start和end变为null
                    self.PaintLink();
                    Audio.playeffect('resources/audio/sound_bomb.ogg');
                    //画完清空状态
                    Global.LLKPath = [];
                    Global.TwoCore = false;
                }else{
                    //Audio.playeffect('resources/audio/sound_error.mp3');
                    console.log('不能连接');
                    Global.start = Global.end;
                    Global.end = null;
                }
            }else{
                //Audio.playeffect('resources/audio/sound_error.mp3');
                console.log('类型不一样');
                //把end替换start,然后置空
                Global.start = Global.end;
                Global.end = null;
                //CheckPath.Two(null,null);
            }
        }else if(Global.start != null){
            self.PaintPoint(Global.start.ad);
        }

    },
    PaintPoint: function(ad){
        var self = this;
        var ctx = self.node.getComponent(cc.Graphics);
        ctx.clear();
        ctx.rect(ad.col * Global.SideLength,ad.raw * Global.SideLength,Global.SideLength - 5,Global.SideLength - 5);
        ctx.stroke();
    },
    PaintLink:function(){
        var self = this;
        var ctx = self.node.getComponent(cc.Graphics);
        ctx.clear();
        console.log('是否俩拐角',Global.TwoCore);
        if(Global.TwoCore){
            ctx.moveTo(Global.start.ad.col * Global.SideLength + Global.SideLength/2,Global.start.ad.raw * Global.SideLength + Global.SideLength/2);
            for(var i =0; i < Global.LLKPath.length ; i++){
                ctx.lineTo(Global.LLKPath[i].col * Global.SideLength - Global.SideLength/2,Global.LLKPath[i].raw * Global.SideLength - Global.SideLength/2);
            }
        }else{
            for(var i =0; i < Global.LLKPath.length ; i++){
                if(i == 0){
                    ctx.moveTo(Global.LLKPath[i].col * Global.SideLength + Global.SideLength/2,Global.LLKPath[i].raw * Global.SideLength + Global.SideLength/2);
                }else{;
                    ctx.lineTo(Global.LLKPath[i].col * Global.SideLength + Global.SideLength/2,Global.LLKPath[i].raw * Global.SideLength + Global.SideLength/2);
                }
            }
        }
        ctx.stroke();
        //消失特效
        self.STOP = true;
        self.Animation();   
    },
    UpdateMapArr:function(){
        //修改Global.Twoarrmap,使已经连接的点的位置变为0
        Global.TwoArrMap[Global.start.ad.raw][Global.start.ad.col] = '0';
        Global.TwoArrMap[Global.end.ad.raw][Global.end.ad.col] = '0';
    },
    Animation:function(){
        var self = this;
        cc.loader.loadRes("prefab/XiaoShiAni", function (err, prefab) {
            var StartAni = cc.instantiate(prefab);
            StartAni.width = Global.SideLength - 5;
            StartAni.height = Global.SideLength - 5;
            StartAni.y = Global.start.ad.raw* Global.SideLength;
            StartAni.x = Global.start.ad.col* Global.SideLength;
            self.node.addChild(StartAni);

            var EndAni = cc.instantiate(prefab);
            EndAni.width = Global.SideLength - 5;
            EndAni.height = Global.SideLength - 5;
            EndAni.y = Global.end.ad.raw* Global.SideLength;
            EndAni.x = Global.end.ad.col* Global.SideLength;
            self.node.addChild(EndAni);

            Global.start.active = false;
            Global.end.active = false;

            Util.removeFromArrByValue(Global.Nodes,Global.start);
            Util.removeFromArrByValue(Global.Nodes,Global.end);

            Global.start = null;
            Global.end = null;
            self.STOP = false;
            
            self.scheduleOnce(function() {
                var ctx = self.node.getComponent(cc.Graphics);
                ctx.clear();
                //self.CheckGameStatus();  
            }, 0.1);
            self.CheckGameStatus();  
        });
        
    },
    CheckGameStatus:function(){
        var self = this;
        var ItemNum = 0;
        for(var i = 0; i <Global.TwoArrMap.length ; i++){
            for(var j = 0; j <Global.TwoArrMap[0].length; j++){
                ItemNum = ItemNum + parseInt(Global.TwoArrMap[i][j]);
            }
        }
        //console.log(Global.Nodes);
        if(ItemNum == 0){
            self.LevelSuccess();
        }else{
           // console.log('死局检查：',Util.CheckDiedMap());
           Util.CheckDiedMap();
        }
    },
    LevelSuccess:function(){
        var self = this;
        cc.audioEngine.stopMusic();

        self.scheduleOnce(function() {
            Audio.playeffect('resources/audio/sound_win.ogg');
        },1);

        var star1 = cc.find('Canvas/Success/Success/star1');
        var star2 = cc.find('Canvas/Success/Success/star2');
        var star3 = cc.find('Canvas/Success/Success/star3');
        
        Storage.Add_Level_Pass();
        cc.find('Canvas/Success').active = true;
        console.log('当前进度',self.progress.progress);
        if(self.progress.progress > 0.4){
            //三星
            star1.getComponent(cc.Animation).play();
            Audio.playeffect('resources/audio/sound_d_star_1.mp3');
            self.scheduleOnce(function() {
                star2.getComponent(cc.Animation).play();
                Audio.playeffect('resources/audio/sound_d_star_2.mp3');
            },0.3);
            self.scheduleOnce(function() {
                star3.getComponent(cc.Animation).play();
                Audio.playeffect('resources/audio/sound_d_star_3.mp3');
            },0.6);       
            Storage.Set_Level_Pass_Info(Global.LevelNow,3);
           
        }else if(self.progress.progress > 0.2){
             //二星
             star1.getComponent(cc.Animation).play();
            Audio.playeffect('resources/audio/sound_d_star_1.mp3');
            self.scheduleOnce(function() {
                star2.getComponent(cc.Animation).play();
                Audio.playeffect('resources/audio/sound_d_star_2.mp3');
            },0.3);
             Storage.Set_Level_Pass_Info(Global.LevelNow,2);
        }else{
            //一星
            star1.getComponent(cc.Animation).play();
            Audio.playeffect('resources/audio/sound_d_star_1.mp3');
            Storage.Set_Level_Pass_Info(Global.LevelNow,1);
        }
        self.scheduleOnce(function() {
            cc.director.pause();
        }, 2);
        
    }
    
   
});
