import WOneByone from "../game/fight/evnet/map1/WOneByone";
import Home from "../game/home/Home";
import FqLogin from "../login/FqLogin";


const { ccclass, property } = cc._decorator;
/**
 * 第一次进入游戏主页时候的引导
 */
@ccclass
export default class PlotHome extends cc.Component {

    @property({
        displayName: '对话对象',
        type: [cc.Node]
    })
    mNode: Array<cc.Node> = [];

    @property(cc.Prefab)
    mMapDialog: cc.Prefab = null;

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

    @property(cc.Node)
    private mBlockInputEvents: cc.Node = null;

    /**
     * 冒险按钮
     */
    @property(cc.Node)
    mBtMx: cc.Node = null;

    @property(sp.Skeleton)
    mBtSpine: sp.Skeleton = null;//冒险菜单动画

    /**
     * 点击任意位置的回调
     */
    private blockInputCallback: () => void;

    private home: Home;
    onLoad() {
        this.home = this.node.getComponent(Home)
    }
    /**
     * 执行对话
     */
    public exDialog() {
        this.dialog(0)
        this.mBtSpine.node.parent.active = false;
    }
    public openMenu() {
        this.mNode[1].children[0].active = false;
        cc.tween(this).sequence(
            cc.delayTime(0.1),
            cc.callFunc(() => {
                this.mBtSpine.setAnimation(0, 'idle', true);
            })
        ).start()
    }

    private dialog(index: number) {
        if (index >= this.text.length) {
            this.setBlockInputCallback(null);
            this.moveToMx(() => {
                this.mBlockInputEvents.active = false;
                this.guideMX()
            })
            return;
        }
        let texts = this.text[index].split('|')
        let mid = parseInt(texts.shift());
        let curNode = this.mNode[mid]
        this.mBlockInputEvents.active = true
        this.showDialog(texts, curNode, null, () => {
            index++;
            this.dialog(index);
        });
    }
    /**
     * 对象头顶显示对话
     */
    private showDialog(dialogs: Array<string>,//对话内容
        curNode: cc.Node,//显示在地图中的位置
        spine: sp.Skeleton,//当前动画精灵
        callback: () => void)//对话结束后的回调
    {
        let node = cc.instantiate(this.mMapDialog);
        node.zIndex = 9999;
        node.y += curNode.height
        node.parent = curNode;
        let obo = node.getComponent(WOneByone);
        obo.dialogs = dialogs;
        obo.spine = spine;
        obo.setCallback(() => {
            node.destroy();
            callback();
        });
        this.setBlockInputCallback(() => {
            obo.jump();
        });
        obo._start();
    }

    public setBlockInputCallback(blockInputCallback: () => void) {
        this.blockInputCallback = blockInputCallback
    }

    public onclickBlockInput() {
        if (this.blockInputCallback) {
            this.blockInputCallback()
        }
    }
    /**
     * 幽灵移动到冒险按钮
     */
    private moveToMx(callback: () => void) {
        //1.幽灵变身
        let spine = this.mNode[1].children[0].getComponent(sp.Skeleton)
        spine.setAnimation(0, 'escape', false);

        spine.setCompleteListener(() => {
            spine.setCompleteListener(null)
            spine.setAnimation(0, 'fly', true);
            let pos = this.mBtMx.convertToWorldSpaceAR(cc.v2(0, 0));
            let winSize = cc.winSize;
            pos.x -= winSize.width / 2;
            pos.y -= winSize.height / 2;
            cc.tween(this.mNode[1]).sequence(
                cc.moveTo(1, pos),
                cc.callFunc(() => {
                    this.openMenu1()
                    callback()
                })
            ).start()
        })
    }
    private openMenu1() {
        this.mBtSpine.node.parent.active = true;
        this.mBtSpine.setCompleteListener(()=>{
            this.mBtSpine.setAnimation(0, 'idle', false);
        })
        this.mBtSpine.setAnimation(0, 'open', false);
    }
    /**
     * 强制引导到冒险
     */
    private guideMX() {
        let main = this.home.main
        let guideMask = main.mGuideMask
        guideMask.setTargetNode(this.mBtMx)
        guideMask.show()

        this.home.setMxCallback(() => {
            guideMask.close()
            this.home.setMxCallback(null)

            FqLogin.commitEvent('jinruguanqia','','')
        })
    }


}
