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

/**
 * 场景内碰撞事件基类
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class BaseEvent extends cc.Component {

    public ff: FF
    //当前处于碰撞区域的对象

    // public spriteList: Array<FSprite> = [];

    onLoad() {
        this.ff = this.node.parent.parent.getComponent(FMap).ff;
    }

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        // if (other.node.group == 'A' && self.tag == 1) {
        //     let obj = other.node.getComponent(FSprite);
        //     if (this.spriteList.indexOf(obj) == -1) {
        //         this.spriteList.push(obj);
        //     }
        // }
        if (other.node.group == 'A' && other.tag == 1) {
            let obj = other.node.getComponent(FSprite);
            if (obj == this.ff.mainSprite) {
                this.onBegin(self.tag)
            }
        }
    }

    onEndContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        // if (other.node.group == 'A' && self.tag == 1) {
        //     let obj = other.node.getComponent(FSprite);
        //     this.removeSprite(obj);
        // } 
        if (other.node.group == 'A' && other.tag == 1) {
            let obj = other.node.getComponent(FSprite);
            if (obj == this.ff.mainSprite) {
                this.onEnd(self.tag)
            }
        }
    }

    // private removeSprite(sprite: FSprite) {
    //     for (let i = 0; i < this.spriteList.length; i++) {
    //         const element = this.spriteList[i];
    //         if (element == sprite) {
    //             this.spriteList.splice(i, 1);
    //             break
    //         }
    //     }
    // }
    /**
     * 主角进入碰撞区域
     *  @param tag 碰撞组件编号
     */
    public onBegin(tag: number) {

    }
    /**
     * 主角离开碰撞区域
     * @param tag 碰撞组件编号
     */
    public onEnd(tag: number) {

    }
    /**
     * 移动摄像机
     * @param pos 
     * @param callback 
     */
    public moveCamera(pos: cc.Vec2, time: number, callback: () => void) {
        let map = this.ff.mMap;
        let camera = map.mCamera;

        let winsize = cc.winSize;
        pos.x -= winsize.width / 2;
        pos.y -= winsize.height / 2;

        cc.tween(camera.node).sequence(
            cc.moveTo(time, pos).easing(cc.easeOut(time)),
            cc.callFunc(() => {
                callback();
            })
        ).start()
    }

    /**
     * 显示界面上可操作按钮
     */
    public showOpt(spriteFrame: cc.SpriteFrame, callback: () => void) {
        this.ff.control.showEventBt(spriteFrame, callback)
    }
    /**
     * 关闭界面上可操作按钮
     */
    public closeOpt() {
        this.ff.control.closeEventBt()
    }
    /**
     * 全屏按钮（用于处理用户点击任意位置）
     * @param callback 
     */
    public showBlockInput(callback: () => void) {
        this.ff.mBlockInputEvents.active = true;
        this.ff.setBlockInputCallback(callback);
    }
    public closeBlockInput() {
        this.ff.mBlockInputEvents.active = false;
        this.ff.setBlockInputCallback(null)
    }

    /**
     * 暂停当前游戏
     */
    public pause() {
        this.ff.pauseSprite(true)
    }
    /**
     * 恢复游戏
     */
    public resume() {
        this.ff.pauseSprite(false)
    }

    /**
     * 根据名字查找精灵节点
     * @param name 
     */
    public findByName(name: string) {
        return cc.find(name, this.ff.mMap.mSprites)
    }

    /**
     * 对象头顶显示对话
     */
    public showDialog(node: cc.Node, dialogs: Array<string>, callback: () => void) {
        let mapDialog = new FMapDialog(this.ff, this.ff.mMapDialog);
        let pos = cc.v2();
        pos.x = node.x
        pos.y = node.y + node.height;
        mapDialog.showDialog(dialogs,
            pos,
            null,
            () => {
                callback()
            }
        );
    }
    /**
    * 对象头顶显示对话
    */
    public showDialogPos(pos: cc.Vec2, dialogs: Array<string>, callback: () => void) {
        let mapDialog = new FMapDialog(this.ff, this.ff.mMapDialog);
        mapDialog.showDialog(dialogs,
            pos,
            null,
            () => {
                callback()
            }
        );
    }
    /**
     * 播放spine动画
     * @param spine 
     * @param callback 
     */
    public spineAction(spine: sp.Skeleton, actionName: string, callback: () => void) {
        spine.setCompleteListener(() => {
            spine.setCompleteListener(null)
            callback()
        })
        spine.setAnimation(0, actionName, false)
    }

    /**
     * 捡起地图上的物品
     * @param objectId 
     */
    public getMapObject(objectId: string, callback: Function) {
        let msg = {
            objectId: objectId
        }
        let main = this.ff.main
        main.gameHttp.sendJson('stage/v1/stageObject', msg, (state, reve: ReveData) => {
            main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    //"_stage":{"good":{"1001":1}}
                    let player = main.player;
                    let stage = player.stage;
                    stage.element.push(objectId);
                    if (reve.data._stage) {
                        stage.data[this.ff.mFFheader.stageData.id] = reve.data._stage;
                        this.ff.mFFheader.flush();
                    }

                    main.showReward(reve, () => {
                        callback && callback()
                    })
                } else {
                    main.showTips(reve.message);
                }
            } else {
                main.showTips('网络异常');
            }
        });
    }

}
