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

/**
 * 种树
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FPlantTrre extends BaseEvent {

    @property({
        displayName: '魔树动画',
        type: sp.Skeleton
    })
    mSpine: sp.Skeleton = null;

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

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

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

    @property({
        displayName: '钥匙',
        type: cc.Node
    })
    mKeyNode: cc.Node = null;

    onLoad() {
        super.onLoad()
        if(this.mSpine){
            this.mSpine.node.active = false
        }
        if(this.mGearNode){
            this.mGearNode.active = false
        }
        if(this.mKeyNode){
            this.mKeyNode.active = false
        }
    }

    onBegin(tag:number){
        if (tag == 1) {
            this.showOpt(this.mTipsIcon, () => {
                let head = this.ff.mFFheader;
                let count = head.getTmpCount(2007);
                if(count > 0){ //有魔豆的时候
                    head.removeTmpGood(2007,1)
                    this.showKey()
                }else{//没有魔豆
                    this.pause()
                    this.dialog(0)
                }
            })
        }
    }
    onEnd(tag:number){
        if (tag == 1) {
            this.closeOpt()
        }
    }
    private dialog(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.dialog(index);
            });
        }
    }

    private showKey(){
        this.pause()
        this.mSpine.node.active = true

        this.mSpine.setCompleteListener(()=>{
            this.mSpine.setCompleteListener(null)

            let pos = this.mGearNode.getPosition()
            this.moveCamera(pos,1,()=>{
                this.showKey1()
            })
        })
        this.mSpine.setAnimation(0,'grow',false)
    }

    private showKey1(){
        this.mGearNode.active = true
        this.mKeyNode.active = true

        this.mGearNode.y += 200;
        this.mKeyNode.y += 200;

        cc.tween(this.mGearNode).sequence(
            cc.moveBy(1,cc.v2(0,-200)),
            cc.callFunc(()=>{
                this.resume()
            })
        ).start()

        cc.tween(this.mKeyNode).sequence(
            cc.moveBy(1,cc.v2(0,-200)),
            cc.callFunc(()=>{
            })
        ).start()
    }



}
