import { AudioMgr } from "../../main/ViewManage";
import EventListener from "../../util/EventListener";
import BaseEvent from "../fight/evnet/base/BaseEvent";

/**
 * 要是开启宝箱
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class DJ0102 extends BaseEvent {

    @property({
        displayName: '宝箱ID'
    })
    boxId: string = '28';

    @property({
        displayName: "钥匙ID"
    })
    keyID: number = 2001;

    @property(sp.Skeleton)
    spine: sp.Skeleton = null;

    @property({
        displayName: '靠近提示',
        type: cc.Node
    })
    closeTips: cc.Node = null;

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

    /**
     * 控制的栅栏机关
     */
    @property({
        displayName: '控制的机关',
        type: [cc.Node],
    })
    mFenceTrigger: Array<cc.Node> = [];

    private isOpen = false;

    onLoad() {
        super.onLoad()
        // this.closeTips.active = false;

        // let stage = this.ff.main.player.stage;
        // if (stage.element.indexOf(this.boxId) >= 0) {
        //     this.node.destroy();
        //     for (let i = 0; i < this.mFenceTrigger.length; i++) {
        //         const element = this.mFenceTrigger[i];
        //         element.destroy()
        //     }
        // }
    }

    /**
     * 主角进入碰撞区域
     */
    public onBegin(tag: number) {
        if (this.isOpen) {
            return
        }
        if (tag == 1) {
            // this.closeTips.active = true;
            this.showOpt(this.mTipsIcon, () => {
                let head = this.ff.mFFheader;
                let count = head.getTmpCount(this.keyID);
                if (count > 0) {
                    // this.closeTips.active = false;
                    this.pause();
                    this.openBox();
                } else {
                    EventListener.dispatchEvent("step_2_1");
                    console.log("===没有钥匙==")
                }
            })
        }
    }
    /**
     * 主角离开碰撞区域
     */
    public onEnd(tag: number) {
        if (tag == 1) {
            this.closeOpt()
            // this.closeTips.active = false;
        }
    }

    private openBox() {
        this.isOpen = true;
        this.closeOpt();
        this.spine.setAnimation(0, 'open', false);
        this.ff.main.playerEffectByPath(AudioMgr.box);
        this.spine.setCompleteListener(() => {
            this.resume();
            EventListener.dispatchEvent("step_2_2");
            // FqLogin.commitEvent(this.node.name, '', '');
            // this.spine.setCompleteListener(null);
            // //与服务器通讯
            // this.getMapObject(this.boxId, () => {

            // })
        });
    }

}