
import i18n from "../../../../i18n/i18n";
import DialogButton from "../../box/DialogButton";
import { SpriteActionType } from "../../object/FSprite";
import WOneByone from "../map1/WOneByone";
import GuideBase from "./GuideBase";

/**
 * 第一个引导剧情
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class Guide1 extends GuideBase {

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

    @property(sp.Skeleton)
    spine: sp.Skeleton = null;//餐桌动画

    @property(cc.Node)
    speak1: cc.Node = null;//1:洛基：祝众神之王与王后，仙福永享寿与天齐

    @property([cc.Node])
    speak2: Array<cc.Node> = [];//2:干杯

    @property(cc.Node)
    speak3: cc.Node = null;//3小兵:报告，巨人来袭

    @property(cc.Node)
    speak3Move: cc.Node = null;//小兵目标位置

    @property(cc.Node)
    speak4: cc.Node = null;//4 洛基：跑……

    @property(cc.Node)
    speak5: cc.Node = null;//5 提尔：索尔：杀……

    @property(cc.Node)
    speak6: cc.Node = null;//5 芙蕾雅，弗雷：……

    @property(cc.Node)
    speak7: cc.Node = null;//6 npc

    @property(cc.Prefab)
    mChoiseDialog: cc.Prefab = null;//选择对话框
    //播放炸弹动画
    //主角入场

    onLoad(){
        this.node.zIndex = -9999;
    }

    public run(){
        let map = this.ff.mMap;
        let winSize = cc.winSize
        let camera = map.mCamera;
        camera.node.x = this.node.x - winSize.width/2;
        camera.node.y = this.node.y - winSize.height/2;

        this.ff.pauseSprite(true);
        this.ff.mBlockInputEvents.active = true;
        this.showDialog1();

    }

    private showDialog1(){
        let dialogs = [
            '干的不错',
            '身为阿萨神族的守护者，保卫阿斯加德不受邪恶的侵扰是我们的职责',
            '你叫什么名字？',
            '真不错，听名字就知道有前途',
            '有朝一日，希望你能成为阿斯加德新的守护神……',
            '来，一起祝众神之王与王后，仙福永享寿与天齐',
        ];

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + this.speak1.x;
        node.y = this.node.y + this.speak1.y + this.speak1.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.showDialog2();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }

    private showDialog2(){
        let dialogs = [
            '干杯',
        ];
        let nodes:Array<cc.Node> = []
        for (let i = 0; i < this.speak2.length; i++) {
            const element = this.speak2[i];
            let node = cc.instantiate(this.mMapDialog);
            node.group = 'map'
            node.zIndex = 9999;
            node.x = this.node.x + element.x;
            node.y = this.node.y + element.y + element.height;
            node.parent = this.ff.mMap.mSprites;
            let obo = node.getComponent(WOneByone);
            
            obo.dialogs = dialogs;
            nodes.push(node);
            obo._start();
        }
        this.ff.setBlockInputCallback(null);
        
       cc.tween(this.node).sequence(
           cc.delayTime(1),
           cc.callFunc(()=>{
               nodes.forEach(da => {
                   da.destroy();
               });
                this.showDialog3();
           })
       ).start();
       
    }
    //小兵跑出来，然后说话
    private showDialog3(){
        
        cc.tween(this.speak3).sequence(
            cc.moveTo(1,this.speak3Move.getPosition()),
            cc.callFunc(()=>{
                this.showDialog3_1()
            })
        ).start()
    }

    private showDialog3_1(){
        let dialogs = [
            '报…报告……',
            '报告各位大神，邪恶四巨兽军团正在接近…',
            '从彩虹桥正面攻入…',
            '请…请…请……',
        ];

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + this.speak3.x;
        node.y = this.node.y + this.speak3.y + this.speak3.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);
        
        obo.dialogs = dialogs;
        obo.setCallback(()=>{
            node.destroy();
            this.ff.setBlockInputCallback(null);
            this.showDialog4();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }

    private showDialog4(){
        let dialogs = [
            '这是什么鬼东西',
        ];

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + this.speak4.x;
        node.y = this.node.y + this.speak4.y + this.speak4.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.showDialog5();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }

    private showDialog5(){
        let dialogs = [
            '伟大的圣母玛利亚呀',
        ];

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + this.speak5.x;
        node.y = this.node.y + this.speak5.y + this.speak5.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.showDialog6();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }
    private showDialog6(){
        let dialogs = [
            '……',
        ];

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + this.speak6.x;
        node.y = this.node.y + this.speak6.y + this.speak6.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.showDialog7();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }
    //播放炸弹
    private showDialog7(){
        this.spine.setCompleteListener(() => {
            this.spine.setCompleteListener(null);
            this.spine.setAnimation(0, 'idle', true);
            this.dead();
            this.mainMove();
        });
        this.spine.setAnimation(0, 'in', false);
    }

    private dead(){
        let spines = this.getAllSpine();

        for (let i = 0; i < spines.length; i++) {
            const spine = spines[i];
            spine.setCompleteListener(()=>{
                spine.setCompleteListener(null);
                spine.setAnimation(0, SpriteActionType.yun2, true);
            });
            spine.setAnimation(0, SpriteActionType.yun, false);
        }
    }
    private getAllSpine():Array<sp.Skeleton>{
        let nodes = this.node.children;
        let spines = [];
        for (let i = 0; i < nodes.length; i++) {
            const node = nodes[i];
            let spriteNode = node.getChildByName('juese01');
            if (spriteNode) {
                spriteNode.zIndex = 2
                let spineNode0 = spriteNode.getChildByName('spineRight');
                if(spineNode0){
                    let spine = spineNode0.getComponent(sp.Skeleton);
                    spines.push(spine);
                }
            }
        }
        return spines;
    }
    //主角移动进场
    private mainMove(){
        let mainSprite = this.ff.mainSprite;

        let pos = cc.v2()
        pos.x = this.node.x + this.speak3Move.x;
        pos.y = this.node.y + this.speak3Move.y;
        mainSprite.playAction2(SpriteActionType.run);
        cc.tween(mainSprite.node).sequence(
            cc.moveTo(1,pos),
            cc.callFunc(()=>{
                mainSprite.playAction2(SpriteActionType.stand);
                this.showDialog8();
            })
        ).start();
    }


    private showDialog8(){
        let dialogs = [
            '危险，保护众神之王，保护众神之母',
            '保护，保护……',
        ];

        let mainSprite = this.ff.mainSprite;

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = mainSprite.node.x;
        node.y = mainSprite.node.y + mainSprite.node.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.showDialog9();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }

    private showDialog9(){
        this.speak7.active = true;
        let spine = this.speak7.getChildByName('chuansongmen1');
        spine.active = true;
        let monster = this.speak7.getChildByName('monster56');
        monster.active = true;
        monster.opacity = 0;

        cc.tween(monster).sequence(
            cc.fadeIn(1),
            cc.callFunc(()=>{
                this.showDialog9_1();
            })
        ).start();
    }
    private showDialog9_1(){
        let dialogs = [
            '亲爱的孩子，谁来保护你呢？',
        ];
        let mainNode = this.speak7;

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + mainNode.x;
        node.y = this.node.y + mainNode.y + mainNode.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.showDialog9_1_1();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }
    private showDialog9_1_1(){
        let node:cc.Node = cc.instantiate(this.mChoiseDialog)
        node.parent = this.ff.node
        let dialog = node.getComponent(DialogButton);
        dialog.setCallback(()=>{
            node.destroy();
            this.showDialog9_1_2();
        })
    }
    private showDialog9_1_2(){
        let dialogs = [
            '剧情要改……',
        ];
        let mainNode = this.speak7;

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + mainNode.x;
        node.y = this.node.y + mainNode.y + mainNode.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.showDialog9_2_1();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }
    private showDialog9_2_1(){
        let node = this.speak7.getChildByName('1')
        node.active = true;
        let chuansongmen2 = this.speak7.getChildByName('chuansongmen2');
        chuansongmen2.active = true;
        this.ff.setBlockInputCallback(()=>{
            node.active = false;
            chuansongmen2.active = false;
            this.showDialog9_2_2();
        });
    }
    private showDialog9_2_2(){
        let dialogs = [
            '哎呀，搞错了',
            '再来',
        ];
        let mainNode = this.speak7;

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + mainNode.x;
        node.y = this.node.y + mainNode.y + mainNode.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.showDialog9_2_3();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();

    }
    private showDialog9_2_3(){
        let nodes:Array<cc.Node> = [];
        for (let i = 2; i <= 5; i++) {
            let node = this.speak7.getChildByName(''+i)
            nodes.push(node)
        }
        let index = 0;
        let chuansongmen2 = this.speak7.getChildByName('chuansongmen2');
        chuansongmen2.active = true;
        nodes[0].active = true;

        let dialogs = [
            '巨狼芬尼尔',
            '黑龙尼德霍格',
            '尘世巨蟒耶梦加德',
            '死境之主海拉',

        ];
        let mainNode = this.speak7;

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + mainNode.x;
        node.y = this.node.y + mainNode.y + mainNode.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);
        obo.isOver = true;
        obo.label.string = i18n.t(dialogs[0])


        this.ff.setBlockInputCallback(()=>{
            index ++;
            if(index >= nodes.length){
                node.destroy();
                nodes[index-1].active = false;
                chuansongmen2.active = false;
                this.showDialog9_3();
            }else{
                nodes[index-1].active = false;
                nodes[index].active = true;
                obo.label.string = i18n.t(dialogs[index])
            }
        });

    }
    private showDialog9_3(){
        let dialogs = [
            '剧情要改',
        ];

        let mainNode = this.speak7;

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x + mainNode.x;
        node.y = this.node.y + mainNode.y + mainNode.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.speak7.active = false;
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }
}
