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

/**
 * 对话开门
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FOpenDoorChat extends BaseEvent{

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

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

    @property({
        displayName: '完成后的对话',
        type:[cc.String]
    })
    finish: Array<string> = [];//完成后的对话

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

    @property({
        displayName: '驱赶条件对象',
        type:FOpenDoorCheck
    })
    mCheck: FOpenDoorCheck = null;

    @property({
        displayName: '需要打开的门',
        type:cc.Node
    })
    mOpenDoor: cc.Node = null;

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

    onLoad(){
        super.onLoad()
    }

    onBegin(tag:number){
        if(!this.mOpenDoor.active){
            return
        }
        if(tag == 1){
            this.showOpt(this.mTipsIcon,()=>{
                this.closeOpt()
                this.pause()
                this.startDialog()
            })
        }
    }
    onEnd(tag:number){
        if(tag == 1){
            this.closeOpt()
        }
    }

    public startDialog(){
        if(this.mCheck.mDriveNode.active){
            this.dialog1(0);
        }else{
            this.dialog2(0);
        }
    }

    private dialog1(index:number){
        if(index >= this.text.length){
            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);
            });
        }
    }

    private dialog2(index:number){
        if(index >= this.finish.length){
            this.spine.setCompleteListener(()=>{
                this.spine.setCompleteListener(null)
                this.spine.destroy()
                this.moveCamera(this.mOpenDoor.getPosition(),1,()=>{
                    this.openGear()
                })
            })
            this.spine.setAnimation(0,'escape2',false)
            return;
        }
        let texts = this.finish[index].split('|')
        let mid = parseInt(texts.shift());
        if(mid == -1){//主角
            let my = this.ff.mainSprite.node;
            this.showDialog(my,texts,()=>{
                index ++;
                this.dialog2(index);
            });
        }else{
            this.showDialog(this.node,texts,()=>{
                index ++;
                this.dialog2(index);
            });
        }
    }

    private openGear() {
        if(this.icon){
            this.icon.active = false;
        }
        let nodes = this.mOpenDoor.children;
        nodes.forEach(element => {
            let spine = element.getComponent(sp.Skeleton);
            if (spine) {
                spine.setCompleteListener(() => {
                    element.active = false;
                });
                spine.setAnimation(0, 'close', false);
            }
        });
        this.ff.main.playerEffectByPath(AudioMgr.openDoor);
        cc.tween(this.ff.mMap.node).sequence(
            cc.delayTime(1),
            cc.callFunc(() => {
                this.mOpenDoor.active = false;
                this.resume()
                this.node.destroy()
                FqLogin.commitEvent(this.node.name,'','');
            })
        ).start()
    }
}
