// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import PlayerConst from "../data/PlayerConst";
import DataMgr from "../mgr/DataMgr";
import EffectMgr from "../mgr/EffectMgr";
import GameMgr, { UI_NAME } from "../mgr/GameMgr";
import HttpMgr from "../mgr/HttpMgr";
import Util from "../util/Util";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelRedPacketUI extends cc.Component {


    @property(cc.Node)
    node_rectBg: cc.Node = null;

    @property(cc.Label)
    label_redPacketCashNum: cc.Label = null;
    @property(cc.Node)
    node_unit: cc.Node = null;
    @property(cc.Label)
    label_playerCashNum: cc.Label = null;

    @property(cc.Node)
    node_info: cc.Node = null;

    @property(cc.Node)
    node_doubleGetBtn: cc.Node = null;
    @property(cc.Node)
    node_getBtn: cc.Node = null;

    public cashNum: number = 0;
    public eliminateNum: number = 4;


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        this.adapt();
        this.initEvent();
    }

    // update (dt) {}

    onEnable() {
        GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, "进入关卡红包界面");
        // //显示广告
        // AdMgr.Inst.showNativeAd(4, true);
        mk.audio.playEffect("ef_redPacket_open");
    }

    onDisable() {
        // //
        // AdMgr.Inst.destroyNativeAd();
    }

    //适配
    adapt() {
        this.node_rectBg.setContentSize(cc.winSize.width, cc.winSize.height);
    }

    /**初始化
     * @param eliminateNum 消除数目 根据消除数目来
     */
    init(eliminateNum: number) {
        mk.console.log("eliminateNum", eliminateNum);
        this.eliminateNum = eliminateNum;
        this.initPlayerCashNum();
        this.initRedPacketCashNum();
        this.showInfo();
    }

    /**初始化事件 */
    initEvent() {
        this.node_doubleGetBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_getBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
    }

    /**显示 */
    showInfo() {
        this.node_info.y = -180;
        cc.tween(this.node_info).to(0.5, { y: 15 }).start();
    }

    /**初始化当前玩家获取比 */
    initPlayerCashNum() {
        // let num = Math.round((PlayerConst.cashNum * 100)) / 100;
        let num = Util.numberFixed(PlayerConst.cashNum, 2);
        this.label_playerCashNum.string = `当前余额：${num} 元`;
    }

    /**初始化关卡随机红包 */
    initRedPacketCashNum() {
        if (this.eliminateNum < 3) {
            this.eliminateNum = 3;
        }
        let randomCashNum = (Math.floor(Math.random() * (this.eliminateNum - 3)) + 1) * 0.01
        this.cashNum = Util.numberFixed(randomCashNum, 2);
        this.label_redPacketCashNum.string = this.cashNum.toString();
    }

    /**点击 */
    onClick(event: cc.Event.EventTouch) {
        switch (event.currentTarget) {
            case this.node_doubleGetBtn:
                this.onClickDoubleGetBtn();
                break;
            case this.node_getBtn:
                this.onClickGetBtn();
                break;
        }
    }

    /**点击获取 */
    onClickGetBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, `点击普通领取${this.cashNum}元`);
        this.getCash(1);
    }

    /**点击双倍领取 */
    onClickDoubleGetBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, `点击双倍领取${this.cashNum}元`);
        // AdMgr.Inst.watchAd(VIDEOTYPE.LevelRedPacket, (ifsuccess) => { this, this.watchCallBack(ifsuccess) });
    }

    /**观看回调 */
    watchCallBack(ifSuccess: boolean = false) {
        if (ifSuccess) {
            mk.console.log("观看成功")
            this.watchSuccess();
        }
        else {
            mk.console.log("观看失败")
            this.watchFailed();
        }
    }

    /**观看成功 */
    watchSuccess() {
        GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, `视频双倍领取${this.cashNum}元成功`);
        EffectMgr.Inst.addTip("观看视频成功,成功双倍领取");
        this.getCash(2);
    }

    /**观看失败 */
    watchFailed() {
        GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, `视频双倍领取${this.cashNum}元失败`);
        // EffectMgr.Inst.addTip("观看视频失败,请稍后再试");
    }

    /**
     * 获取体力
     * @param multiple 倍数 默认是1 
     */
    getCash(multiple: number = 1) {
        mk.console.log("multiple", multiple);
        DataMgr.Inst.updateMoneyNum(this.cashNum * multiple)

        let get_type = multiple === 1 ? 0 : 1;
        HttpMgr.Inst.randomRedPacket(get_type, PlayerConst.levelNum, this.cashNum).then((data) => {
            EffectMgr.Inst.addTip("观看视频成功,成功领取双倍红包");
            mk.console.log("randomRedPacket data", data);
        }).catch((err) => {

        });

        mk.ui.closePanel("LevelRedPacketUI");
    }
}
