import FqLogin from "../../login/FqLogin";
import { AudioMgr } from "../../main/ViewManage";
import BaseEvent from "../fight/evnet/base/BaseEvent";

const SpineName = {
    CLOSE: "close",
    OPEN: "open"
}


/**
 * 多组按钮控制开门
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class OpenDoor extends BaseEvent {

    @property({
        displayName: '道具ID',
    })
    goodID: number = 2001;

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

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



    private count = 0;

    onBegin(tag: number) {
        let head = this.ff.mFFheader;
        let count = head.getTmpCount(this.goodID);
        if (count > 0) {
            this.showOpt(this.mTipsIcon,()=>{
                head.removeTmpGood(this.goodID, 1);
                this.closeOpt();
                this.checkOpen();
            })
        }else{

        }
    }

    onEnd(tag: number) {
        this.closeOpt();
    }

    private checkOpen() {
        //检查其它开关是否打开
        this.pause();
        this.moveCamera(this.mFenceTrigger[0].getPosition(), 1, () => {
            cc.tween(this.node).sequence(
                cc.callFunc(() => {
                    for (let i = 0; i < this.mFenceTrigger.length; i++) {
                        const element = this.mFenceTrigger[i];
                        this.showFence(element, SpineName.OPEN);
                    }
                    this.ff.main.playerEffectByPath(AudioMgr.openDoor);
                }),
                cc.delayTime(1),
                cc.callFunc(() => {
                    this.resume()
                    for (let i = 0; i < this.mFenceTrigger.length; i++) {
                        const element = this.mFenceTrigger[i];
                        // element.active = false;
                        element.getComponent(cc.PhysicsBoxCollider).enabled = false;
                    }
                })
            ).start();

        })
    }
    private showFence(element, action) {
        let nodes = element.children;
        for (let i = 0; i < nodes.length; i++) {
            const element = nodes[i];
            let spine: sp.Skeleton = element.getComponent(sp.Skeleton);
            if (spine) {
                spine.setAnimation(0, action, false);
            }
        }
    }
}
