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

/**
 * 藤蔓控制的门
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FOpenDoorVine extends BaseEvent {

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

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

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

    @property({
        displayName: '藤蔓出现的提示',
        type: cc.Node
    })
    mFindTips: cc.Node = null;

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

    @property({
        displayName: '出现的藤蔓',
        type: cc.Node
    })
    mGearNode: cc.Node = null;

    private isOver = false

    onLoad() {
        super.onLoad()
        if(this.mGearNode){
            this.mGearNode.active = false
        }
        if(this.mNearTips){
            this.mNearTips.active = false
        }
        if(this.mFindTips){
            this.mFindTips.active = false
        }
    }

    onBegin(tag:number){
        if(this.isOver){
            return
        }
        if (tag == 1) {
            if(this.mNearTips){
                this.mNearTips.active = false
            }
            this.showOpt(this.mTipsIcon, () => {
                this.closeOpt()
                this.pause()
                this.dialog(0)
            })
        }else if(tag == 2){
            if(this.mNearTips){
                this.mNearTips.active = true
            }
        }
    }
    onEnd(tag:number){
        if (tag == 1) {
            this.closeOpt()
        }else if (tag == 2) {
            if(this.mNearTips){
                this.mNearTips.active = false
            }
        }
    }

    private dialog(index:number){
        if(index >= this.text.length){
            if(this.mGearNode){
                this.vine()
            }
            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);
            });
        }
    }
    /**
     * 藤蔓出现
     */
    private vine(){
        let pos = this.mGearNode.getPosition()
        let target = this.mGearNode.children[0].children[0]
        this.mGearNode.active = true
        target.y += 100

        this.moveCamera(pos,1,()=>{
            cc.tween(target).sequence(
                cc.moveBy(1,cc.v2(0,-100)),
                cc.callFunc(()=>{
                    this.isOver = true
                    this.resume()
                    if(this.mFindTips){
                        this.mFindTips.active = true
                    }
                })
            ).start()
        })

    }

}