cc.Class({
    extends: cc.Component,

    properties: {
        TwoArrMap:null,
        filename:null,
        StylePath:null,
        StyleNum:0,
    },

    // use this for initialization
    onLoad: function () {
        var self = this;
        if(Global.Style == 0){
            self.StylePath = 'pet/a';
            self.StyleNum = 20;
        }else if(Global.Style == 1){
            self.StylePath = 'fruit/';
            self.StyleNum = 16;
        }else if(Global.Style == 2){
            self.StylePath = 'bq1/a';
            self.StyleNum = 16;
        }
        //console.log(Global.leveljson[Global.LevelNow]);
        self.filename = Global.leveljson[Global.LevelNow];
        self.MakeMap(self.filename.filename);
        self.scheduleOnce(function() {
            Audio.playeffect('resources/audio/sound_readygo.mp3');
        },0.5);
    },
    start:function(){
        
    },
    // called every frame
    update: function (dt) {

    },
    MakeMap:function(json){
        var self = this;
        var jsonpath = 'level/'+json;
        cc.loader.loadRes(jsonpath, function (err, jsonAsset) {
            var mystr = jsonAsset.json.maps;
            var maps = mystr.split(",");
            var TwoArrMap = self.One2TwoArray(maps,jsonAsset.json.col_d);
            self.TwoArrMap = TwoArrMap;
            Global.TwoArrMap = TwoArrMap;
            Global.LevelTime = jsonAsset.json.time;
            self.CreateMap(TwoArrMap);
        });
    },
    //subArrayNum为每行的个数
    One2TwoArray:function(maps,subArrayNum){
        //let data = [3, 6, 4, 5, 6, 99, 55, 45, 33, 22, 90, 12, 54, 23, 33, 243, 2324, 12, 335, 23, 21, 22, 22];
        //let subArrayNum = 12;
        var dataArr = new Array(Math.ceil(maps.length / subArrayNum));

        for(let i = 0; i < dataArr.length;i++) {

            dataArr[i] = new Array();
            for(let j = 0; j < subArrayNum; j++) {
                //先创建空的二维数组
                dataArr[i][j] = '';

            }

        }
        for(let i = 0; i < maps.length;i++) {
            //将一维数组的值赋值给二维数组
            dataArr[parseInt(i / subArrayNum)][i % subArrayNum] = maps[i]; 

        } 
        return dataArr;
    },
    CreateMap:function(TwoArrMap){
        var self = this;
        var Raw = TwoArrMap.length;
        var col = TwoArrMap[0].length;
        for(let i = 0; i < Raw; i++){
            for(let j = 0; j < col ; j++){
                if(TwoArrMap[i][j] == "1"){
                    cc.loader.loadRes("prefab/LLKItem", function (err, prefab) {
                        var newNode = cc.instantiate(prefab);
                        var MainArea = cc.find('Canvas/MainArea');
                        var SideLength = Math.min(MainArea.width/col,MainArea.height/Raw);
                        Global.SideLength = SideLength;
                        MainArea.addChild(newNode);
                        newNode.attr({ ad:{raw: i,col:j} })
                        newNode.width = SideLength - 5;
                        newNode.height = SideLength - 5;
                        newNode.y = i*SideLength;
                        newNode.x = j*SideLength;
                    });
                }
            }
        }
        self.SetNodeDetail();
        
    },
    SetNodeDetail:function(){
        var self = this;
        var TwoArrMap = self.TwoArrMap;
        var NeedShowNum = 0;
        for(let i = 0; i < TwoArrMap.length; i++){
            for(let j=0; j< TwoArrMap[0].length;j++){
                if(TwoArrMap[i][j] == "1"){
                    NeedShowNum++;
                }
            }
        }
        var ResIndexArr = [];
        for(let i = 0; i < NeedShowNum/2 ; i++){
            ResIndexArr.push(i% self.StyleNum +1);//随机生成几副图的单个资源
        }
        for(let i = 0; i <NeedShowNum/2 ; i++){
            ResIndexArr.push(ResIndexArr[i]);//翻倍
        }
        ResIndexArr = Util.shuffleArray(ResIndexArr);
        //console.log(ResIndexArr);
        var NodeList = cc.find("Canvas/MainArea").children;
        //console.log(ResIndexArr);
        
		setTimeout(()=>{
			Global.Nodes = NodeList;
			for (let i = 0; i < ResIndexArr.length; i++) {
				var imgpath = self.StylePath + ResIndexArr[i];
				cc.loader.loadRes(imgpath, cc.SpriteFrame, function (err, spriteFrame) {
					NodeList[i].getComponent(cc.Sprite).spriteFrame = spriteFrame;
					NodeList[i].attr({ Type: ResIndexArr[i], })
				});
			}
			self.scheduleOnce(function () {
				Util.CheckDiedMap();
			}, 1);
		},2000)
       

		
        //Util.ReCreateMap();
       // self.CheckMapisOK();
    },
    CheckMapisOK:function(){
        //在原生平台会出现最后两个item不显示，但是item存在，属性为空。如果发现该问题，调用地图重绘方法
        for(var i = 0;i<Global.Nodes.length;i++){
            console.log(Global.Nodes[i]);
            if(Global.Nodes[i].getComponent(cc.Sprite).spriteFrame == null){
                Util.ReCreateMap();
                break;
            }
        }
    }
});
