import BaseEvent from "../base/BaseEvent";

/**
 * 完成开门条件
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FOpenDoorCheck extends BaseEvent {

    @property({
        displayName: '需要的物品id'
    })
    public goodId = 2002;

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

    @property({
        displayName:'对话内容',
        type:[cc.String]
    })
    text: Array<string> = [];
    
    @property({
        displayName: '提示图标',
        type:cc.SpriteFrame
    })
    mTipsIcon: cc.SpriteFrame = null;

    @property({
        displayName: '驱赶的对象',
        type:cc.Node
    })
    mDriveNode: cc.Node = null;

    @property({
        displayName: '棍子',
        type:cc.Node
    })
    mGunNode: cc.Node = null;

    @property({
        displayName: '乌鸦',
        type:sp.Skeleton
    })
    mSpine: sp.Skeleton = null;

    onLoad(){
        super.onLoad()
        this.mGunNode.active = false
    }

    onBegin(tag:number){
        if(tag == 1){
            if(!this.mDriveNode.active){
                return
            }
            this.showOpt(this.mTipsIcon,()=>{
                this.closeOpt()
                let head = this.ff.mFFheader;
                let count = head.getTmpCount(this.goodId);
                if(count > 0){
                    this.ff.pauseSprite(true);
                    this.ff.mBlockInputEvents.active = true;
                    head.removeTmpGood(this.goodId,1);
                    this.mGunNode.active = true
                    this.mSpine.setAnimation(0, 'hurt', false);
                    cc.tween(this.mGunNode).sequence(
                        cc.moveBy(0.2,cc.v2(0,40)),
                        cc.moveBy(0.2,cc.v2(0,-40)),
                        cc.moveBy(0.2,cc.v2(0,40)),
                        cc.moveBy(0.2,cc.v2(0,-40)),
                        cc.moveBy(0.2,cc.v2(0,40)),
                        cc.moveBy(0.2,cc.v2(0,-40)),
                        cc.callFunc(()=>{
                            this.flyGo()
                        }),
                        cc.moveBy(0.2,cc.v2(0,20)),
                        cc.moveBy(0.2,cc.v2(0,-20)),
                        cc.fadeOut(1),
                    ).start()
                }else{
                    this.dialog(0)
                }
            })
        }
    }

    private flyGo(){
        this.ff.pauseSprite(false);
        this.ff.mBlockInputEvents.active = false;

        this.mSpine.setCompleteListener(()=>{
            this.mDriveNode.active = false
            this.pause()
            this.showDialog(this.ff.mainSprite.node,['乌鸦被赶走了，去告诉神秘人吧'],()=>{
                this.resume()
            });
        })
        this.mSpine.setAnimation(0, 'fly', false);
       
    }


    onEnd(tag:number){
        if(tag == 1){
            this.closeOpt()
        }
    }
    private dialog(index:number){
        if(index >= this.text.length){
            this.closeOpt()
            this.ff.pauseSprite(false);
            this.ff.mBlockInputEvents.active = false;
            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.dialog(index);
            });
        }
    }
}
