import FqLogin from "../../../../../login/FqLogin";
import { AudioMgr } from "../../../../../main/ViewManage";
import FSpriteTmpGood from "../../../object/FSpriteTmpGood";
import BaseEvent from "../../base/BaseEvent";
import FHitBox from "../../box/FHitBox";

const { ccclass, property } = cc._decorator;
/**
 * 1_3关卡的祭坛
 */
@ccclass
export default class FAltar1_5 extends BaseEvent {

    @property({
        displayName: '祭坛动画',
        type: sp.Skeleton
    })
    spine: sp.Skeleton = null;

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

    @property({
        displayName: "提示文字"
    })
    text: string = "";

    @property({
        displayName: "镜片",
        type: [cc.Node]
    })
    mHole: cc.Node[] = [];

    @property({
        displayName: '需要的物品id'
    })
    public goodId = 2004;

    @property({
        displayName: '存储ID'
    })
    public saveId: string = "";

    /**
     * 控制的栅栏
     */
    @property([cc.Node])
    mFenceTrigger: Array<cc.Node> = [];

    @property(cc.Node)
    monster: cc.Node = null;

    @property(cc.Node)
    box: cc.Node = null;

    public isShow = false
    /**
     * 放了多少个石头
     */
    public holdCount = 0;

    start() {
        let stage = this.ff.main.player.stage;
        if (stage.element.indexOf(this.saveId) > -1) {
            this.mFenceTrigger.forEach(node => {
                node.active = false;
            })
            this.mHole.forEach(element => {
                element.active = false;
            })
            if (this.monster) {
                this.monster.removeComponent(FSpriteTmpGood);
            }
            if (this.box) {
                let hitbox = this.box.getComponent(FHitBox);
                hitbox.hide.destroy();
            }
            this.spineAction(this.spine, "over", () => { });
        }
    }

    public onBegin(tag: number) {
        if (this.ff.main.player.stage.element.indexOf(this.saveId) > -1) return
        if (this.holdCount >= this.mFenceTrigger.length) return
        if (tag == 1) {
            this.showOpt(this.mTipsIcon, () => {
                this.holdStore()
            })
        }
    }
    public onEnd(tag: number) {
        if (tag == 1) {
            this.closeOpt()
        }
    }

    public holdStore() {
        let head = this.ff.mFFheader;
        let count = head.getTmpCount(this.goodId);
        if (count > 0) {
            this.holdCount++
            this.spineAction(this.spine, `0${this.holdCount}`, () => {
                head.removeTmpGood(this.goodId, 1);
                this.openmFenceTrigger(this.holdCount - 1);
            })
        } else {
            this.ff.main.showTips(this.text);
        }
    }

    public openmFenceTrigger(index: number) {
        if (this.mFenceTrigger.length <= 0) {
            return
        }
        if (index >= this.mFenceTrigger.length) return
        this.pause()
        this.moveCamera(this.mFenceTrigger[index].getPosition(), 1, () => {
            cc.tween(this.node).sequence(
                cc.callFunc(() => {
                    this.showFence(this.mFenceTrigger[index], 'close');
                    this.ff.main.playerEffectByPath(AudioMgr.openDoor);
                }),
                cc.delayTime(1),
                cc.callFunc(() => {
                    this.resume()
                    this.mFenceTrigger[index].active = false;
                    if (this.holdCount == this.mFenceTrigger.length) {
                        FqLogin.commitEvent(this.node.name, '', '');
                        this.getMapObject(this.saveId, null);
                        this.spineAction(this.spine, "over", () => { })
                        this.closeOpt();
                    }
                })
            ).start();
        })
    }
    private showFence(element, action) {
        let nodes = element.children;
        for (let i = 0; i < nodes.length; i++) {
            const element = nodes[i];
            let spine = element.getComponent(sp.Skeleton);
            if (spine) {
                spine.setAnimation(0, action, false);
            }
        }
    }

}
