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

cc.Class({
    extends: cc.Component,

    properties: {

        content: cc.Node,

        coinRichText: cc.RichText,

        watchVideoRichText: cc.RichText,

        shareRichText: cc.RichText,

        secretarySprite: cc.Node,

        videoBtn: {
            tooltip: '观看视频按钮',
            default: null,
            type: cc.Node
        },
        shareBtn: {
            tooltip: '分享按钮',
            default: null,
            type: cc.Node
        },
        normalBtn: {
            tooltip: '普通确认按钮',
            default: null,
            type: cc.Node
        },

        _grossIncome: 0,
        grossIncome: {
            get() {
                return this._grossIncome;
            },

            set(value) {
                this._grossIncome = value;
                this.setCoinRichText(value);
                this.setWatchVideoRichText(value);
                this.setShareVideoRichText(value);
            }
        }
    },

    // LIFE-CYCLE CALLBACKS:

    init(grossIncome) {
        this.grossIncome = grossIncome;

        this._resetBtn();
        if (CC_WECHATGAME) {
            wx.request({
                url: "https://pub.dwstatic.com/wxgame/taptapstar/sheet/offlineIncome.json",
                success: (res) => {
                    if (res.data.video) {
                        this.videoBtn.active = true;
                    }
                    if (res.data.share) {
                        this.shareBtn.active = true;
                    }
                    if (res.data.normal) {
                        this.normalBtn.active = true;
                    }
                }
            })
        } else {
            this.videoBtn.active = true;
            this.shareBtn.active = true;
        }
    },

    onLoad() {

    },


    start() {
        this.content.scaleX = 0;
        this.content.scaleY = 0;
        this.content.runAction(cc.scaleTo(0.35, 1, 1).easing(cc.easeBackOut()));

        this.secretarySprite.runAction(cc.moveBy(0.3, cc.v2(400, 0)));
    },

    /**
     * 重置按钮状态
     */
    _resetBtn() {
        this.videoBtn.active = false;
        this.shareBtn.active = false;
        this.normalBtn.active = false;
    },

    dismiss() {
        let finish = cc.callFunc(() => {
            this.node.destroy();
        }, this);
        let sequence = cc.sequence(cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()), finish)
        this.content.runAction(sequence);
    },

    /**
     * 普通关闭，获得正常收益x1
     */
    close() {
        this.addIncome(this._grossIncome);
        this.dismiss()
    },

    /**
     * 增加金币收入
     * @param {number} value 数量
     */
    addIncome(value) {
        console.log("addIncome: " + value);
        GameModule.userInfo.gold += parseInt(value);

        // GameEvent.fire(GameNotificationKey.HandleOfflineIncomeAnim)
    },

    /**
     * 看视频获取更多离线收益
     */
    watchVideo() {
        // Todo: 对接微信广告Api
        this.addIncome(this._grossIncome * 2);
        this.dismiss();
    },

    /**
     * 调起微信分享及后续分享逻辑
     */
    share() {
        if (CC_WECHATGAME) {
            // this.shareFlag = 1
            wx.shareAppMessage({
                title: '确认过眼神，你就是我要的C位',
                imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share1.jpg',
                // query: 'uid=' + Global.user.uid + '&shareType=' + ShareAction.ADD_FRIEND
                success: (res) => {
                    console.log('分享成功');
                    this.addIncome(this._grossIncome * 3)
                    this.dismiss();
                    // this.shareFlag = 0;
                },
                fail: (res) => {
                    console.log('分享失败或取消');
                }
            });

            // 微信分享api无法正确回调时候的备用方案
            // wx.onShow(res => {
            //     if(this.shareFlag == 1) {
            //         GameModule.userInfo.grossIncome += this.grossIncome * 2;
            //         this.shareFlag = 0;
            //         this.dismiss();
            //     }
            // })
        } else {
            this.addIncome(this._grossIncome * 3);
            this.dismiss();
        }
    },

    setCoinRichText(coin) {
        this.coinRichText.string = `<img src='coin_small'/><outline color=#ffffff width=4><b><color=#009227> ${DWTool.coinParse(coin)}</c></b></outline>`;
    },

    setWatchVideoRichText(coin) {
        this.watchVideoRichText.string = `<img src='coin_small'/><color=#ffffff> ${DWTool.coinParse(coin * 2)}</c>`;
    },

    setShareVideoRichText(coin) {
        this.shareRichText.string = `<img src='coin_small'/><color=#ffffff> ${DWTool.coinParse(coin * 3)}</c>`;
    },


    // update (dt) {},
});
