import CMath from "../../../../util/CMath";
import BObject from "../../bullet/BObject";
import FF from "../../FF";
import FSprite, { SpriteActionType } from "../../object/FSprite";
import PSprite from "../../object/PSprite";
import FMapDialog from "../dialog/FMapDialog";
import WOneByone from "../map1/WOneByone";

/**
 * 剧情
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class Plot1 extends cc.Component {

    @property(sp.Skeleton)
    myspine: sp.Skeleton = null;

    @property(cc.Prefab)
    mBullet: cc.Prefab = null;//子弹

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

    @property(cc.Prefab)
    mEffect: cc.Prefab = null;//爆炸效果

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

    @property([cc.Node])
    mDx: Array<cc.Node> = [];

    @property(cc.SpriteFrame)
    mAppleIcon: cc.SpriteFrame = null;//苹果图标

    private ff:FF;

    onLoad () {

    }
    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider){
        if(other.node.group == 'A'){
            let obj = other.node.getComponent(FSprite);
            if(obj.hp > 0){
                this.ff = obj.ff;
                this.node.removeComponent(cc.PhysicsBoxCollider);
                this.run();
            }
        }
    }


    private run(){
        this.ff.pauseSprite(true);
        this.ff.mBlockInputEvents.active = true;

        let map = this.ff.mMap;
        let winSize = cc.winSize
        let camera = map.mCamera;
        let cx = this.node.x - winSize.width/2;
        let cy = this.node.y - winSize.height/2;

        cc.tween(camera.node).sequence(
            cc.moveTo(0.7,cc.v2(cx,cy)),
            cc.callFunc(()=>{
                this.d1();
            })
        ).start()

    }

    private d1(){
        let dialogs = [
            '解决她',
        ];
        let mapDialog = new FMapDialog(this.ff,this.mMapDialog);
        let pos = cc.v2();
        pos.x = this.node.x + this.mDx[0].x;
        pos.y = this.node.y + this.mDx[0].y + this.mDx[0].height;
        mapDialog.showDialog(dialogs,
                pos,
                null,
                ()=>{
                    this.playAni1();
                }
            );
    }
    /**
     * 怪物死亡
     * @param node
     */
    private mDead(node:cc.Node){
        let spine = node.getComponent(sp.Skeleton);

        spine.setCompleteListener(() => {
            node.destroy();
        });
        spine.setEventListener(null);
        spine.setAnimation(0, SpriteActionType.dead, false);

    }

    private playAni1(){
        let node = cc.instantiate(this.mBullet);
        let bObject = node.getComponent(BObject);
        let mHitEffect = bObject.mHitEffect;
        bObject.destroy();

        let p1 = this.myspine.node.getPosition();
        p1.y += this.myspine.node.height/2;

        let p2 = this.mDx[0].getPosition();
        p2.y += this.mDx[0].height/2;

        node.parent = this.node;
        let angle = CMath.getAngle(p1,p2)*180/Math.PI;
        node.angle = angle

        cc.tween(node).sequence(
            cc.moveTo(0.5,p2),
            cc.callFunc(()=>{
                node.destroy();
                let xnode = cc.instantiate(mHitEffect);
                xnode.x = this.mDx[0].x;
                xnode.y = this.mDx[0].y;
                xnode.parent = this.node;
                this.playAni2()
                this.mDead(this.mDx[0]);
            })
        ).start()
    }

    private playAni2(){
        let node = cc.instantiate(this.mBullet);
        let bObject = node.getComponent(BObject);
        let mHitEffect = bObject.mHitEffect;
        bObject.destroy();

        let p1 = this.myspine.node.getPosition();
        p1.y += this.myspine.node.height/2;

        let p2 = this.mDx[1].getPosition();
        p2.y += this.mDx[1].height/2;

        node.parent = this.node;
        let angle = CMath.getAngle(p1,p2)*180/Math.PI;
        node.angle = angle

        cc.tween(node).sequence(
            cc.moveTo(0.5,p2),
            cc.callFunc(()=>{
                node.destroy();
                let xnode = cc.instantiate(mHitEffect);
                xnode.x = this.mDx[1].x;
                xnode.y = this.mDx[1].y;
                xnode.parent = this.node;
                this.mDead(this.mDx[1]);
                this.d2()

            })
        ).start()
    }
    private d2(){
        this.myspine.node.scaleX = 1;
        let dialogs = [
            '妖...妖怪',
        ];
        let mapDialog = new FMapDialog(this.ff,this.mMapDialog);
        let pos = cc.v2();
        pos.x = this.node.x + this.mDx[2].x;
        pos.y = this.node.y + this.mDx[2].y + this.mDx[2].height;
        mapDialog.showDialog(dialogs,
                pos,
                null,
                ()=>{
                    this.d3();
                }
            );
    }
    private d3(){
        let dialogs = [
            '快...快跑',
        ];
        let mapDialog = new FMapDialog(this.ff,this.mMapDialog);
        let pos = cc.v2();
        pos.x = this.node.x + this.mDx[3].x;
        pos.y = this.node.y + this.mDx[3].y + this.mDx[3].height;
        mapDialog.showDialog(dialogs,
                pos,
                null,
                ()=>{
                    this.playAni3();
                }
            );
    }

    private playAni3(){
        let node = cc.instantiate(this.mBullet);
        let bObject = node.getComponent(BObject);
        let mHitEffect = bObject.mHitEffect;
        bObject.destroy();

        let p1 = this.myspine.node.getPosition();
        p1.y += this.myspine.node.height/2;

        let p2 = this.mDx[2].getPosition();
        p2.y += this.mDx[2].height/2;

        node.parent = this.node;
        let angle = CMath.getAngle(p1,p2)*180/Math.PI;
        node.angle = angle

        cc.tween(node).sequence(
            cc.moveTo(0.5,p2),
            cc.callFunc(()=>{
                node.destroy();
                let xnode = cc.instantiate(mHitEffect);
                xnode.x = this.mDx[2].x;
                xnode.y = this.mDx[2].y;
                xnode.parent = this.node;
                this.playAni4()
                this.mDead(this.mDx[2]);
            })
        ).start()
    }

    private playAni4(){
        let node = cc.instantiate(this.mBullet);
        let bObject = node.getComponent(BObject);
        let mHitEffect = bObject.mHitEffect;
        bObject.destroy();

        let p1 = this.myspine.node.getPosition();
        p1.y += this.myspine.node.height/2;

        let p2 = this.mDx[3].getPosition();
        p2.y += this.mDx[3].height/2;

        node.parent = this.node;
        let angle = CMath.getAngle(p1,p2)*180/Math.PI;
        node.angle = angle

        cc.tween(node).sequence(
            cc.moveTo(0.5,p2),
            cc.callFunc(()=>{
                node.destroy();
                let xnode = cc.instantiate(mHitEffect);
                xnode.x = this.mDx[3].x;
                xnode.y = this.mDx[3].y;
                xnode.parent = this.node;
                this.mDead(this.mDx[3]);

                this.mainMove();
            })
        ).start()
    }
    //主角移动进场
    private mainMove(){
        this.myspine.node.scaleX = -1
        let mainSprite = this.ff.mainSprite;

        let p1 = this.node.getPosition();
        p1.x -= 180;
        mainSprite.playAction2(SpriteActionType.run,true)
        cc.tween(mainSprite.node).sequence(
            cc.moveTo(1,p1),
            cc.callFunc(()=>{
                mainSprite.playAction2(SpriteActionType.stand,true)
                this.d4();
            })
        ).start()
    }

    private d4(){
        let dialogs = [
            '小巫女，你看到2个和我差不多大的人类吗？',
            '什么？你已经救了他们？',
            '太感谢你了，有没有什么我可以帮助你的？',
            '你要去传风带？',
            '#yun',
            '不要，我可不想跟你一起把小命丢了',
        ];
        let mapDialog = new FMapDialog(this.ff,this.mMapDialog);
        let pos = cc.v2();
        pos.x = this.node.x;
        pos.y = this.node.y  + this.node.height;
        mapDialog.showDialog(dialogs,
                pos,
                this.myspine,
                ()=>{
                    this.mainLose();
                }
            );
    }
    /**
     * 主角丢苹果在地上,然后飞到对方身上
     */
    private mainLose(){
        let node = new cc.Node();
        node.group = 'map';
        let sprite = node.addComponent(cc.Sprite);
        sprite.spriteFrame = this.mAppleIcon;
        let mainSprite = this.ff.mainSprite;
        node.x = mainSprite.node.x;
        node.y = mainSprite.node.y + mainSprite.node.height/2;
        node.parent = this.ff.mMap.mSprites;

        node.setScale(1.5);
        let jumpDis = CMath.getRandom(-100,100);
        let jumpBy = cc.jumpBy(0.5, cc.v2(jumpDis, 0), 100, 1);
        cc.tween(node).sequence(
            jumpBy,
            cc.delayTime(0.5),
            cc.callFunc(()=>{
                cc.tween(node).sequence(
                    cc.moveTo(0.5,this.node.getPosition()),
                    cc.callFunc(()=>{
                        this.d5()
                        node.destroy();
                    })
                ).start()
                cc.tween(node).repeatForever(
                    cc.rotateBy(1, 360)
                    ).start()
                })
        ).start()
    }


    private d5(){
        let dialogs = [
            '好吧，好吧，看在你救了我朋友的份上',
            '就陪你走一趟，耶稣菩萨保佑，我们一路平安',
            '出发',
        ];
        let mapDialog = new FMapDialog(this.ff,this.mMapDialog);
        let pos = cc.v2();
        pos.x = this.node.x;
        pos.y = this.node.y  + this.node.height;
        mapDialog.showDialog(dialogs,
                pos,
                this.myspine,
                ()=>{
                    this.getPet();
                }
            );
    }

 /**
     * 解救伙伴
     */
    private getPet(){
        let mainSprite = this.ff.mainSprite;
        let pos = mainSprite.node.getPosition()
        pos.x -= this.node.x;
        pos.y -= this.node.y;
        this.myspine.setAnimation(0, SpriteActionType.run, true);
        cc.tween(this.myspine.node).sequence(
            cc.moveTo(1,pos),
            cc.callFunc(()=>{
                this.myspine.node.removeFromParent();
                for (let i = 0; i < this.mMonster.length; i++) {
                    const element = this.mMonster[i];
                    let node = cc.instantiate(element);
                    let sp = node.getComponent(PSprite);
                    this.ff.addRole(sp);
                }
                this.node.destroy();
                this.ff.pauseSprite(false);
            })
        ).start()

    }

}
