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 JG0111 extends BaseEvent {

    @property({
        displayName: '替换的图片',
        type: cc.Sprite,
    })
    mIcon: cc.Sprite = null;

    @property({
        displayName: '未踩上图片',
        type: cc.SpriteFrame,
    })
    mIcon0: cc.SpriteFrame = null;

    @property({
        displayName: '踩上后的图片',
        type: cc.SpriteFrame,
    })
    mIcon1: cc.SpriteFrame = null;

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

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

    /**
     * 是否选中
     */
    public isHang = false;
    /**
     * 机关是否已经结束
     */
    public isOver = false;

    private count = 0;

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

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        if (other.node.group != 'bullet') {
            this.count++
            this.onBegin(self.tag)
        }

    }

    onEndContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        if (other.node.group != 'bullet') {
            this.count--
            if (this.count <= 0) {
                this.count = 0;
                this.onEnd(self.tag)
            }
        }
    }

    onBegin(tag: number) {
        this.isHang = true;
        this.mIcon.spriteFrame = this.mIcon1;
        this.checkOpen();
    }

    onEnd(tag: number) {
        this.isHang = false
        this.mIcon.spriteFrame = this.mIcon0
        this.isOver = false;
        for (let i = 0; i < this.mFenceTrigger.length; i++) {
            const element = this.mFenceTrigger[i];
            // element.active = true;
            this.showFence(element, SpineName.CLOSE);
            element.getComponent(cc.PhysicsBoxCollider).enabled = true;
        }
    }

    private checkOpen() {
        if (this.isOver) {
            return
        }
        //检查其它开关是否打开
        for (let i = 0; i < this.mButtons.length; i++) {
            const element = this.mButtons[i];
            let fdb = element.getComponent(JG0111)
            if (!fdb.isHang) return
        }
        this.isOver = true;
        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);
            }
        }
    }
}
