
import FSprite, { SpriteActionType } from "../../object/FSprite";
import PSprite from "../../object/PSprite";
import WOneByone from ".././map1/WOneByone";
import BaseEvent from ".././base/BaseEvent";
/**
 * 牢笼,可解救伙伴
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FCage extends BaseEvent {

    @property(cc.Prefab)
    mMapDialog: cc.Prefab = null;
    
    @property(sp.Skeleton)
    spine: sp.Skeleton = null;//牢笼

    @property(cc.Node)
    mPet: cc.Node = null;//伙伴

    @property({
        type:[cc.Prefab],
        displayName: '解救的对象'
    })
    mMonster: Array<cc.Prefab> = [];

    @property(cc.Node)
    guides: Array<cc.Node> = [];//引导标识

    @property({
        displayName: '靠近的提示',
        type:cc.Sprite
    })
    mIcon: cc.Sprite = null;//靠近后的提示

    @property({
        type:[cc.SpriteFrame],
        displayName: '不同状态的图标'
    })
    mIconFrame: Array<cc.SpriteFrame> = [];

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

    private isOver = false;
    onLoad() {
        super.onLoad()
        if(this.mIcon){
            this.mIcon.node.active = false;
        }
    }

    onBegin(tag:number){
        if(this.isOver){
            return;
        }
        if(tag == 1){
            this.showButton();
        }
        
    }
    onEnd(tag:number){
        if(tag == 1){
            this.closeButton();
        }
    }

    public showButton(){
        this.iconTips(true);

        this.showOpt(this.mTipsIcon,()=>{
            this.openCage()
        })
    }
    public closeButton(){
        this.iconTips(false);
        this.closeOpt()
    }
    /**
     * 
     * @param show 是否显示提示
     */
    private iconTips(show){
        if(this.mIcon){
            if(show){
                this.mIcon.node.active = true;
                let head = this.ff.mFFheader;
                let count = head.getTmpCount(2001);
                if(count > 0){
                    this.mIcon.spriteFrame = this.mIconFrame[0]
                }else{
                    this.mIcon.spriteFrame = this.mIconFrame[1]
                }
            }else{
                this.mIcon.node.active = false;
            }
        }
    }


    /**
     * 事件结束，清除事件
     */
    public delEvent(){
        this.node.removeComponent(cc.PhysicsBoxCollider);
    }
    //打开牢笼
    private openCage(){
        this.closeButton();
        let head = this.ff.mFFheader;
        let count = head.getTmpCount(2001);
        if(count > 0){
            this.isOver = true;
            for (let i = 0; i < this.guides.length; i++) {
                const element = this.guides[i];
                element.destroy();
            }
            this.guides = [];
    

            this.spine.setCompleteListener(() => {
                this.spine.setCompleteListener(null);
                this.startStory();
            });
            this.spine.setAnimation(0, 'open', false);
        }else{
            this.ff.main.showTips('我需要一把牢笼钥匙');
        }
    }


    public startStory(){
        /**
         * 暂停所有
         */
        this.ff.pauseSprite(true);
        this.ff.mBlockInputEvents.active = true;
        this.movePet();
    }

    private movePet(){
        let anim = this.mPet.getComponent(cc.Animation);
        let spine = this.mPet.getComponent(sp.Skeleton);

        spine.setAnimation(0, SpriteActionType.run, true);
        anim.on('finished',this.onFinished,this);
        anim.play('cage_pet_move');
    }
    private onFinished(num, string){
        let anim = this.mPet.getComponent(cc.Animation);
        let spine = this.mPet.getComponent(sp.Skeleton);
        anim.off('finished',this.onFinished,this);
        spine.setAnimation(0, SpriteActionType.stand, true);

        this.showDialog2();
    }
    private showDialog2(){
        let dialogs = [
            '伟大的古尔薇格，谢谢你把小女巫送来',
            '我是琳达，这是鲍西',
            '敌人抓我们时，为了保护我，这个傻子冲到了最前面第一个被打晕了，我也被他们抓了',
            '还好你及时赶到，救了我们',
            '我们还有个同伴，露西，正在前面与敌人作战',
            '请你过去帮帮她',
        ];

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + this.mPet.x;
        node.y = this.node.y + this.mPet.y + this.mPet.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);
        
        obo.dialogs = dialogs;
        obo.setCallback(()=>{
            node.destroy();
            this.ff.setBlockInputCallback(null);
            this.ff.pauseSprite(false);
            this.ff.mBlockInputEvents.active = false;

           
            this.node.removeComponent(cc.PhysicsBoxCollider);

            // this.getPet();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }


    /**
     * 解救伙伴
     */
    private getPet(){
        this.delEvent()
        let head = this.ff.mFFheader;

        let mainSprite = this.ff.mainSprite;
        let pos = mainSprite.node.getPosition()
        pos.x -= this.node.x;
        pos.y -= this.node.y;
        let spine = this.mPet.getComponent(sp.Skeleton);
        spine.setAnimation(0, SpriteActionType.run, true);
        cc.tween(this.mPet).sequence(
            cc.moveTo(1,pos),
            cc.callFunc(()=>{
                head.removeTmpGood(2001,1);
                this.mPet.removeFromParent();
                this.mMonster.forEach(element => {
                    let node = cc.instantiate(element);
                    let sp = node.getComponent(PSprite);
                    this.ff.addRole(sp);
                });
            })
        ).start()

    }




}
