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

/**
 * 闲聊脚本
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FDialogNoneNPC extends BaseEvent {

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

    @property({
        displayName: '对话内容',
        type: [cc.String]
    })
    text: Array<string> = [];

    @property({
        displayName: '靠近的提示',
        type: cc.Node
    })
    icon: cc.Node = null;

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

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

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

    onLoad() {
        super.onLoad()
        if (this.icon) {
            this.icon.active = false;
        }
    }

    onBegin(tag: number) {
        if (tag == 1) {
            this.showOpt(this.mTipsIcon, () => {
                this.startDialog()
            })
        } else if (tag == 2) {
            if (this.icon) {
                this.icon.active = true;
            }
        }

    }
    onEnd(tag: number) {
        if (tag == 1) {
            this.closeOpt()
        } else if (tag == 2) {
            if (this.icon) {
                this.icon.active = false;
            }
        }
    }

    public startDialog() {
        if (this.icon) {
            this.icon.active = false;
        }
        this.dialog1();
    }

    public closeButton() {
        if (this.icon) {
            this.icon.active = false;
        }
        this.closeOpt()
    }

    private dialog1(index: number = 0) {
        if (index >= this.text.length) {
            this.npcFly(() => {
                this.openmFenceTrigger();  
                this.spine.node.parent.active = false;
                this.closeOpt();
            });
            this.closeButton();
            this.resume();
            return;
        }
        let texts = this.text[index].split('|')
        let mid = parseInt(texts.shift());
        if (mid == -1) {//主角
            let my = this.ff.mainSprite.node;
            this.showDialog(my, texts, () => {
                index++;
                this.dialog1(index);
            });
        } else {
            this.showDialog(this.node, texts, () => {
                index++;
                this.dialog1(index);
            });
        }
    }

    npcFly(callBack: Function) {
        //1.幽灵变身
        this.spine.setAnimation(0, 'escape', false);
        this.spine.setCompleteListener(() => {
            this.spine.setCompleteListener(null)
            this.spine.setAnimation(0, 'fly', true);
            let pos = cc.v2(-370, 460);
            cc.tween(this.spine.node).sequence(
                cc.moveTo(1, pos),
                cc.callFunc(() => {
                    callBack && callBack();
                })
            ).start()
        })
    }

    public openmFenceTrigger() {
        if (this.mFenceTrigger.length <= 0) {
            return
        }
        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, 'close');
                    }
                    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;

                    }
                    FqLogin.commitEvent(this.node.name, '', '');
                })
            ).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);
            }
        }
    }

}
