import FF from "../FF";
import { GroupType } from "../object/FObject";
import FSprite, { SpriteActionType } from "../object/FSprite";
import WOneByone from "./map1/WOneByone";

/**
 * 伙伴间的对话
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FDialogPet extends cc.Component {

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

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

    @property({
        displayName: '移除伙伴id',
        type:[cc.Integer]
    })
    mRemoveId: Array<number> = [];//对话结束移出的伙伴id

    private ff:FF

    private isOver = false;

    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider){
        if(this.isOver){
            return
        }
        if(otherCollider.node.group == 'A'){//主角踩到机关
            let obj = otherCollider.node.getComponent(FSprite);
            this.ff = obj.ff;
            if(obj == this.ff.mainSprite){
                this.node.removeComponent(cc.PhysicsBoxCollider);
                this.ff = obj.ff;
                this.isOver = true;
                this.startFight();
            }
        }
    }
    /**
     * 开始
     */
    private startFight(){
        this.ff.pauseSprite(true);
        this.dialog(0);
    }
    //移除伙伴
    private removePet(){
        for (let i = 0; i < this.mRemoveId.length; i++) {
            const id = this.mRemoveId[i];
            this.ff.removePet(id);
        }
    }


    private dialog(index:number){
        if(index >= this.text.length){
            this.ff.mBlockInputEvents.active = false;
            this.ff.pauseSprite(false);
            this.node.destroy();
            this.removePet();
            return;
        }
        let mysprites = this.ff.getGroupBy(GroupType.A);
        let texts = this.text[index].split('|')

        let action = texts.shift();
        let mid = parseInt(action);
        let my = mysprites[mid].node;
        
        this.showDialog(my,texts,()=>{
            index ++;
            this.dialog(index);
        });
    }
    private showDialog(my:cc.Node,dialogs,fCallback:()=>void){
        let d1 = dialogs[0] as string;
        if(d1.indexOf('#') == 0){//第一个字符为#表示表情
            let sprite = my.getComponent(FSprite)
            let spine = sprite.spine;
            if(spine){
                spine.setCompleteListener(()=>{
                    spine.setAnimation(0, SpriteActionType.stand, true);
                    fCallback();
                })
                let action = d1.replace('#','');
                spine.setAnimation(0,action, false);
            }else{
                fCallback();
            }
        }else{
            this.ff.mBlockInputEvents.active = true;
            let node = cc.instantiate(this.mMapDialog);
            node.group = 'map'
            node.zIndex = 9999;
            node.x = my.x;
            node.y = my.y + my.height;
            node.parent = this.ff.mMap.mSprites;
            let obo = node.getComponent(WOneByone);
            
            obo.dialogs = dialogs;
            obo.setCallback(()=>{
                node.destroy();
                this.ff.setBlockInputCallback(null);
                fCallback();
            });
            this.ff.setBlockInputCallback(()=>{
                obo.jump();
            });
            obo._start();
        }
    }
}
