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

/**
 * 宝箱
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FTreasureBox extends BaseEvent {

    @property({
        displayName: '地图元素id'
    })
    boxId: string = '28';

    @property([cc.String])
    text: Array<string> = [];

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

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

    @property(cc.Node)
    guides: Array<cc.Node> = [];//引导标识

    @property({
        displayName: '是否需要引导穿装备'
    })
    isGuide: boolean = false

    @property({
        displayName: '指引步骤'
    })
    guideStep: number = 0;

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

    @property({
        displayName: '消失的提示',
        type: cc.Node
    })
    mCancelNode: cc.Node = null;

    private isOpen = false;

    onLoad() {
        super.onLoad()
        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.showOpt(this.mTipsIcon, () => {
                if (this.mCancelNode) {
                    this.mCancelNode.destroy()
                }
                this.openBox();
            })
        }
    }
    /**
     * 主角离开碰撞区域
     */
    public onEnd(tag: number) {
        if (tag == 1) {
            this.closeOpt()
        }
    }

    private openBox() {
        this.closeOpt()
        this.isOpen = true;
        this.pause()
        for (let i = 0; i < this.guides.length; i++) {
            const element = this.guides[i];
            element.destroy();
        }
        this.guides = [];

        this.spine.setCompleteListener(() => {
            FqLogin.commitEvent(this.node.name, '', '');

            this.spine.setCompleteListener(null);
            //与服务器通讯
            this.getMapObject(this.boxId, () => {
                this.showDialog1()
            })
        });
        this.spine.setAnimation(0, 'open', false);
        this.ff.main.playerEffectByPath(AudioMgr.box);
    }
    private showDialog1() {
        if (this.text.length <= 0) {
            this.resume()
            return
        }
        this.showDialog(this.ff.mainSprite.node, this.text, () => {
            this.resume()
            if (this.isGuide) {
                if (this.guideStep == 0) {
                    this.guide()
                } else if (this.guideStep == 1) {
                    this.guide_1()
                }
            }
        })
    }
    /**
     * 开始引导穿装备
     */
    private guide() {

        for (let i = 0; i < this.mFenceTrigger.length; i++) {
            const element = this.mFenceTrigger[i];
            element.destroy()
        }

        let main = this.ff.main

        let guideMask = main.mGuideMask
        let res = this.ff.fres;
        let hudNode = res.mHudNode.children[0]
        guideMask.setTargetNode(hudNode)
        guideMask.show()

        let panelIcon = hudNode.getComponent(FPanelIcon)
        panelIcon.inGuide = true

        panelIcon.setGuideCallback(() => {
            panelIcon.setGuideCallback(null)
            guideMask.close();
            this.ff.firstGetSkillTips();
        })

    }

    /**
     * 开始引导强化装备
     */
    private guide_1() {
        for (let i = 0; i < this.mFenceTrigger.length; i++) {
            const element = this.mFenceTrigger[i];
            element.destroy()
        }
        let main = this.ff.main
        let guideMask = main.mGuideMask
        let res = this.ff.fres;
        let hudNode = res.mHudNode.children[0];
        guideMask.setTargetNode(hudNode);
        guideMask.show();
        let panelIcon = hudNode.getComponent(FPanelIcon);
        panelIcon.inGuide = true;
        panelIcon.guideStep = this.guideStep;
        panelIcon.setGuideCallback(() => {
            guideMask.close();
        })
    }

}
