
cc.Class({
    extends: cc.Component,

    properties: {
        work: {
            default: null,
            type: cc.SpriteFrame,
            serializable: true,
        },
        sleep: {
            default: null,
            type: cc.SpriteFrame,
            serializable: true,
        },



        dogBasinFood: {
            default: null,
            type: cc.SpriteFrame,
            serializable: true,
        },

        dogBasin: {
            default: null,
            type: cc.SpriteFrame,
            serializable: true,
        },

        DogNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },

        DogSliderNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },

        DogSliderLabel: {
            default: null,
            type: cc.Label,
            serializable: true,
        },

        basinNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },

        basinSliderNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },

        basinSliderLabel: {
            default: null,
            type: cc.Label,
            serializable: true,
        },

        showButtonLabel: {
            default: null,
            type: cc.Label,
            serializable: true,
        },

        infoBoxNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },

        //是否是自家的狗
        bSelfDog: {
            default: true,
            visible: false
        },

        buyDogFoodPrefab: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        }

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.onGetSelfList();
    },

    start() {

        this.basinNode.on(cc.Node.EventType.TOUCH_START, () => {

            console.log("点击添加狗粮,this.bSelfDog =", this.bSelfDog);
            if (!this.bSelfDog) return;

            //生成预制件
            let _foodPanel = cc.instantiate(this.buyDogFoodPrefab);
            let parent = cc.find('Canvas/UICamera/DAPPContainer');
            _foodPanel.parent = parent;
            _foodPanel.setPosition(0, 0);

            //把操作对象传入
            let _buyDogInfoScript = _foodPanel.getComponent("BuyDogInfo");
            _buyDogInfoScript.parentNode = this;
            _buyDogInfoScript.onLoadFoodList();

        })
    },

    /**
     * 直接更新狗的状态
     * @param {狗对象}} dog 
     */
    onSetSelfDogState(dog) {
        this.bSelfDog = true;
        GlobalD.Dog = dog;
        this.infoBoxNode.active = true;
        this.DogNode.active = true;
        this._updateDogState(GlobalD.Dog);
    },
    onGetSelfList() {
        // let ManageUI = cc.find("GameNode/ManageUI").getComponent("ManageUI");
        this.bSelfDog = true;
        //获取狗的信息
        this._updateList({});

    },
    //获取其他用户狗的信息
    onGetOtherUserList() {
        this.bSelfDog = false;
        this._updateList({ otherUserId: GlobalD.OtherUserInfo.userId });
    },
    _updateList(data) {

        this.infoBoxNode.active = false;
        this._resetDogState();

        GlobalD.GameData.onGetEquipmentListState(data, (value) => {
            console.log(value);
            let _list = value.data;

            for (let i = 0; i < _list.length; i++) {
                if (0 === _list[i].otherType) {

                    GlobalD.Dog = _list[i];
                    //存在狗
                    if (this.bSelfDog) {
                        this.infoBoxNode.active = true;
                        this.DogNode.active = true;
                    } else {
                        this.DogNode.active = 1 === GlobalD.Dog.isShow ? true : false;
                    }
                    if (this.DogNode.active) {
                        this._updateDogState(GlobalD.Dog);
                    }
                } else if (1 === _list[i].otherType) {
                    GlobalD.Stick = _list[i];
                    if (GlobalD.Stick.remainingDay > 0)
                        GlobalD.game._ManageUIScript.onSettingStickAlpha(255);
                }
            }


        })
    },
    _resetDogState() {
        GlobalD.Dog = null;
        GlobalD.Stick = null;
        this.DogNode.active = false;
        this.basinNode.getComponent(cc.Sprite).spriteFrame = this.dogBasin;
        GlobalD.game._ManageUIScript.onSettingDogAlpha(125);
        GlobalD.game._ManageUIScript.onSettingStickAlpha(125);
    },
    //切换小狗状态
    onSwitchDogActive() {
        console.log(GlobalD.Dog);
        GlobalD.GameData.onSetDogShowState({ isShow: 1 === GlobalD.Dog.isShow ? 0 : 1 }, (value) => {
            console.log(value.data);
            GlobalD.Dog = value.data.dog;
            //修改按钮提示文字
            this.showButtonLabel.string = 1 === GlobalD.Dog.isShow ? "隐藏小狗" : "显示小狗"

        })
    },

    _updateDogState(dogValue) {

        this.DogSliderLabel.string = "剩" + dogValue.remainingDay + "天";
        this.basinSliderLabel.string = "剩余" + dogValue.remainingConsumption + "G";

        //狗的剩余天数进度
        let _dogSliderNodeScript = this.DogSliderNode.getComponent("slider_progress");
        let _dogSliderProccess = 0 === dogValue.totalDay ? 0 : dogValue.remainingDay / dogValue.totalDay;
        _dogSliderNodeScript.onSetProcgress(_dogSliderProccess);

        //狗粮的剩余进度
        let _basinSliderNodeScript = this.basinSliderNode.getComponent("slider_progress");
        let _basinSliderProccess = 0 === dogValue.totalConsumption ? 0 : dogValue.remainingConsumption / dogValue.totalConsumption;
        _basinSliderNodeScript.onSetProcgress(_basinSliderProccess);


        if (dogValue.remainingDay > 0 && dogValue.remainingConsumption > 0) {
            this.basinNode.getComponent(cc.Sprite).spriteFrame = this.dogBasinFood;
            this.DogNode.getComponent(cc.Sprite).spriteFrame = this.work;
            GlobalD.game._ManageUIScript.onSettingDogAlpha(255);
        } else {
            //不够狗粮，睡觉
            this.basinNode.getComponent(cc.Sprite).spriteFrame = this.dogBasin;
            this.DogNode.getComponent(cc.Sprite).spriteFrame = this.sleep;
            GlobalD.game._ManageUIScript.onSettingDogAlpha(125);
        }
    },

});
