import { AudioMgr } from "../../../../../main/ViewManage";
import BaseEvent from "../../base/BaseEvent";

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

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

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


    @property({
        displayName: '祭坛3个孔',
        type: [cc.Node]
    })
    mHole: Array<cc.Node> = [];

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

    @property({
        displayName: '获得的物品id'
    })
    public getGoodId = 2006;

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

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

    start() {
        this.node.active = false
        this.spine.node.active = false
        let stage = this.ff.main.player.stage;
        if (stage.element.indexOf("3001") > -1) {
            for (let i = 0; i < this.mFenceTrigger.length; i++) {
                this.mFenceTrigger[i].active = false;
            }
        }
    }
    /**
     * 祭坛显现
     */
    public showAltar() {
        this.pause()

        this.moveCamera(this.node.getPosition(), 1, () => {
            this.spine.node.active = true
            this.spine.setCompleteListener(() => {
                this.isShow = true
                this.resume()
            });
            this.spine.setAnimation(0, 'up', false);
        })

    }

    public onBegin(tag: number) {
        if (this.holdCount >= 3) {
            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++
            head.removeTmpGood(this.goodId, 1);
            if (this.holdCount >= 3) {
                this.closeOpt()
                this.showKnife()
            } else {
                this.spine.setAnimation(0, '0' + this.holdCount, false);
                this.spine.setEndListener(() => {
                    this.openmFenceTrigger(this.holdCount - 1);
                })
            }
        } else {
            this.ff.main.showTips('需要祭石');
        }
    }

    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;
                })
            ).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);
            }
        }
    }

    private showKnife() {
        this.pause();
        this.spine.setCompleteListener(() => {
            this.showKnife1()
        });
        this.spine.setAnimation(0, 'down', false);
    }
    private showKnife1() {
        this.spine.setCompleteListener(() => {
            let header = this.ff.mFFheader;
            header.addTmpGood(this.getGoodId, 1);
            this.resume()
        });
        this.spine.setAnimation(0, 'Pull', false);
    }

}
