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

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

    coinLayer: Node;
    skillLayer: Node;

    coinNumNode: Node;
    skillIcon: Node;
    skillNumNode: Node;
    btnAdGet: Node;

    isBoss: boolean;
    coin: number;
    skillId: number;
    skillNum: number;

    onLoad() {
        super.onLoad();

        this.coinLayer = this.getNodeByPath("info/coinLayer");
        this.skillLayer = this.getNodeByPath("info/skillLayer");

        this.coinNumNode = find("num", this.coinLayer);

        this.skillIcon = find("icon", this.skillLayer);
        this.skillNumNode = find("num", this.skillLayer);

        this.btnAdGet = this.getNodeByPath("btns/btnAdGet");

        this.isBoss = this.obj.isBoss;
        this.coin = this.obj.coin;
        this.skillId = this.obj.skillId;
        this.skillNum = 1;

        let titleNode1 = this.getNodeByPath("info/title1");
        let titleNode2 = this.getNodeByPath("info/title2");
        if (this.obj.isBox) {
            titleNode2.active = true;
            titleNode1.active = false;
        } else {
            titleNode1.active = true;
            titleNode2.active = false;
        }

        this.initUI();
    }

    initUI() {
        this.initEffect();

        if (this.coin > 0) {
            this.coinLayer.active = true;
            this.skillLayer.active = false;

            this.coinNumNode.active = true;
            this.skillIcon.active = false;
            this.skillNumNode.active = false;
            this.setString(this.coinNumNode, "+" + this.coin);
        } else if (this.skillId > 0) {
            this.coinLayer.active = false;
            this.skillLayer.active = true;

            this.coinNumNode.active = false;
            this.skillIcon.active = true;
            this.skillNumNode.active = true;
            this.setString(this.skillNumNode, "x" + this.skillNum);
            let row = designManager.instance.getRowById(constants.tableName.skill, this.skillId);
            iconManager.instance.setSprite(this.skillIcon, row.icon);
        }
    }

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

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

    closeLayer() {
        super.closeLayer();
        if (this["cb"]) {
            this["cb"]();
        }
    }

    onClickBtnGet(node: Node) {
        if (this.coin > 0) {
            playerModel.instance.addCoin(this.coin);
            this.openCoinGetEffectLayer(this.coinNumNode);
        } else if (this.skillId > 0) {
            playerModel.instance.addSkillNumById(this.skillId, this.skillNum);
        }

        this.closeLayer();

        if (this.isBoss) {
            levelManager.instance.bossBoxHasGet = true;
            levelManager.instance.judgeLevelSuccess();
        }
    }

    onClickBtnAdGet(node: Node) {
        sdkManager.instance.sendEvent("观看激励视频-双倍奖励");
        sdkManager.instance.openAd((st: number) => {
            if (st != 1) {
                return;
            }
            this.btnAdGet.active = false;
            if (this.coin > 0) {
                // 金币加倍
                this.coin *= 2;
                this.initUI();
            } else if (this.skillId > 0) {
                // 技能加倍
                this.skillNum++;
                this.initUI();
            }
            sdkManager.instance.sendEvent("观看完激励视频-双倍奖励");
        });
    }

}

