import { Node, _decorator } from 'cc';
import { BaseLayer } from '../../common/BaseLayer';
import { cocosUtil } from '../../utils/cocosUtil';
import { levelManager } from '../manager/levelManager';
import { sdkManager } from '../manager/sdkManager';
import { playerModel } from '../model/playerModel';
const { ccclass, property } = _decorator;

@ccclass('AdCoinLayer')
export class AdCoinLayer extends BaseLayer {

    coinNumNode: Node;
    btnAdGet: Node;

    coin: number;

    onLoad() {
        super.onLoad();

        this.coinNumNode = this.getNodeByPath("info/coinLayer/num");
        this.btnAdGet = this.getNodeByPath("btns/btnAdGet");

        this.coin = levelManager.instance.adCoinNum;

        this.initUI();
    }

    initUI() {
        this.initEffect();
        this.setString(this.coinNumNode, "+" + this.coin);

    }

    initEffect() {
        cocosUtil.tweenScaleBreath(this.btnAdGet);
    }

    onButtonClick(node: Node, name: string) {
        switch (name) {
            case "btnAdGet":
                this.onClickBtnAdGet(node);
                break;
            default:
                break;
        }
    }

    onClickBtnAdGet(node: Node) {
        sdkManager.instance.sendEvent("观看激励视频-领取金币500");
        sdkManager.instance.openAd((st: number) => {
            if (st != 1) {
                return;
            }
            this.btnAdGet.active = false;
            playerModel.instance.addCoin(this.coin);
            this.openCoinGetEffectLayer(this.coinNumNode);
            sdkManager.instance.sendEvent("观看完激励视频-领取金币500");
        });
    }

}

