import { HttpStateType, ReveData } from "../../../../util/CHttp";
import FF from "../../FF";
import FMap from "../../map/FMap";
import FSprite from "../../object/FSprite";
import WOneByone from "../map1/WOneByone";

/**
 * 打开附加关卡
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FExtra extends cc.Component {
    @property({
        type:cc.Integer,
        displayName: '附加关卡id'
    })
    public extraId:number = 1001;

    @property({
        type:cc.Node,
        displayName: '开启特效'
    })
    public spine:cc.Node = null;

    @property({
        type:cc.Node,
        displayName: '任务提示'
    })
    public iconTouch:cc.Node = null;

    /**
     * 控制的栅栏
     */
    @property([cc.Node])
    mFenceTrigger: Array<cc.Node> = [];

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

    private ff:FF
    private map:FMap;
    onLoad () {
        this.spine.active = false;
        this.map = this.node.parent.parent.getComponent(FMap);
    }

    start () {
        let ff = this.map.ff;
        let stage = ff.main.player.stage;
        if(stage.extraStage.indexOf(this.extraId) >= 0){
            this.node.active = false;
            this.mFenceTrigger.forEach(element => {
                element.active = false;
            });
        }else{
            this.node.active = true;
        }
    }

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider){
        if(other.node.group == 'A'){
            let obj = other.node.getComponent(FSprite);
            this.ff = obj.ff;
            if(obj == this.ff.mainSprite){
                this.showButton();
            }
        }
    }
    onEndContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider){
        if(other.node.group == 'A'){
            let obj = other.node.getComponent(FSprite);
            
            this.ff = obj.ff;
            if(obj == this.ff.mainSprite){
                this.closeButton();
            }
        }
    }
    public showButton(){
        this.ff.control.mEventButton.node.active = true;
        this.ff.control.mEventButton.setCallback(()=>{
            this.startStory();
        });
    }
    public closeButton(){
        this.ff.control.mEventButton.node.active = false;
    }
    public startStory(){
        /**
         * 暂停所有
         */
        this.ff.pauseSprite(true);
        this.iconTouch.active = false;
        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;
        node.y = this.node.y + this.node.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);
        
        obo.dialogs = dialogs;
        obo.setCallback(()=>{
            node.destroy();
            this.playAction();
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }
    private exitExtra(){
        
        this.ff.pauseSprite(false);
        this.ff.mBlockInputEvents.active = false;
        this.node.removeComponent(cc.PhysicsBoxCollider);
    }

    private playAction(){
        this.spine.active = true;
        let spine = this.spine.getComponent(sp.Skeleton);
        spine.setCompleteListener(() => {
            spine.setCompleteListener(null);
            this.spine.active = false;
            
            this.openExtraStage();
        });
        spine.setAnimation(0, 'vs_direct', false);
    }

    private moveCarmea(){
        let map = this.ff.mMap;
        let camera = map.mCamera;

        let cameraPos = this.mFenceTrigger[0];
        let pos = cc.v2();
        let winsize = cc.winSize;
        pos.x = cameraPos.x-winsize.width/2;
        pos.y = cameraPos.y-winsize.height/2;

        cc.tween(camera.node).sequence(
            cc.moveTo(1.5,pos),
            cc.callFunc(()=>{
                this.mFenceTrigger.forEach(element => {
                    this.showFence(element,'close');
                });
            }),
            cc.delayTime(1),
            cc.callFunc(()=>{
                this.mFenceTrigger.forEach(element => {
                    element.active = false;
                });
                this.exitExtra();
            })
        ).start();
    }
    private showFence(element,action){
        // let nodes = element.children;
        // nodes.forEach(tmp => {
        //     let spine = tmp.getComponent(sp.Skeleton);
        //     if(spine){
        //         spine.setAnimation(0, action, false);
        //     }
        // });
    }
    /**
     * 打开关卡
     */
    private openExtraStage(){
        let msg = {
            id:this.extraId
        }
        let ff = this.ff;
        ff.main.gameHttp.sendJson('stage/v1/openExtraStage',msg,(state,reve:ReveData)=>{
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    let player = ff.main.player;
                    let stage = player.stage;
                    stage.extraStage.push(this.extraId);
                    this.moveCarmea();
                }else{
                    this.exitExtra();
                    ff.main.showTips(reve.message);
                }
            }else{
                this.exitExtra();
                ff.main.showTips('网络异常');
            }
        });
    }
}
