
const DWTool = require("../utils/DWTool");
const AlertManager = require('../utils/AlertManager');
const {GameNotificationKey} = require('../utils/GameEnum');
const ArtistTrainApi = require('../net/ArtistTrainApi');
const GameModule = require('../utils/GameModule');

cc.Class({
    extends: cc.Component,

    properties: {
      
        content: cc.Node,

        scrollView: cc.ScrollView,

        countDownLabel: cc.Label,

        refreshLabel: cc.Label,

        tipLabel: cc.Label,

        refreshBtn: cc.Button,

        countDown: {
            get: function() {
                if (!this._countDown) {
                    this._countDown = 0;
                }
                return this._countDown;
            },
            set: function (value) {
                this._countDown = value;
                this.countDownLabel.string = DWTool.calculateTime(this._countDown);
                this._preCountDown = this._countDown;
            }
        },

        isTraining: {
            get: function() {
                if (this._isTraining === undefined) {
                    this._isTraining = false;
                }
                return this._isTraining;
            },
            set: function (value) {
                this._isTraining = value;
                if (this._isTraining) {
                    this.refreshLabel.node.active = false;
                    this.tipLabel.node.active = false;
                    this.refreshBtn.node.active = false;
                } else {
                    this.refreshLabel.node.active = true;
                    this.tipLabel.node.active = true;
                    this.refreshBtn.node.active = true;
                }
            }
        },
    },

    init(targetUid) {
        this.targetUid = targetUid;
    },

    // LIFE-CYCLE CALLBACKS:
    onLoad () {
        
        this.countDown = 600;

        this._currentTime = 0;

        this.getNetworkData();

        GameEvent.on(GameNotificationKey.BeginArtistTrain, this, (missionId) => {
            this.clickStartTrain(missionId);
        });

        GameEvent.on(GameNotificationKey.ArtistTrainCompletion, this, (missionId) => {

            ArtistTrainApi.missionGain(this.targetUid, missionId, (responseData) => {
                console.log("任务已完成!");
                GameEvent.fire(GameNotificationKey.RefreshUserInformation);
            }, (err, msg) => {
                console.log(msg);
            });
            this.close();
        });

        GameModule.homeGuide.on('Fire_state29', () => {
            if (this.missionData && this.missionData.length > 0) {
                let missionId = this.missionData[0].id;
                this.clickStartTrain(missionId);
            }
        }, this);

    },

    onDestroy() {
        GameEvent.off(GameNotificationKey.BeginArtistTrain, this);
        GameEvent.off(GameNotificationKey.ArtistTrainCompletion, this);
    },

    start() {

        this.content.y = -cc.view.getVisibleSize().height;

        let bouncesActionArray = [];

        let space = 50;
        let upAction = cc.moveTo(0.25, cc.v2(0, space));
        let downAction = cc.moveBy(0.1, cc.v2(0, -space));

        bouncesActionArray.push(upAction);
        bouncesActionArray.push(downAction);

        this.content.runAction(cc.sequence(bouncesActionArray));
    },

    getNetworkData() {

        ArtistTrainApi.missionGetTrainings(this.targetUid, (responseData) => {
            
            this.missionData = responseData.list || [];

            if (typeof responseData.list === 'array' && responseData.list.length > 0) {
                for (let item of responseData.list) {
                    if (item.status === 1 || item.status === 2) {
                        this.isTraining = true;
                    } else {
                        this.isTraining = false;
                    }
                }
            } else {
                // 设置为true可以将倒计时隐藏起来
                this.isTraining = true;
            }
            
            this.layout();

        }, (err, msg) => {
            console.log("error: " + msg);
        });
    },

    clickStartTrain(missionId) {

        ArtistTrainApi.missionTrains(this.targetUid, missionId, (responseData) => {
            
            this.missionData = responseData.list || [];
            this.isTraining = true;
            this.layout();

            GameModule.homeGuide.getComponent('HomeGuide').handleState('state30');

        }, (err, msg) => {
            console.log("error: " + msg);
        });
    },

    layout() {

        for (let child of this.scrollView.content.children) {
            child.destroy();
        }

        DWTool.loadResPrefab("./prefabs/artist_train_item")
        .then((result) => {   
            for (let i = 0; i < this.missionData.length; i++) {
                let item = cc.instantiate(result);
                item.getComponent('ArtistTrainItem').init(this.targetUid, this.missionData[i], this.node.zIndex);
                this.scrollView.content.addChild(item);
            }

            GameModule.homeGuide.getComponent('HomeGuide').handleState('state29');
        });
    },

    close() {
        this.node.destroy();
    },

    diamondRefresh() {
        AlertManager.showRechargeAlert(this.node.zIndex);
    },

    update(dt) {
    
        if (this.isTraining) { return; }

        if (Math.floor(this._currentTime) === this.countDown) {

            this._currentTime = 0;
            this.getNetworkData();
            
        } else {

            this._currentTime += dt;
            
            let resultCountDown = this.countDown - Math.floor(this._currentTime);
            if (this._preCountDown !== resultCountDown) {

                this.countDownLabel.string = DWTool.calculateTime(resultCountDown);
                this._preCountDown = resultCountDown;
            }
        } 
    }
});
