cc.Class({
    extends: cc.Component,

    properties: {
        ManageAI: cc.Node,

        GoldLabel: cc.Node,
        DiamondLabel: cc.Node,

        LV: cc.Node,
        MaxWorkLV: cc.Node,
        WorkerNum: cc.Node,
        WorkerCapacity: cc.Node,
        PhysicalStrength: cc.Node,
        Labor: cc.Node,
        Wages: cc.Node,
        RecruitCost: cc.Node,
        UpGradeCost: cc.Node,
        // //工人容量值
        // workerCapacity: { default: 32, type: cc.Integer, tooltip: '可招聘工人最大值' },
        //“管理”界面的招聘进度条 Recruit
        ManagementProgressBar_Recruit: {
            default: null,
            type: cc.Node,
        },
        //“管理”界面的培训进度条
        ManagementProgressBar_Train: {
            default: null,
            type: cc.Node,
        },
        AlertUIView: cc.Node
    },
    start() {

        // this.InitWorkerAI();
    },
    InitWorkerAI: function () {

        //体力 劳动力 薪资 培训费用
        this.WorkerLVPropertyJSON = [
            { "PhysicalStrength": "50", "Labor": "5", "Wages": "50", "UpgradeCost": "20", "MoveSpeed": "100" },
            { "PhysicalStrength": "55", "Labor": "7", "Wages": "100", "UpgradeCost": "50", "MoveSpeed": "120" },
            { "PhysicalStrength": "60", "Labor": "9", "Wages": "200", "UpgradeCost": "100", "MoveSpeed": "140" },
            { "PhysicalStrength": "65", "Labor": "11", "Wages": "300", "UpgradeCost": "500", "MoveSpeed": "160" },
            { "PhysicalStrength": "70", "Labor": "13", "Wages": "400", "UpgradeCost": "1000", "MoveSpeed": "180" },
            { "PhysicalStrength": "75", "Labor": "15", "Wages": "500", "UpgradeCost": "1500", "MoveSpeed": "200" },
            { "PhysicalStrength": "80", "Labor": "17", "Wages": "600", "UpgradeCost": "2000", "MoveSpeed": "220" },
            { "PhysicalStrength": "85", "Labor": "18", "Wages": "700", "UpgradeCost": "2500", "MoveSpeed": "240" },
            { "PhysicalStrength": "90", "Labor": "19", "Wages": "800", "UpgradeCost": "3000", "MoveSpeed": "260" },
            { "PhysicalStrength": "100", "Labor": "20", "Wages": "900", "UpgradeCost": "0", "MoveSpeed": "280" },
        ];
        this.WorkerNum.getComponent(cc.Label).string = GlobalD.GameData.GetWorkerNum();
        //容量是定死的值，不用存储，也不用读取
        this.WorkerCapacity.getComponent(cc.Label).string = GlobalD.GameData.GetWorkerCapacity();
        this.SetBar_RecruitLength(GlobalD.GameData.GetWorkerNum() / GlobalD.GameData.GetWorkerCapacity());

        this.LV.getComponent(cc.Label).string = GlobalD.GameData.GetWorkerLV() + 1;
        this.MaxWorkLV.getComponent(cc.Label).string = this.WorkerLVPropertyJSON.length;
        this.SetBar_TrainLength((GlobalD.GameData.GetWorkerLV() + 1) / this.WorkerLVPropertyJSON.length);

        let CurrentGradeJson = this.WorkerLVPropertyJSON[GlobalD.GameData.GetWorkerLV()];
        this.PhysicalStrength.getComponent(cc.Label).string = CurrentGradeJson.PhysicalStrength;
        this.Labor.getComponent(cc.Label).string = CurrentGradeJson.Labor;
        this.Wages.getComponent(cc.Label).string = CurrentGradeJson.Wages;
        this.RecruitCost.getComponent(cc.Label).string = CurrentGradeJson.Wages;
        this.UpGradeCost.getComponent(cc.Label).string = CurrentGradeJson.UpgradeCost;

        this._ManageAI = this.ManageAI.getComponent('ManageAI');
        this._ManageAI.InitCurrentGradeJson = CurrentGradeJson;
        //生成存储的工人数据
        this._ManageAI.onSpawnWorkerAIFromStoredData();

        // cc.log('工人');
    },
    //招聘
    onRecruit: function () {
        if (task.TaskIconCountClick == 1) {

            //现在关掉招聘的 页面 弹出对话框 提示 要点击建造
            task.managerUi.onButtonEvent_Personnel_Close();
            task.showManagerhide();
            var Canvas = cc.find("Canvas/UICamera");
            //移动camera 对应位置
            cc.find('GameNode/ManageTask').getComponent('ManageTask').onMoveToTiledTile(16, 16);
           
            //招聘了人马上设置对应的数
            task.TaskIconCountClick = -1;
            dialogmanager.init(Canvas, function () {
                dialogmanager.pickUp(dialogmanager.dialogue[30]);
            }.bind(this));
            dialogmanager.setOnCloseDialog(function () {
                task.removeTaskNode(task.taskCursorName[3]);
                // task._setTaskIconCountClick(12);
                // task._setTaskIconCountClick(13);

                // setTimeout(() => {
                //     task.taskCallBack();

                // }, 500);

                //建房子
                GlobalD.ManageTask.onInitGameStartTask();
                
            }.bind(this));
        }

        if (GlobalD.GameData.GetWorkerNum() + 1 > GlobalD.GameData.GetWorkerCapacity()) {
            cc.loader.loadRes('resUI/ShowNotEnoughMoney', function (err, texture) {
                var prefab = cc.instantiate(texture);
                prefab.getComponent('ShowNotEnoughMoney').Text('超员了！！');
                this.AlertUIView.addChild(prefab);
            }.bind(this));
            return;
        }
        if (GlobalD.GameData.GetGolden() < parseInt(this.WorkerLVPropertyJSON[GlobalD.GameData.GetWorkerLV()].Wages)) {

            cc.loader.loadRes('resUI/ShowNotEnoughMoney', function (err, texture) {
                var prefab = cc.instantiate(texture);
                prefab.getComponent('ShowNotEnoughMoney').Text('金币不足！！');
                this.AlertUIView.addChild(prefab);
            }.bind(this));
            return;
        }

        GlobalD.GameData.PlusGolden(-parseInt(this.WorkerLVPropertyJSON[GlobalD.GameData.GetWorkerLV()].Wages));
        GlobalD.GameData.PlusWorkerNum(1);
        this.WorkerNum.getComponent(cc.Label).string = GlobalD.GameData.GetWorkerNum();
        this._ManageAI.onSpawnWorkerAI();
        this.SetBar_RecruitLength(GlobalD.GameData.GetWorkerNum() / GlobalD.GameData.GetWorkerCapacity());
    },

    Building_t: function () {
        var Canvas = cc.find("Canvas/UICamera");
        var Building = cc.find("Canvas/UICamera/BelowTheMask/Building");
        task.addTaskTips(Canvas, task.taskPrefab[6], 0, 0, task.taskCursorName[1], function (nodePrefabs1) {
            UtilsPrefabs.setOn(nodePrefabs1.getChildByName("SureBtn"), function () {
                task.removeTaskNode(task.taskCursorName[1]);
                task.addTaskTips(Building, task.taskPrefab[1], -50, -50, task.taskCursorName[0], function (nodePrefabs) {

                }.bind(this));
            }.bind(this))
        }.bind(this));
    },
    //解雇
    onFire: function () {
        if (GlobalD.GameData.GetWorkerNum() - 1 < 0) return;

        if (this._ManageAI.onRemoveWorkerAI()) {
            //如果成功删除AI
            GlobalD.GameData.PlusWorkerNum(-1);
            this.WorkerNum.getComponent(cc.Label).string = GlobalD.GameData.GetWorkerNum();
        }
    },
    onFireNum() {
        this.FirePerson(5);
    },

    FirePerson: function (num) {
        if (GlobalD.GameData.GetWorkerNum() - num < 0) {
            num = GlobalD.GameData.GetWorkerNum();
        }
        //不需要解雇
        if (num <= 0) return;

        if (this.AutoFireWorker)
            this.unschedule(this.AutoFireWorker);
        //自动存储数据
        this.AutoFireWorker = function () {
            if (num <= 0) return;
            for (let i = 0; i < num; i++) {
                //只能解雇可以解雇状态下的工人
                if (this._ManageAI.onRemoveWorkerAI()) {
                    //如果成功删除AI
                    GlobalD.GameData.PlusWorkerNum(-1);
                    this.WorkerNum.getComponent(cc.Label).string = GlobalD.GameData.GetWorkerNum();
                    num--;
                }
            }
        }
        this.schedule(this.AutoFireWorker, 0.5);

    },
    //培训
    onUpgrade: function () {
        if (this.WorkerLVPropertyJSON.length - 2 < GlobalD.GameData.GetWorkerLV()) return;

        let CurrentGradeJson = this.WorkerLVPropertyJSON[GlobalD.GameData.GetWorkerLV()];
        let UpgradeCost = CurrentGradeJson.UpgradeCost;

        if (GlobalD.GameData.GetDiamond() < UpgradeCost) {
            cc.loader.loadRes('resUI/ShowNotEnoughMoney', function (err, texture) {
                var prefab = cc.instantiate(texture);
                prefab.getComponent('ShowNotEnoughMoney').Text('钻石不足！！');
                this.AlertUIView.addChild(prefab);
            }.bind(this));
            return;
        }

        GlobalD.GameData.PlusDiamond(-UpgradeCost);
        // this.GoldLabel.getComponent(cc.Label).string = GlobalD.GameData.GetDiamond();
        this.DiamondLabel.getComponent(cc.Label).string = GlobalD.GameData.GetDiamond();

        GlobalD.GameData.PlusWorkerLV(1);
        this.LV.getComponent(cc.Label).string = GlobalD.GameData.GetWorkerLV() + 1;

        CurrentGradeJson = this.WorkerLVPropertyJSON[GlobalD.GameData.GetWorkerLV()];
        this.PhysicalStrength.getComponent(cc.Label).string = CurrentGradeJson.PhysicalStrength;
        this.Labor.getComponent(cc.Label).string = CurrentGradeJson.Labor;
        this.Wages.getComponent(cc.Label).string = CurrentGradeJson.Wages;
        GlobalD.GameData.SetWorkerCapacity(CurrentGradeJson.Labor);
        this.WorkerCapacity.getComponent(cc.Label).string = CurrentGradeJson.Labor;

        //修改全体人物属性
        this._ManageAI.InitCurrentGradeJson = CurrentGradeJson;
        this._ManageAI.onUpgradeAllWorker();

        this.SetBar_TrainLength((GlobalD.GameData.GetWorkerLV() + 1) / this.WorkerLVPropertyJSON.length);
        this.SetBar_RecruitLength(GlobalD.GameData.GetWorkerNum() / GlobalD.GameData.GetWorkerCapacity());

        this.RecruitCost.getComponent(cc.Label).string = CurrentGradeJson.Wages;
        this.UpGradeCost.getComponent(cc.Label).string = CurrentGradeJson.UpgradeCost;
    },
    onDegrade: function () {
        if (GlobalD.GameData.GetWorkerLV() - 1 < 0) return;

        GlobalD.GameData.PlusWorkerLV(-1);
        this.LV.getComponent(cc.Label).string = GlobalD.GameData.GetWorkerLV() + 1;

        let CurrentGradeJson = this.WorkerLVPropertyJSON[GlobalD.GameData.GetWorkerLV()];

        CurrentGradeJson = this.WorkerLVPropertyJSON[GlobalD.GameData.GetWorkerLV()];
        this.PhysicalStrength.getComponent(cc.Label).string = CurrentGradeJson.PhysicalStrength;
        this.Labor.getComponent(cc.Label).string = CurrentGradeJson.Labor;
        this.Wages.getComponent(cc.Label).string = CurrentGradeJson.Wages;
        GlobalD.GameData.SetWorkerCapacity(CurrentGradeJson.Labor);
        this.WorkerCapacity.getComponent(cc.Label).string = CurrentGradeJson.Labor;

        //修改全体人物属性
        this._ManageAI.InitCurrentGradeJson = CurrentGradeJson;
        this._ManageAI.onUpgradeAllWorker();

        this.SetBar_TrainLength((GlobalD.GameData.GetWorkerLV() + 1) / this.WorkerLVPropertyJSON.length);
        this.SetBar_RecruitLength(GlobalD.GameData.GetWorkerNum() / GlobalD.GameData.GetWorkerCapacity());
    },
    //招聘进度条
    SetBar_RecruitLength: function (BarLength) {//BarLength(0~1,float)
        this.ManagementProgressBar_Recruit.getComponent(cc.ProgressBar).progress = BarLength;
    },
    //培训进度条
    SetBar_TrainLength: function (BarLength) {//BarLength(0~1,float)
        this.ManagementProgressBar_Train.getComponent(cc.ProgressBar).progress = BarLength;
    },
});
