cc.Class({
    extends: cc.Component,

    properties: {
        ZhuangBei:cc.Node,
        JueSe:cc.Node,
        DaoJu:cc.Node,
        MiaoShu:cc.Label,
        MingZi:cc.Label,
        content:cc.Node,
        BiaoShi:cc.Node,
        KaiGuan:cc.Node,
        ShiYong:cc.Label,
    },

    onLoad: function () {
        this.hehehe = require("data_1").zhuangbeicao;
        this.hahaha = require("data_1").zblei;
        this.caozuo = 0;
    },

    test:function(){
        var a = parseInt(1 + Math.random()*8);
        this.creator(a);
    },

    creator:function(num){
        for(let i in this.hahaha){
            let Sp = this.ZhuangBei._children[i].getComponent(cc.Sprite);
            if(this.caozuo == 1){
                Sp.spriteFrame = new cc.SpriteFrame(cc.url.raw("resources/" + this.hahaha[i] + ".png"));
            }
            if(this.hahaha[i] == 0 && this.caozuo == 0){
                Sp.spriteFrame = new cc.SpriteFrame(cc.url.raw("resources/" + num + ".png"));
                this.hahaha[i] = num;
                break;
            }
        }
        this.caozuo = 0;
    },
    
    click:function(event,num){
        var suit = ["Helmet", "arms", "Breastplate", "shoes", "belt", "glove"];
        var emmmm = require("data_1").ZB;
        for(let i in emmmm){        
            if(this.hahaha[num-1] == emmmm[i].id){
                this.MiaoShu.string = emmmm[i].describe;
                this.MingZi.string = i;
                this.BiaoShi.x = event.target.x+15;
                this.BiaoShi.y = event.target.y-15;
                for(let j=0; j<6; j++){
                    if(suit[j] == emmmm[i].Position){
                        this.JueSe._children[0].x = this.JueSe._children[j+2].x;  
                        this.JueSe._children[0].y = this.JueSe._children[j+2].y;
                        this.usenumber = num - 1;
                        this.usenumber1 = j;
                        this.usenumber2 = emmmm[i].id;
                        this.content._contentSize.height = this.MiaoShu.node._contentSize.height;
                        this.useOrunload = 0;
                        this.ShiYong.string = "使用";
                        return;
                    }
                }
            }
        }
    },

    click2:function(event,num){
        var emmmm = require("data_1").ZB;
        num = parseInt(num);
        for(let i in emmmm){
            if(this.hehehe[num-1] == emmmm[i].id){
                this.MiaoShu.string = emmmm[i].describe;
                this.MingZi.string = i;
                this.JueSe._children[0].x = this.JueSe._children[num+1].x;  
                this.JueSe._children[0].y = this.JueSe._children[num+1].y;
                this.useOrunload = 1;
                this.anhao = num+1;
                this.anhao2 = emmmm[i].id;
                this.ShiYong.string = "卸下";
                return;
            }
        }
        
    },

    use:function(event,num){
        if(this.useOrunload == 1){
            if(this.anhao == null) return;
            //用来判断血量  是否可以脱装备    if(this.heroLife <= this.equipmentLife) return;
            //背包满了就不让脱  if(this.hahaha.length == 11) return;
            let Sp = this.JueSe._children[this.anhao].getComponent(cc.Sprite);
            Sp.spriteFrame = new cc.SpriteFrame(cc.url.raw("resources/" + "00" + ".png"));
            this.hehehe[this.anhao-2] = 0;
            for(let i in this.hahaha){
                if(this.hahaha[i] == 0){
                    this.hahaha[i] = this.anhao2;
                    break;
                }
            }
            this.biandong();
        }
        if(this.useOrunload == 0){
            if(this.usenumber == null)  return;
            for(let k=2; k<8; k++){
                if(k-2 == this.usenumber1){
                    let Sp = this.JueSe._children[k].getComponent(cc.Sprite);
                    Sp.spriteFrame = new cc.SpriteFrame(cc.url.raw("resources/" + this.hahaha[this.usenumber] + ".png"));
                }
            }
            if(this.hehehe[this.usenumber1] == 0){
                for(let i=0; i<this.hahaha.length; i++){
                    if(i == this.usenumber){
                        this.hahaha[i] = 0;
                        for(let j=i; j<this.hahaha.length; j++){
                            this.hahaha[j] = this.hahaha[j+1];
                            if(j == 11) this.hahaha[j] = 0;
                        }
                    }
                }
            }
            else this.hahaha[this.usenumber] = this.hehehe[this.usenumber1];
            this.hehehe[this.usenumber1] = this.usenumber2;
            this.biandong();
        }
        this.caozuo = 1;
        this.BiaoShi.x = 8888;
        this.JueSe._children[0].x = 8888;
        this.usenumber = null;
        this.creator();
    },

    arrangement:function(){
        for(let i=0; i<this.hahaha.length; i++){
            for(let j=i+1; j<=this.hahaha.length; j++){
                if(this.hahaha[i]<this.hahaha[j]){
                    this.hahaha[i] = this.hahaha[i]^this.hahaha[j];
                    this.hahaha[j] = this.hahaha[j]^this.hahaha[i];
                    this.hahaha[i] = this.hahaha[i]^this.hahaha[j];
                }
            }
        }
        this.caozuo = 1;
        this.creator();
    },


    decompose:function(){
        if(this.useOrunload == 0){
            this.hahaha[this.usenumber] == 0;
        }
        if(this.useOrunload == 1){
            let Sp = this.JueSe._children[this.anhao].getComponent(cc.Sprite);
            Sp.spriteFrame = new cc.SpriteFrame(cc.url.raw("resources/" + "00" + ".png"));
            this.hehehe[this.anhao-2] = 0;
        }
        this.cailiaohuode();
        this.creator();
    },

    cailiaohuode:function(){
        //this.gold += 00000;
    },

    biandong:function(){
        /////////
    },



    switch_:function(){
        if(this.KaiGuan.active) this.KaiGuan.active = false;
        else this.KaiGuan.active = true;
        this.BiaoShi.x = 8888;
        this.JueSe._children[0].x = 8888;
    },

    save:function(){
        cc.sys.localStorage.setItem("savav", JSON.stringify(this.hahaha));
    },

    load:function(){
        this.hahaha = JSON.parse(cc.sys.localStorage.getItem("savav"));
        this.caozuo = 1;
        this.creator();
    },
});
