cc.Class({
    extends: cc.Component,

    properties: {
        Level_num:0,
        Level_pass:0
    },
    onLoad: function () {
        var self = this;
        cc.loader.loadRes('level/llk', function (err, jsonAsset) {
            Global.leveljson = jsonAsset.json;
            self.Level_num = self.getJsonLength(jsonAsset.json);
            self.Level_pass = Storage.Get_Level_Pass();
            self.CreateLevelButton();
            self.moveToNow();
        });
       
    },
    moveToNow:function(){
        var self = this;
        var i = self.Level_pass+1;
        var pageview = cc.find('Canvas/PageView').getComponent(cc.PageView);
        if(i<20){
            pageview.setCurrentPageIndex(0);
        }else if(i<40){
            pageview.setCurrentPageIndex(1);
        }else if(i<60){
            pageview.setCurrentPageIndex(2);
        }else if(i<80){
            pageview.setCurrentPageIndex(3);
        }else if(i<100){
            pageview.setCurrentPageIndex(4);
        }else if(i<120){
            pageview.setCurrentPageIndex(5);
        }else if(i<140){
            pageview.setCurrentPageIndex(6);
        }else if(i<160){
            pageview.setCurrentPageIndex(7);
        }else if(i<180){
            pageview.setCurrentPageIndex(8);
        }
    },

    // called every frame
    update: function (dt) {

    },
    getJsonLength:function (jsonData) {
        var length = 0;
        for(var ever in jsonData) {
            length++;
        }
        return length;
    },
    CreateLevelButton:function(){
        var self = this;

        cc.loader.loadRes("prefab/关卡按钮Pass", function (err, prefab) {
            for(var i = 0; i <self.Level_pass; i++){//self.Level_num
                var newNode = cc.instantiate(prefab);
                var starnum = Storage.Get_Level_Pass_Info(i+1);
                if(starnum == 1){
                    newNode.getChildByName('星星2').active = false;
                    newNode.getChildByName('星星3').active = false;
                }else if(starnum == 2){
                    newNode.getChildByName('星星3').active = false;
                }else{
                    //全过
                }
                if(i<20){
                    newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_1').addChild(newNode);
                }else if(i<40){
                    newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_2').addChild(newNode);
                }else if(i<60){
                    newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_3').addChild(newNode);
                }else if(i<80){
                    newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_4').addChild(newNode);
                }else if(i<100){
                    newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_5').addChild(newNode);
                }else if(i<120){
                    newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_6').addChild(newNode);
                }else if(i<140){
                    newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_7').addChild(newNode);
                }else if(i<160){
                    newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_8').addChild(newNode);
                }else if(i<180){
                    newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_9').addChild(newNode);
                }
            }
            self.LoadNext();
        });
    },
    LoadNext:function(){
        var self = this;
        cc.loader.loadRes("prefab/关卡按钮Next", function (err, prefab) {
            var i = parseInt(self.Level_pass)+1;
            if(i>self.Level_num){
                //通关了大表哥
            }else if(self.Level_pass<20){
                var newNode = cc.instantiate(prefab);
                newNode.getChildByName('number').getComponent(cc.Label).string = i;
                cc.find('Canvas/PageView/view/content/page_1').addChild(newNode);
            }else if(self.Level_pass<40){
                var newNode = cc.instantiate(prefab);
                newNode.getChildByName('number').getComponent(cc.Label).string = i;
                cc.find('Canvas/PageView/view/content/page_2').addChild(newNode);
            }else if(self.Level_pass<60){
                var newNode = cc.instantiate(prefab);
                newNode.getChildByName('number').getComponent(cc.Label).string = i;
                cc.find('Canvas/PageView/view/content/page_3').addChild(newNode);
            }else if(self.Level_pass<80){
                var newNode = cc.instantiate(prefab);
                newNode.getChildByName('number').getComponent(cc.Label).string = i;
                cc.find('Canvas/PageView/view/content/page_4').addChild(newNode);
            }else if(self.Level_pass<100){
                var newNode = cc.instantiate(prefab);
                newNode.getChildByName('number').getComponent(cc.Label).string = i;
                cc.find('Canvas/PageView/view/content/page_5').addChild(newNode);
            }else if(self.Level_pass<120){
                var newNode = cc.instantiate(prefab);
                newNode.getChildByName('number').getComponent(cc.Label).string = i;
                cc.find('Canvas/PageView/view/content/page_6').addChild(newNode);
            }else if(self.Level_pass<140){
                var newNode = cc.instantiate(prefab);
                newNode.getChildByName('number').getComponent(cc.Label).string = i;
                cc.find('Canvas/PageView/view/content/page_7').addChild(newNode);
            }else if(self.Level_pass<160){
                var newNode = cc.instantiate(prefab);
                newNode.getChildByName('number').getComponent(cc.Label).string = i;
                cc.find('Canvas/PageView/view/content/page_8').addChild(newNode);
            }else if(self.Level_pass<180){
                var newNode = cc.instantiate(prefab);
                newNode.getChildByName('number').getComponent(cc.Label).string = i;
                cc.find('Canvas/PageView/view/content/page_9').addChild(newNode);
            }
            self.LoadLock();
        });
    },
    LoadLock:function (){
        var self = this;
        cc.loader.loadRes("prefab/关卡按钮Lock", function (err, prefab) {
            for(var i = self.Level_pass; i < self.Level_num; i++){
               // console.log(i);
                if(i<20){
                    var newNode = cc.instantiate(prefab);
                    //newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_1').addChild(newNode);
                }else if(i<40){
                    var newNode = cc.instantiate(prefab);
                    //newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_2').addChild(newNode);
                }else if(i<60){
                    var newNode = cc.instantiate(prefab);
                    //newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_3').addChild(newNode);
                }else if(i<80){
                    var newNode = cc.instantiate(prefab);
                    //newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_4').addChild(newNode);
                }else if(i<100){
                    var newNode = cc.instantiate(prefab);
                    //newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_5').addChild(newNode);
                }else if(i<120){
                    var newNode = cc.instantiate(prefab);
                    //newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_6').addChild(newNode);
                }else if(i<140){
                    var newNode = cc.instantiate(prefab);
                    //newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_7').addChild(newNode);
                }else if(i<160){
                    var newNode = cc.instantiate(prefab);
                    //newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_8').addChild(newNode);
                }else if(i<180){
                    var newNode = cc.instantiate(prefab);
                    //newNode.getChildByName('number').getComponent(cc.Label).string = i+1;
                    cc.find('Canvas/PageView/view/content/page_9').addChild(newNode);
                }
            } 
        });
    },
});
