var reGameStates = require('GameStates');


cc.Class({
    extends: cc.Component,

    properties: {
        stateArray: {

            default: [],
            type: cc.SpriteFrame,
            serializable: true,
        },
        workArray: {

            default: [],
            type: cc.SpriteFrame,
            serializable: true,
        },
        changeSprite: {
            default: null,
            type: cc.Sprite,
            serializable: true,
        },


        //工作状态
        workingStatus: {
            default: null,
            type: cc.Node,
            serializable: true,
        },

        workPlace: {
            default: reGameStates.BuildType.Farmland,
            type: cc.Enum(reGameStates.BuildType),
            serializable: true,
        },
    },
    start() {

        if (this.workPlace == reGameStates.BuildType.TimberYard) {
            // this.onChangeSpriteFromGrow();
        }
    },


    onChangeFromAIWorker(_workPlace, _consume) {
        // cc.log('this.workPlace',_workPlace,_consume);

        if (_workPlace == reGameStates.BuildType.Farmland) {
            this.onChangeSpriteFromWork(_consume);
        } else if (_workPlace == reGameStates.BuildType.TimberYard) {
            this.onChangeSpriteFromTimberYardWork(_consume);

        } else if (_workPlace == reGameStates.BuildType.MiningPit) {
            this.onChangeSpriteFromWork(_consume);

        }
    },

    // 工作时候
    onChangeSpriteFromWork(consume) {

        if (consume <= 0 || consume >= 50) {
            this.workingStatus.active = false;
        } else if (!this.workingStatus.active) {
            this.workingStatus.active = true
        }
        let index = 50 - consume;
        /*
        //这部分是旧的代码
        if (index < 20 && index > 10)
            // this.changeSprite.spriteFrame = this.workArray[0];
            this.changeSprite.spriteFrame = this.workArray[0];
        else if (index >= 20 && index <= 35)
            this.changeSprite.spriteFrame = this.workArray[1];
        else if (index > 35)
            this.changeSprite.spriteFrame = this.workArray[2];
        */
        if (index < 20 && index > 10) {
            this.changeSprite.spriteFrame = this.workArray[0];
            // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            // this.changeSprite.node.width = this.workArray[0].getRect().width;
            // this.changeSprite.node.height = this.workArray[0].getRect().height;
        }
        else if (index >= 20 && index <= 35) {
            this.changeSprite.spriteFrame = this.workArray[1];
            // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            // this.changeSprite.node.width = this.workArray[1].getRect().width;
            // this.changeSprite.node.height = this.workArray[1].getRect().height;
        }
        else if (index > 35) {
            this.changeSprite.spriteFrame = this.workArray[2];
            // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            // this.changeSprite.node.width = this.workArray[2].getRect().width;
            // this.changeSprite.node.height = this.workArray[2].getRect().height;
        }

    },


    onChangeSpriteFromTimberYardWork(consume) {
        if (consume <= 0 || consume >= 50) {
            this.workingStatus.active = false;
        } else if (!this.workingStatus.active) {
            this.workingStatus.active = true
        }
        let index = 50 - consume;
        /*
        //这部分是旧的代码
        if (index < 25 && index > 10)
            this.changeSprite.spriteFrame = this.workArray[0];
        else if (index >= 25 && index <= 40)
            this.changeSprite.spriteFrame = this.workArray[1];
        else if (index > 40)
            this.changeSprite.spriteFrame = this.workArray[2];
        */

        if (index < 25 && index > 10) {
            this.changeSprite.spriteFrame = this.workArray[0];
            // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            // this.changeSprite.node.width = this.workArray[0].getRect().width;
            // this.changeSprite.node.height = this.workArray[0].getRect().height;
            // cc.log(this.workArray[0].getRect().width);
        }
        else if (index >= 25 && index <= 40) {
            this.changeSprite.spriteFrame = this.workArray[1];
            // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            // this.changeSprite.node.width = this.workArray[1].getRect().width;
            // this.changeSprite.node.height = this.workArray[1].getRect().height;
        }
        else if (index > 40) {
            this.changeSprite.spriteFrame = this.workArray[2];
            // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            // this.changeSprite.node.width = this.workArray[2].getRect().width;
            // this.changeSprite.node.height = this.workArray[2].getRect().height;
        }
    },

    // 运输时候调用
    onChangeSpriteFromGrow() {

        this.count = 0;
        this.callback = function () {
            if (this.count === this.stateArray.length - 1) {
                // 在第六次执行回调时取消这个计时器
                this.unschedule(this.callback);
            }
            this.onTreeGrowth(this.count);
            this.count++;
        }
        this.schedule(this.callback, 0.5);
    },
    /**
     * 神龙田地的直接生长
     */
    onHolyFarmlandSeedFromGrow() {
        this.count = 0;
        this.callback = function () {
            if (this.count === this.stateArray.length - 1) {
                // 在第六次执行回调时取消这个计时器
                this.unschedule(this.callback);
            }
            this.onTreeGrowth(this.count);
            this.count++;
        }
        this.schedule(this.callback, 0.5);
    },
    // 生长
    onTreeGrowth(count) {
        this.changeSprite.spriteFrame = this.stateArray[count];
    },

    /**
     * 神龙田地的直接 生长图片设置
     */
    onSetGrow(growStage,harvestSpriteFrame){
        // console.log(growStage,harvestSpriteFrame);
        if(growStage === 0 || growStage === 1 || growStage === 2){
            this.changeSprite.spriteFrame = this.stateArray[growStage];
        }else{
            this.changeSprite.spriteFrame = harvestSpriteFrame;
        }
   
    },

    onResetSprite(_workPlace) {
        if (_workPlace == reGameStates.BuildType.Farmland) {
            // this.changeSprite.spriteFrame = this.stateArray[0];
            this.changeSprite.spriteFrame = this.stateArray[0];
            this.onChangeSpriteFromGrow();
        } else if (_workPlace == reGameStates.BuildType.TimberYard) {
            //运输时候木材清空
            this.changeSprite.spriteFrame = this.workArray[3];

            this.onChangeSpriteFromGrow();

        } else if (_workPlace == reGameStates.BuildType.MiningPit) {
            this.changeSprite.spriteFrame = this.stateArray[0];
        }
    },

    //设置当前的完成状态

    //读取数据时候设置
    //todo....
    onSetWorkStateFormReadingData(_Index) {
        this.changeSprite.spriteFrame = this.workArray[_Index];
    },
});
