// 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 { PROPTYPE, SURPRISETASKTYPE, VIDEOADSTATE, VIDEOTYPE } from "../data/Enum";
import GamePlay from "../GamePlay";
import DataMgr from "../mgr/DataMgr";
import EffectMgr from "../mgr/EffectMgr";
import GameMgr, { UI_NAME } from "../mgr/GameMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GetPropUI extends cc.Component {

    @property(cc.Node)
    public node_rectBg: cc.Node = null;
    @property(cc.Node)
    public node_closeBtn: cc.Node = null;
    @property(cc.Node)
    public node_getBtn: cc.Node = null;
    @property(cc.Node)
    node_prop: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        this.adapt();
        this.initEvent();
        //mk.ad.showBannerAd();
        // mk.ad.showNativeAd(4);
        // mk.ad.showInterAd(0);
    }

    // update (dt) {}

    onEnable() {
        GameMgr.Inst.sendEvent(UI_NAME.GetPropUI, `进入获取道具界面`);
        this.init();
    }

    onDisable() {
        GamePlay.Inst.curPropType = PROPTYPE.Null;
    }

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

    init() {
        for (var i = 0; i < this.node_prop.childrenCount; i++) {
            let prop = this.node_prop.children[i];
            let propCode = prop.children[0] ? prop.children[0].name : "";
            if (propCode == GamePlay.Inst.curPropType) {
                prop.active = true;
            }
            else {
                prop.active = false;
            }
        }
    }

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

    onClick(event: cc.Event.EventTouch) {
        //AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.buttonClick);
        mk.audio.playEffect("button");
        switch (event.currentTarget) {
            case this.node_getBtn:
                this.onClickGet();
                break;
            case this.node_closeBtn:
                this.onClickClose();
                break;
        }
    }

    onClickClose() {
        GameMgr.Inst.sendEvent(UI_NAME.GetPropUI, "点击关闭按钮");
        mk.ui.closePanel("GetPropUI");

    }

    /**点击获取 */
    onClickGet() {
        GameMgr.Inst.sendEvent(UI_NAME.GetPropUI, `点击${GamePlay.Inst.curPropType}按钮`);
        GamePlay.Inst.cancelShowInter();
        mk.ad.watchAd((ifsuccess: boolean) => {
            this.watchCallBack(ifsuccess)
        });
    }

    /**观看回调 */
    watchCallBack(ifSuccess: boolean = false) {

        if (ifSuccess) {
            this.scheduleOnce(() => {
                mk.tip.pop("观看成功，获取奖励")
            }, 0)
            this.watchSuccess();
        }
        else {
            this.scheduleOnce(() => {
                mk.tip.pop("观看失败，无法领取奖励哦")
            }, 0)
            this.watchFailed();
        }
    }

    /**观看成功 */
    watchSuccess() {
        GameMgr.Inst.sendEvent(UI_NAME.GetPropUI, `视频获取${GamePlay.Inst.curPropType}道具成功`);
        EffectMgr.Inst.addTip("观看视频成功,获得道具");
        DataMgr.Inst.updatePropNum(1, GamePlay.Inst.curPropType, 1);
        // mk.ui.closePanel("GetPropUI")
    }

    /**观看失败 */
    watchFailed() {
        GameMgr.Inst.sendEvent(UI_NAME.GetPropUI, `视频获取${GamePlay.Inst.curPropType}道具失败`);
        // EffectMgr.Inst.addTip("观看视频失败,请稍后再试");
    }
}
