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

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

    coinNode: Node;
    btnAdGet: Node;
    adCoinNode: Node;

    coin: number;

    arrowAnimation: Animation;

    onLoad() {
        super.onLoad();

        this.screenAdapterBG(this.getNodeByPath("info"));

        this.coinNode = this.getNodeByPath("info/coin");
        this.btnAdGet = this.getNodeByPath("btns/btnAdGet");
        this.adCoinNode = find("num", this.btnAdGet);
        this.arrowAnimation = this.getNodeByPath("barLayer/arrow").getComponent(Animation);

        this.coin = levelManager.instance.getLevelSuccessCoin();

        this.initUI();

        levelManager.instance.levelSuccess();

        audioManager.instance.playEffect(constants.audioNames.win);

        sdkManager.instance.addLevelCount();
        sdkManager.instance.sendEventLevelEnd(true);

    }

    initUI() {
        this.initLocaltext();
        this.initEffect();
        this.setString(this.coinNode, "+" + this.coin);
    }

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

    initLocaltext() {

    }

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

    onClickBtnNext(node: Node) {
        playerModel.instance.addCoin(this.coin);
        this.openCoinGetEffectLayer(this.coinNode);
        this.closeLayer();


        // if (levelManager.instance.isLevelEsp()) {
        //     // 特殊关卡，回到开始页面，并且主动弹出特殊关卡选择界面
        //     eventManager.instance.send(msgac.levelEspSuccess);
        // } else {
        //     // 自动下一关
        //     eventManager.instance.send(msgac.levelNext);
        // }

        // 运营需求：改成关闭关卡界面，并回到开始页面
        eventManager.instance.send(msgac.LevelLayerClose);
        let obj: any = {};
        obj.name = constants.layers.StartLayer.name;
        eventManager.instance.send(msgac.openLayer, obj);

    }

    // 处理广告多倍奖励条
    adSuccess() {
        this.btnAdGet.active = false;
        let scale = this.getAwardScale();

        this.coin *= scale;
        this.initUI();
    }

    getAwardScale(): number {
        let scale = 1;
        let posX = this.arrowAnimation.node.getPosition().x;
        if (posX >= -194 && posX < -130) {
            scale = 2;
        } else if (posX >= -130 && posX < -70) {
            scale = 4;
        } else if (posX >= -70 && posX < 52) {
            scale = 2;
        } else if (posX >= 52 && posX < 108) {
            scale = 4;
        } else if (posX >= 108 && posX < 194) {
            scale = 3;
        }
        return scale;
    }

    onClickBtnAdGet(node: Node) {
        this.arrowAnimation.pause();
        sdkManager.instance.sendEvent("观看激励视频-多倍奖励");
        sdkManager.instance.openAd((st: number) => {
            if (st == 1) {
                this.adSuccess();
                sdkManager.instance.sendEvent("观看完激励视频-多倍奖励");
            } else {
                this.arrowAnimation.resume();
            }
        });
    }

    update(dt: number) {
        if (this.btnAdGet.active) {
            let scale = this.getAwardScale();
            this.setString(this.adCoinNode, "+" + (this.coin * scale));
        }
    }

}

