import FqLogin from "../../../login/FqLogin";
import { AudioMgr } from "../../../main/ViewManage";
import FExitBox from "../box/FExitBox";
import FF from "../FF";
import BaseEvent from "./base/BaseEvent";

/**
 * 退出副本事件
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FExit extends BaseEvent {

    @property({
        displayName: '提示图标',
        type: cc.SpriteFrame
    })
    mTipsIcon: cc.SpriteFrame = null;

    private isEnd = false;

    onLoad(){
        super.onLoad();
        this.node.zIndex = -9999;
    }


    onBegin(tag: number) {
        if (this.isEnd) {
            return
        }
        if (tag == 1) {
            this.showOpt(this.mTipsIcon, () => {
                this.exitFF(this.ff);
            })
        }

    }
    onEnd(tag: number) {
        if (tag == 1) {
            this.closeOpt()
        }
    }

    private exitFF(ff: FF) {
        this.ff = ff;
        ff.stopRuning();
        ff.pauseSprite(true);


        let node = cc.instantiate(this.ff.mExitBox);
        let exitBox = node.getComponent(FExitBox);
        exitBox.main = this.ff.main;
        exitBox.init(this.ff.mFFheader.stageData);
        exitBox.show();
        exitBox.setCloseCallback(() => {
            ff.pauseSprite(false);
        })
        exitBox.setOKCallback(() => {
            this.isEnd = true;
            this.ff.exitCallback();
            this.ff.main.topNode.active = true;
            this.ff.main.playMusicByPath(AudioMgr.homeMusic);
        });
        FqLogin.commitEvent(this.node.name, '', '');
    }
}
