
const GameModule = require("./utils/GameModule");
const DWTool = require("./utils/DWTool");
const Api = require('./net/Api');
const NotiKey = require('./utils/GameEnum').GameNotificationKey;
const BuildingModel = require('./utils/BuildingModel');
const AlertManager = require('./utils/AlertManager');
const ReportType = require("./utils/GameEnum").ReportType;

cc.Class({

    extends: cc.Component,

    properties: {

        _reportFailDuration: 0,
        grossIncomeLabel: cc.Label,
        grossCoin: sp.Skeleton,
        // rateLabel: cc.Label,

        headSprite: cc.Sprite,

        starsLabel: cc.Label,

        starNode: cc.Node,

        diamondLabel: cc.Label,

        recordModify: [],

        recordUnlockModify: [],

        stars: {
            get: function () {
                return this._stars;
            },
            set: function (value) {
                this._stars = value;
                this.starsLabel.string = this._stars;
                this.updateStarAnimation();
                DWTool.submitWechatStars(this._stars);
                if (value == 20) {
                    GameModule.homeGuide.getComponent('HomeGuide').handleState('state6');
                    // GameEvent.fire(NotiKey.ResetLevelHomePaddingBottom);
                }
            }
        },

        grossIncome: {
            get: function () {
                return this._grossIncome;
            },
            set: function (value) {
                this._grossIncome = value;
                this.grossIncomeLabel.string = DWTool.coinParse(this._grossIncome);
            }
        },

        diamond: {
            get: function () {
                return this._diamond;
            },
            set: function (value) {
                this._diamond = value;
                this.diamondLabel.string = this._diamond;
            }
        },

        levelHomeItemFullCount: 0
    },

    onLoad() {
        GameModule.userInfo = this;
        this._stars = 0;
        this._grossIncome = 0;
        this._rate = 0;
        this._isPlayAnimation = false;
        this.seq = 1;

        this.headSprite.node.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
            GameEvent.fire(NotiKey.ShowUserInfomation, this.userInfo.uid);
        }, 1000, true), this);

        //监听用户收取金币事件
        GameEvent.on(NotiKey.UserCollectCoin, this, (flag) => {
            console.log(flag);
            let animKey = flag ? 'jinbi_huoqu2' : 'jinbi_huoqu'
            this.grossCoin.setAnimation(0, animKey, false)
        });

        GameEvent.on(NotiKey.LevelHomeItemBuildingFull, this, () => {
            this.levelHomeItemFullCount += 1;
            if (this.levelHomeItemFullCount === 5) {
                GameEvent.fire(NotiKey.LevelHomeItemBuildingAllFull);
                GameEvent.fire(NotiKey.CurrentCompanyMax);
                // 满级立刻上报
                this.reportFunc();
            }
        });

        this.reportFunc = () => {

            if (this._reportFailDuration > 0) {
                this._reportFailDuration -= 3;
                return;
            }

            DWTool.reportInfo(this.seq, this.grossIncome, this.stars, this.recordModify, this.recordUnlockModify)
                .then((result) => {

                    this._reportFailDuration = 0;
                    this.recordModify = [];
                    this.recordUnlockModify = [];
                    this.seq += 1;

                }).catch((err) => {

                    if (err.code === -4) {
                        this._reportFailDuration = 60;
                    }
                    console.log(err.msg);
                });
        }

        this.schedule(this.reportFunc, 3.0);
    },

    setUserInfo(userInfo) {
        if (arguments.length < 1) {
            throw new Error("setUserInfo Missing parameter...");
        }
        this.userInfo = userInfo;
        Api.createImageFromUrl(userInfo.head, (spriteFrame) => {
            this.headSprite.spriteFrame = spriteFrame;
        }, null);
    },

    setGrossIncomeAndStars(grossIncome, stars) {
        if (arguments.length < 2) {
            throw new Error("setGrossIncomeAndStars Missing parameter...");
        }
        this.grossIncome = grossIncome;
        this.stars = stars;
    },

    updateStarAnimation() {

        if (this._isPlayAnimation) { return; }

        this._isPlayAnimation = true;
        let jumpHeight = 30;
        let duration = 0.2;

        let animationArray = [];
        while (jumpHeight > 0.1) {
            jumpHeight = jumpHeight - (jumpHeight / 3);
            duration = duration - (duration / 3);
            let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
            let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn());
            animationArray.push(upAction);
            animationArray.push(downAction);
        }

        let callback = cc.callFunc(() => {
            this._isPlayAnimation = false;
        });

        animationArray.push(callback);
        this.starsLabel.node.runAction(cc.sequence(animationArray));
    },

    updateRecordModify(buildingInfo) {

        for (let i = 0; i < this.recordModify.length; i++) {
            let temp = this.recordModify[i];
            if (buildingInfo.buildingId == temp.buildingId) {
                this.recordModify.splice(i, 1, buildingInfo)
                return;
            }
        }

        this.recordModify.push(buildingInfo);
    }

});