var reGameStates = require('GameStates');

cc.Class({
    extends: cc.Component,

    properties: {
        //显示计算日期的label
        DateLabel: cc.Node,
        ManagerWorker: cc.Node,
        Tips: cc.Node,
    },
    Init() {
        this.CurrentMonth = GlobalD.GameData.GameMonth;
        this.CurrentYear = GlobalD.GameData.GameYear;
        this.SetDate();

        //Update new day
        // this.DateLabel.getComponent(cc.Label).string = GlobalD.GameData.GameYear.toString() + '年 : ' + GlobalD.GameData.GameMonth.toString() + '月 : ' + GlobalD.GameData.GameDay.toString() + '日';

        var myDate = new Date();
        myDate.setFullYear(GlobalD.GameData.GameYear, GlobalD.GameData.GameMonth, GlobalD.GameData.GameDay);

        var tempGameMonth = myDate.getMonth();
        var tempGameYear = myDate.getFullYear();

        this.schedule(function () {
            myDate.setDate(myDate.getDate() + 1);
            GlobalD.GameData.GameYear = myDate.getFullYear();
            GlobalD.GameData.GameMonth = myDate.getMonth();
            GlobalD.GameData.GameDay = myDate.getDate();
            cc.sys.localStorage.setItem('GameYear', GlobalD.GameData.GameYear);
            cc.sys.localStorage.setItem('GameMonth', GlobalD.GameData.GameMonth);
            cc.sys.localStorage.setItem('GameDay', GlobalD.GameData.GameDay);

            if (GlobalD.GameData.GameYear != 0) {
                this.DateLabel.getComponent(cc.Label).string = GlobalD.GameData.GameYear.toString() + '年 : ' + GlobalD.GameData.GameMonth.toString() + '月 : ' + GlobalD.GameData.GameDay.toString() + '日';
            }
            else if (GlobalD.GameData.GameMonth != 0) {
                this.DateLabel.getComponent(cc.Label).string = GlobalD.GameData.GameMonth.toString() + '月 : ' + GlobalD.GameData.GameDay.toString() + '日';
            }
            else {
                this.DateLabel.getComponent(cc.Label).string = GlobalD.GameData.GameDay.toString() + '日';
            }

            if (tempGameMonth != myDate.getMonth()) {
                tempGameMonth = myDate.getMonth();
            }

            //过了一个月
            if (this.CurrentMonth != GlobalD.GameData.GameMonth) {
                this.CurrentMonth = GlobalD.GameData.GameMonth;
                this.MonthEvent();
            }
            //过了一年
            if (this.CurrentYear != GlobalD.GameData.GameYear) {
                this.CurrentYear = GlobalD.GameData.GameYear;
                this.YearEvent();
            }

        }.bind(this), 5, cc.repeatForever());
    },
    SetDate() {
        if (GlobalD.GameData.GameYear != 0) {
            this.DateLabel.getComponent(cc.Label).string = GlobalD.GameData.GameYear.toString() + '年 : ' + GlobalD.GameData.GameMonth.toString() + '月 : ' + GlobalD.GameData.GameDay.toString() + '日';
        }
        else if (GlobalD.GameData.GameMonth != 0) {
            this.DateLabel.getComponent(cc.Label).string = GlobalD.GameData.GameMonth.toString() + '月 : ' + GlobalD.GameData.GameDay.toString() + '日';
        }
        else {
            this.DateLabel.getComponent(cc.Label).string = GlobalD.GameData.GameDay.toString() + '日';
        }
    },
    MonthEvent() {
        //每月扣费
        this._AllBuildingRuningCost();
        this.WorkerSalary();
        this.GiveMoney();

        UtilsWX.getAllBuild();
        UtilsWX.openWXSubContextView(true);

        setTimeout(function () {
            UtilsWX.openWXSubContextView(false);
        }.bind(this),5000)
        // this.YearEvent();
        GlobalD.GameData.PlusGolden(GlobalD.GameData.EveryGive[0]);
        GlobalD.GameData.PlusDiamond(GlobalD.GameData.EveryGive[1]);
    },
    YearEvent() {
        //每年上报告
        var data1 = {
            name: "Whoareme",
            openId : userData.openId,

        }
        UtilsWX.postMessage(data1);
    },
    WorkerSalary() {
        let ManagerWorkerScript = this.ManagerWorker.getComponent('ManageWorker');
        let Wages = ManagerWorkerScript.WorkerLVPropertyJSON[GlobalD.GameData.GetWorkerLV()].Wages;
        Wages = parseInt(Wages);
        // cc.log('Wages='+Wages);
        let CanPayNum = GlobalD.GameData.GetGolden() / Wages;
        // cc.log('CanPayNum1111='+CanPayNum);
        CanPayNum = parseInt(CanPayNum);
        // cc.log('CanPayNum2222='+CanPayNum);
        // cc.log('GetWorkerNum='+GlobalD.GameData.GetWorkerNum());

        GlobalD.GameData.PlusGolden(-(GlobalD.GameData.GetWorkerNum() * Wages));

        let PayPersonNum = CanPayNum - GlobalD.GameData.GetWorkerNum();
        // cc.log('PayPersonNum='+PayPersonNum);
        if (PayPersonNum > 0 || PayPersonNum == 0) return;

        let FirePersonNum = -PayPersonNum;
        ManagerWorkerScript.FirePerson(FirePersonNum);

        this.Tips.active = true;
        this.Tips.getChildByName('Detail').getComponent(cc.Label).string = ' ' + FirePersonNum + '名工人因发不出工资已经离职';
        this.scheduleOnce(
            function () {
                this.Tips.active = false;
            }, 2
        )
    },
    GiveMoney() {
        let ManagerWorkerScript = this.ManagerWorker.getComponent('ManageWorker');
        let Wages = ManagerWorkerScript.WorkerLVPropertyJSON[GlobalD.GameData.GetWorkerLV()].Wages;
        GlobalD.GameData.PlusGolden(parseInt(Wages));
    },
    onRuningCost() {
        this._AllBuildingRuningCost();
        // GlobalD.GameData.PlusGolden(-1000105);
    },
    onAddGolden() {
        GlobalD.GameData.PlusGolden(1000000);
        GlobalD.GameData.PlusDiamond(1000);

        // GlobalD.GameData.SetWorkerLV(0);
    },
    //每个月扣除可销售的全部建筑的
    _AllBuildingRuningCost() {

        let buildsTildMapUnit = GlobalD.game.buildingsTiledMapUnit;
        let length = buildsTildMapUnit.length;
        GlobalD.GameData.GetGoldenCallBack((res) => {
            if (!res) {
                cc.warn('获取金币失败：', res);
                return;
            }
            // cc.log('获取金币：', res.resGolden);
            let _currentGolden = res.resGolden;
            for (let i = 0; i < length; i++) {

                // let _buildType = buildsTildMapUnit[i].getBuildingsInfo.buildInfo.buildType;
                // if (reGameStates.BuildType.Housing == _buildType
                //     || reGameStates.BuildType.Special == _buildType
                //     || reGameStates.BuildType.Factory == _buildType) {
                //     continue;
                // }
                //reGameStates.BuildType.Shop !== _buildType
                //只要运营成本不为零，都会每个月扣钱
                if (buildsTildMapUnit[i].getBuildingsInfo.buildInfo.RunningCost == 0) continue;

                let _runningCost = buildsTildMapUnit[i].getBuildingsInfo.buildInfo.RunningCost;

                // cc.log(_currentGolden, _runningCost);
                let _golden = _currentGolden - _runningCost;

                //交的起的，都交
                if (_golden < 0) {
                    // cc.log('不够钱交租啦，此建筑暂停营业：', buildsTildMapUnit[i].getBuildingsInfo.buildInfo.buildingName);
                    buildsTildMapUnit[i].getBuildingsInfo.buildInfo.isItSaleable = false;
                    buildsTildMapUnit[i].getBuildingsInfo.buildInfo.isItStopOperation = true;
                    buildsTildMapUnit[i].getBuildingsInfo.onStopOperation();
                    GlobalD.game.onAddBuildingTipView(buildsTildMapUnit[i].getBuildingsInfo);
                } else {
                    GlobalD.game.onRemoveBuildingTipView(buildsTildMapUnit[i].getBuildingsInfo);
                    buildsTildMapUnit[i].getBuildingsInfo.buildInfo.isItStopOperation = false;
                    buildsTildMapUnit[i].getBuildingsInfo.buildInfo.isItSaleable = true;

                    GlobalD.GameData.PlusGolden(-_runningCost);
                    // cc.log('商店：', buildsTildMapUnit[i].getBuildingsInfo.buildInfo.buildingName, '= 收费:', _runningCost);
                }
                //更新信息
                // cc.log('更新信息');
                GlobalD.game.updateBuilding(buildsTildMapUnit[i].getBuildingsInfo);

                _currentGolden = _golden;
            }
        })
    }
});
