window.Storage = {};
Storage.ChangeCoin = function (Number) {
    var StorageCoin = Storage.GetCoin();
    StorageCoin += Number;
    cc.sys.localStorage.setItem('coin',StorageCoin);
},
Storage.GetCoin = function(){
    var StorageCoin = 600;
    var coin = cc.sys.localStorage.getItem('coin');
    if(!coin){
        StorageCoin = 600;
    }else{
        StorageCoin = parseInt(coin)
    }
    return StorageCoin;
},
Storage.Get_Level_Pass_Info = function(num){
    if(Global.Style == 0){
        //宠物版
        var levelinfo = cc.sys.localStorage.getItem('Pet'+num);
        if(!levelinfo){
            levelinfo = 0;
        }else{
            levelinfo = parseInt(levelinfo);
        }
        return levelinfo;
    }else if(Global.Style == 1){
        //水果版
        var levelinfo = cc.sys.localStorage.getItem('Fruit'+num);
        if(!levelinfo){
            levelinfo = 0;
        }else{
            levelinfo = parseInt(levelinfo);
        }
        return levelinfo;
    }else if(Global.Style == 2){
        //表情
        var levelinfo = cc.sys.localStorage.getItem('Emoji'+num);
        if(!levelinfo){
            levelinfo = 0;
        }else{
            levelinfo = parseInt(levelinfo);
        }
        return levelinfo;
    }
},
Storage.Set_Level_Pass_Info = function(num,starnum){
    if(Global.Style == 0){
        //宠物版
        cc.sys.localStorage.setItem('Pet'+num,starnum);
    }else if(Global.Style == 1){
        cc.sys.localStorage.setItem('Fruit'+num,starnum);
    }else if(Global.Style == 2){
        cc.sys.localStorage.setItem('Emoji'+num,starnum);
    }
},
Storage.Add_Level_Pass = function(){
    let Level_Pass_Now = Storage.Get_Level_Pass() + 1;
    if(Global.LevelNow >= Level_Pass_Now){
        if(Global.Style == 0){
            cc.sys.localStorage.setItem('Pet_Level_Pass',Level_Pass_Now);
        }else if(Global.Style == 1){
            cc.sys.localStorage.setItem('Fruit_Level_Pass',Level_Pass_Now);
        }else if(Global.Style == 2){
            cc.sys.localStorage.setItem('Emoji_Level_Pass',Level_Pass_Now);
        }
        
    }
    Storage.Maidian();
},
Storage.Maidian = function(){
	return;
    var xhr = cc.loader.getXMLHttpRequest();//创建XMLHttpRequest对象
    //cc.log("Status: Send Get Request to httpbin.org");
    //set arguments with <URL>?xxx=xxx&yyy=yyy
    //var UserName = null;
    var UserName = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getAndroidId", "()Ljava/lang/String;");
    var level = Global.LevelNow;
    var url ="https://yuestore.top/DreamGame/index.php/Home/Dream/addItem";//?UserName=ZengFeio&Level=9&GameName=CarLogo
    url=url+"?"+"UserName="+UserName+"&Level="+level+"&GameName=EmojiLink";
    console.log(url);
    xhr.open("GET", url, true);//设置和服务器交互的参数,True表示异步执行
    xhr.send(null);//调用了send方法后才会发出请求
    //并且get方式发送请求时send参数是null
},
Storage.Get_Level_Pass = function(){
    if(Global.Style == 0){
        //宠物版
        var Level_Pass = cc.sys.localStorage.getItem('Pet_Level_Pass');
        if(!Level_Pass){
            Global.Level_Pass = 0;
        }else{
            Global.Level_Pass = parseInt(Level_Pass);
        }
        return Global.Level_Pass;
    }else if(Global.Style == 1){
        //水果版
        var Level_Pass = cc.sys.localStorage.getItem('Fruit_Level_Pass');
        if(!Level_Pass){
            Global.Level_Pass = 0;
        }else{
            Global.Level_Pass = parseInt(Level_Pass);
        }
        return Global.Level_Pass;
    }else if(Global.Style == 2){
        //表情版
        var Level_Pass = cc.sys.localStorage.getItem('Emoji_Level_Pass');
        if(!Level_Pass){
            Global.Level_Pass = 0;
        }else{
            Global.Level_Pass = parseInt(Level_Pass);
        }
        return Global.Level_Pass;
    }
    
};
window.CheckPath = {};
CheckPath.Check = function(Start,End,TwoArrMap){
    if(CheckPath.Zero(Start,End,TwoArrMap)){
        Global.TwoCore = false;
        return true;
    }else if(CheckPath.One(Start,End,TwoArrMap)){
        Global.TwoCore = false;
        return true;
    }else if(CheckPath.Two(Start,End,TwoArrMap)){
        Global.TwoCore = true;
        return true;
    }
    return false;
},
CheckPath.TipCheck = function(Start,End,TwoArrMap){
    if(CheckPath.Zero(Start,End,TwoArrMap)){
        Global.TipTwoCore = false;
        return true;
    }else if(CheckPath.One(Start,End,TwoArrMap)){
        Global.TipTwoCore = false;
        return true;
    }else if(CheckPath.Two(Start,End,TwoArrMap)){
        Global.TipTwoCore = true;
        return true;
    }
    return false;
},
//false,buyong -1
CheckPath.Zero = function(Start,End,TwoArrMap){
    //检查是否不用转弯
    if(Start.raw == End.raw){
        //同一行
        var MinCol= Math.min(Start.col,End.col);
        var MaxCol = Math.max(Start.col,End.col);
        var length = 0;
        for (var j = MinCol+1; j<MaxCol; j++) {
            length = length+parseInt(TwoArrMap[Start.raw][j]);
        }
       
        if(length == 0){
            //可以连线
            if(!CheckPath.IsInArray(Global.LLKPath,Start)){
                Global.LLKPath.push(Start);
                //console.log(Global.LLKPath);
            }
            if(!CheckPath.IsInArray(Global.LLKPath,End)){
                Global.LLKPath.push(End);
                //ole.log(Global.LLKPath);
            }
            return true;
        }else{
            return false;
        }
    }else if(Start.col == End.col){
        var MinRaw = Math.min(Start.raw,End.raw);
        var MaxRaw = Math.max(Start.raw,End.raw);
        var length = 0;
        for (var j = MinRaw+1; j<MaxRaw; j++) {
            length = length+parseInt(TwoArrMap[j][Start.col]);
        }
       
        if(length == 0){
            //可以连线
            if(!CheckPath.IsInArray(Global.LLKPath,Start)){
                Global.LLKPath.push(Start);
                //console.log(Global.LLKPath);
            }
            if(!CheckPath.IsInArray(Global.LLKPath,End)){
                Global.LLKPath.push(End);
                //console.log(Global.LLKPath);
            }
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
},
CheckPath.One = function(Start,End,TwoArrMap){
    if(TwoArrMap[Start.raw][End.col] == '0'){
        var Mid = {raw:Start.raw,col:End.col};
        //判断这个拐角到Start和End是否可直连
        if(CheckPath.Zero(Start,Mid,TwoArrMap)){
            if(CheckPath.Zero(Mid,End,TwoArrMap)){
                return true
            }else{
                //清空Global
                Global.LLKPath = [];
            }
        } 
    }
    if(TwoArrMap[End.raw][Start.col] == '0' ){
        //判断这个拐角到Start和End是否可直连
        var Mid = {raw:End.raw,col:Start.col};
        //判断这个拐角到Start和End是否可直连
        if(CheckPath.Zero(Start,Mid,TwoArrMap)){
            if(CheckPath.Zero(Mid,End,TwoArrMap)){
                return true
            }else{
                //清空Global
                Global.LLKPath = [];
            }
        } 
    }
    return false;
},
	CheckPath.AddZeroTwoArrMap = function (TwoArrMap) {
		var Raw = TwoArrMap.length + 2;
		var Col = TwoArrMap[0].length + 2;
		var AddZeroMap = new Array();
		for (var i = 0; i < Raw; i++) {
			AddZeroMap[i] = new Array();
			for (var j = 0; j < Col; j++) {
				//第一行和最后一行全是0；
				if (i == 0 || i == Raw - 1) {
					AddZeroMap[i][j] = "0";
					continue;
				}
				if (j == 0 || j == Col - 1) {
					AddZeroMap[i][j] = "0";
					continue;
				}
				AddZeroMap[i][j] = Global.TwoArrMap[i - 1][j - 1];
			}
		}
		return AddZeroMap;
	},
CheckPath.Two = function(Start,End,TwoArrMap){
	var AddZeroMap = CheckPath.AddZeroTwoArrMap(TwoArrMap);
    //先情况Global
    Global.LLKPath = [];
    //console.log(AddZeroMap);
    //我们首先要将start和end点的坐标都加一
    var NewStat = {raw:Start.raw+1,col:Start.col+1};
    var NewEnd = {raw:End.raw+1,col:End.col+1};
    //水平扫描，从开始点到结束点，水平方向raw相同的点，能否有个点经过一拐到end点。
    for(var i = 0 ;i< AddZeroMap[0].length ; i++){
       // console.log(Global.LLKPath);
        if(AddZeroMap[NewStat.raw][i] == '0'){
            var Mid = {raw:NewStat.raw,col:i};
            //先判断Start到mid是否直连
            if(CheckPath.Zero(NewStat,Mid,AddZeroMap,true)){
                //如果直连
                if(CheckPath.One(Mid,NewEnd,AddZeroMap,true)){
                    return true;
                }else{
                    Global.LLKPath = [];
                }
            }
        }
    };
    //垂直扫描
    for(var i = 0;i<AddZeroMap.length;i++){
        if(AddZeroMap[i][NewStat.col] == '0'){
            var Mid = {raw:i,col:NewStat.col};
           
            //先判断Start到mid是否直连
            if(CheckPath.Zero(NewStat,Mid,AddZeroMap,true)){
                //如果直连
                if(CheckPath.One(Mid,NewEnd,AddZeroMap,true)){
                    return true;
                }else{
                    Global.LLKPath = [];
                }
            }
        }
    }
    return  false;
},
CheckPath.IsInArray = function(arr,val){
    for(var i = 0; i < arr.length ; i++){
        if(val == arr[i]){
            return true;
        }
    }
    return false;
} 
window.Util = {};
Util.removeFromArrByValue = function(arr, val) {
    for(var i=0; i<arr.length; i++) {
      if(arr[i] == val) {
        arr.splice(i, 1);
        break;
      }
    }
},
Util.CheckDiedMap = function(){
    var NodeNum = Global.Nodes.length;
    var Start = null;
    var End = null;
    var TypeArr = Global.Nodes;

        for(var i = 0; i< TypeArr.length ;i++){
            for(var j = 0; j<NodeNum;j++){
                if(TypeArr[i].Type == Global.Nodes[j].Type && TypeArr[i] != Global.Nodes[j]){
                    if(CheckPath.TipCheck(TypeArr[i].ad,Global.Nodes[j].ad,Global.TwoArrMap)){
                       // console.log('可否相连：',CheckPath.Check(TypeArr[i],Global.Nodes[j],Global.TwoArrMap));
                        console.log(TypeArr[i].ad,Global.Nodes[j].ad);
                        Global.TipStart = TypeArr[i];
                        Global.TipEnd = Global.Nodes[j];
                        Global.LLKPath = [];
                        return true;
                    }
                }
            }
        }
    Util.ReCreateMap();//false直接调用重绘地图
    return false;
},
Util.ReCreateMap = function(){
    var Raw = Global.TwoArrMap.length;
    var col = Global.TwoArrMap[0].length;
    //var MainArea = cc.find('Canvas/MainArea');
    //MainArea.removeAllChildren();js按引用传值，清空后会将Global.Nodes清空，故设计为active为false
    for(var i = 0;i<Global.Nodes.length;i++){
        Global.Nodes[i].active = false;
    }
    var NodeIndex = -1;
    var newnodes = Util.shuffleArray(Global.Nodes);
    for(var i = 0; i < Raw; i++){
        for(var j = 0; j < col ; j++){
            if(Global.TwoArrMap[i][j] == "1"){
                NodeIndex++;
                newnodes[NodeIndex].y = i*Global.SideLength;
                newnodes[NodeIndex].x = j*Global.SideLength;
                //修改节点携带的位置属性
                newnodes[NodeIndex].attr({ ad:{raw: i,col:j} })
                newnodes[NodeIndex].active = true;
            }
        }
    }
    //重绘之后需要检查死局
    Util.CheckDiedMap();
},
Util.shuffleArray = function(array) {
    for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
},
window.Audio = {};
Audio.playmusic = function(Audio_url){
    if(Global.NoSound == false){
        cc.loader.load(cc.url.raw(Audio_url), function (err, clip) {
            cc.audioEngine.playMusic(clip, true);
            cc.audioEngine.setMusicVolume(0.3);
        });
    }
},
Audio.playeffect = function(Audio_url){
    if(Global.NoSound == false){
        cc.loader.load(cc.url.raw(Audio_url), function (err, clip) {
            cc.audioEngine.playEffect(clip, false);
            cc.audioEngine.setEffectsVolume(1);
        });
    }
}
