cc.Class({
    extends: cc.Component,

    properties: {
        Env: {
            default: null,
            type: cc.Node,
        },
        Labour: {
            default: null,
            type: cc.Node,
        },
        Shop: {
            default: null,
            type: cc.Node,
        },
        SpeBuilding: {
            default: null,
            type: cc.Node,
        },
        Seed: {
            default: null,
            type: cc.Node,
        },
        Fruit: {
            default: null,
            type: cc.Node,
        },
        EvnBtn: {
            default: null,
            type: cc.Node,
        },
        LabourBtn: {
            default: null,
            type: cc.Node,
        },
        ShopBtn: {
            default: null,
            type: cc.Node,
        },
        SpeBuildingBtn: {
            default: null,
            type: cc.Node,
        },
        SeedBtn: {
            default: null,
            type: cc.Node,
        },
        FruitBtn: {
            default: null,
            type: cc.Node,
        },

        SelectedFrame: cc.Node,
        Building00: cc.Node,
        Building10: cc.Node,
        Building20: cc.Node,
        Building30: cc.Node,

        Building40: cc.Node,
        Building50: cc.Node,
        ManageUI: cc.Node,


        //四个按钮对应的sprite
        Normal_Env_Sprite: cc.SpriteFrame,
        Selecteded_Env_Sprite: cc.SpriteFrame,

        Normal_Labour_Sprite: cc.SpriteFrame,
        Selecteded_Labour_Sprite: cc.SpriteFrame,

        Normal_Shop_Sprite: cc.SpriteFrame,
        Selecteded_Shop_Sprite: cc.SpriteFrame,

        Normal_Spe_Sprite: cc.SpriteFrame,
        Selecteded_Spe_Sprite: cc.SpriteFrame,


        Normal_Seed_Sprite: cc.SpriteFrame,
        Selecteded_Seed_Sprite: cc.SpriteFrame,

        Normal_Fruit_Sprite: cc.SpriteFrame,
        Selecteded_Fruit_Sprite: cc.SpriteFrame,

        //需要切换节点顺序的父节点
        container: cc.Node,

        //商城的列表
        mySeedList: [],
        myFruitList: [],
        seedPrefab: cc.Prefab,
        fruitPrefab: cc.Prefab,

        seedSpriteFrame: {
            default: [],
            type: [cc.SpriteFrame]
        },
        fruitSpriteFrame: {
            default: [],
            type: [cc.SpriteFrame]
        },
    },
    HiddenAll() {

        if (this.Env.active)
            this.Env.active = false;
        if (this.Labour.active)
            this.Labour.active = false;
        if (this.Shop.active)
            this.Shop.active = false;
        if (this.SpeBuilding.active)
            this.SpeBuilding.active = false;
        if (this.Seed.active)
            this.Seed.active = false;
        if (this.Fruit.active)
            this.Fruit.active = false;
    },
    start() {
        //默认设置第一个节点为 最上面
        this.EvnBtn.setSiblingIndex(10);

        this.onUpdateList();

    },

    onUpdateList() {
        //先判断container下面是否存在Seed
        // this.Seed.destroyAllChildren;
        // console.log("this.Seed.children:", this.Seed.children);
        //清除种子的预制
        let _children = this.Seed.children;
        for (let i = _children.length - 1; i > 0; i--) {
            _children[i].destroy();
        }
        //清除仓库的预制
        let _fruitChildren = this.Fruit.children;
        for (let i = _fruitChildren.length - 1; i > 0; i--) {
            _fruitChildren[i].destroy();
        }
        // console.log("this.Seed.children2:", this.Seed.children);

        //获取种子和果实
        GlobalD.GameData.getWarehouseSeedAndFruit((res, vaule) => {
            this.mySeedList = vaule.data.seed;
            this.myFruitList = vaule.data.fruit;

            console.log(this.mySeedList, this.myFruitList);
            for (let i = 0; i < this.mySeedList.length; i++) {
                //没有种子数量不显示 todo 看看后续需不需要处理删除
                if (this.mySeedList[i].amount < 1) continue;
                
                let _seed = cc.instantiate(this.seedPrefab);
                _seed.parent = this.Seed;
               
                //if (this.mySeedList[i].amount == 0)
                //{
                //    _seed.getChildByName("SellOut").active = 0;
                //}

                let _seedScript = _seed.getComponent("Content_Button");
                _seedScript.Name = this.mySeedList[i].name;
                _seedScript.Price = 0;
                _seedScript.SNB = this.mySeedList[i].priceSnb;
                _seedScript.Synopsis = "成熟期:" + this.mySeedList[i].maturity + "天," + this.mySeedList[i].seedDescribe;

                //设置一个生成点
                let _spawnScript = _seed.getComponent("Content_seed");
                _spawnScript.SpawnPoint = cc.find("Canvas/SpawnParent/SpawnPoint");
                _spawnScript.goodsSeedInfo = this.mySeedList[i];//这里传递一个生成的种子信息过去

                _seedScript.NumLabel.string = this.mySeedList[i].amount;
                _seedScript.NameLabel.string = this.mySeedList[i].name;
                switch (this.mySeedList[i].picture) {
                    case "Cabbage":
                        _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[0];
                        _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[0];
                        break;
                    case "Potato":
                        _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[1];
                        _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[1];
                        break;
                    case "Carrot":
                        _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[2];
                        _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[2];
                        break;
                    case "Broccoli":
                        _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[3];
                        _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[3];
                        break;
                    case "Tomato":
                        _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[4];
                        _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[4];
                        break;
                    case "Squash":
                        _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[5];
                        _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[5];
                        break;
                    case "Eggplant":
                        _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[6];
                        _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[6];
                        break;
                    case "Pepper":
                        _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[7];
                        _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[7];
                        break;
                    case "Lentil":
                        _seedScript.bgSprite.spriteFrame = this.seedSpriteFrame[8];
                        _spawnScript.fruitSpriteFrame = this.fruitSpriteFrame[8];
                        break;
                    default:
                        break;
                }
                //获取一下种子图片
                _spawnScript.seedSpriteFrame = _seedScript.bgSprite.spriteFrame;



            }
            for (let i = 0; i < this.myFruitList.length; i++) {
                let _fruit = cc.instantiate(this.fruitPrefab);
                _fruit.parent = this.Fruit;
                _fruit.zIndex = 999;
                let _fruitScript = _fruit.getComponent("Content_Button");
                _fruitScript.NumLabel.string = this.myFruitList[i].amount;
                _fruitScript.NameLabel.string = this.myFruitList[i].name;
                _fruitScript.Name = this.myFruitList[i].name;
                _fruitScript.SNB = this.myFruitList[i].priceSnb;
                _fruitScript.Synopsis = this.myFruitList[i].fruitDescribe;

                //if (this.myFruitList[i].amount == 0)
                //{
                //    _fruit.getChildByName("SellOut").active = 0;
                //}

                //设置果实信息
                let _contentFruit = _fruit.getComponent("Content_fruit");
                _contentFruit.fruitInfo = this.myFruitList[i];

                switch (this.myFruitList[i].picture) {
                    case "Cabbage":
                        _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[0];
                        break;
                    case "Potato":
                        _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[1];
                        break;
                    case "Carrot":
                        _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[2];
                        break;
                    case "Broccoli":
                        _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[3];
                        break;
                    case "Tomato":
                        _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[4];
                        break;
                    case "Squash":
                        _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[5];
                        break;
                    case "Eggplant":
                        _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[6];
                        break;
                    case "Pepper":
                        _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[7];
                        break;
                    case "Lentil":
                        _fruitScript.bgSprite.spriteFrame = this.fruitSpriteFrame[8];
                        break;
                    default:
                        break;
                }

            }
        });
    },

    //切换建筑的面板
    onSwitchBuildingContent(event, index) {

        this.EvnBtn.setScale(1);
        this.LabourBtn.setScale(1);
        this.ShopBtn.setScale(1);
        this.SpeBuildingBtn.setScale(1);
        this.SeedBtn.setScale(1);
        this.FruitBtn.setScale(1);

        this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Env_Sprite;
        this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Labour_Sprite;
        this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Shop_Sprite;
        this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Spe_Sprite;
        this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Seed_Sprite;
        this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Normal_Fruit_Sprite;

        this.EvnBtn.setContentSize(100, 54);
        this.LabourBtn.setContentSize(100, 54);
        this.ShopBtn.setContentSize(100, 54);
        this.SpeBuildingBtn.setContentSize(100, 54);
        this.SeedBtn.setContentSize(100, 54);
        this.FruitBtn.setContentSize(100, 54);

        this.EvnBtn.y = -5;
        this.LabourBtn.y = -5;
        this.ShopBtn.y = -5;
        this.SpeBuildingBtn.y = -5;
        this.SeedBtn.y = -5;
        this.FruitBtn.y = -5;

        this.HiddenAll();
        //面板设置index
        //环境
        if ('0' == index) {
            this.EvnBtn.y = -20;
            this.EvnBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Env_Sprite;
            this.Env.active = true;
            // this.SelectedFrame.parent = this.Building00;
            this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building00.getComponent('Content_Button').Name, this.Building00.getComponent('Content_Button').Price, this.Building00.getComponent('Content_Button').Synopsis);

            //设置节点顺序
            this.EvnBtn.setSiblingIndex(10);
            this.EvnBtn.setContentSize(110, 100);
        }
        //劳动 
        else if ('1' == index) {
            this.LabourBtn.y = -20;
            this.LabourBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Labour_Sprite;
            this.Labour.active = true;
            // this.SelectedFrame.parent = this.Building10;
            this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building10.getComponent('Content_Button').Name, this.Building10.getComponent('Content_Button').Price, this.Building10.getComponent('Content_Button').Synopsis);

            this.LabourBtn.setSiblingIndex(10);
            // this.LabourBtn.setContentSize(86, 88);
            this.LabourBtn.setContentSize(110, 100);
        }
        //商铺
        else if ('2' == index) {
            this.ShopBtn.y = -20;
            this.ShopBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Shop_Sprite;
            this.Shop.active = true;
            // this.SelectedFrame.parent = this.Building20;
            this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building20.getComponent('Content_Button').Name, this.Building20.getComponent('Content_Button').Price, this.Building20.getComponent('Content_Button').Synopsis);

            this.ShopBtn.setSiblingIndex(10);
            this.ShopBtn.setContentSize(110, 100);

        }
        //特殊
        else if ('3' == index) {
            this.SpeBuildingBtn.y = -20;
            this.SpeBuildingBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Spe_Sprite;
            this.SpeBuilding.active = true;
            // this.SelectedFrame.parent = this.Building30;
            this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.Building30.getComponent('Content_Button').Name, this.Building30.getComponent('Content_Button').Price, this.Building30.getComponent('Content_Button').Synopsis);

            this.SpeBuildingBtn.setSiblingIndex(10);
            this.SpeBuildingBtn.setContentSize(110, 100);
        }
        //种子
        else if ('4' == index) {
            this.SeedBtn.y = -20;
            this.SeedBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Seed_Sprite;
            this.Seed.active = true;
            // this.SelectedFrame.parent = this.Building40;

            if (this.mySeedList.length > 0) {
                let Synopsis = "成熟期：" + this.mySeedList[0].maturity + "天," + this.mySeedList[0].seedDescribe;
                this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.mySeedList[0].name, 0, Synopsis);
            }

            this.SeedBtn.setSiblingIndex(10);
            this.SeedBtn.setContentSize(110, 100);
        }
        //果实
        else if ('5' == index) {
            this.FruitBtn.y = -20;
            this.FruitBtn.getComponent(cc.Sprite).spriteFrame = this.Selecteded_Fruit_Sprite;
            this.Fruit.active = true;
            // this.SelectedFrame.parent = this.Building50;

            if (this.myFruitList.length > 0) {
                let Synopsis = this.myFruitList[0].fruitDescribe;
                // "出售价格：" + this.SNB + " SNB";
                this.ManageUI.getComponent('ManageUI').onSetButtonInfo(this.myFruitList[0].name, "出售价格：" + this.myFruitList[0].priceSnb + " SNB", Synopsis);
            }

            this.FruitBtn.setSiblingIndex(10);
            this.FruitBtn.setContentSize(110, 100);
        }


        //切换节点顺序
        // this.switch();
    },

    switch: function () {
        var children = this.container.children;
        var length = children.length;
        if (length > 1) {
            var src = Math.floor(Math.random() * length);
            var node = children[src];
            var dst = src === length - 1 ? 0 : src + 1;
            node.setSiblingIndex(dst);
        }
    },
});
