import Singleton from "../../../extensions/ngame/assets/ngame/util/Singleton";
import { app } from "../App";
import { GAction } from "../consts/GAction";
import { GActionType, GUIChatMessage, GUIChatMessageDTO } from "../consts/GActionType";
import BaseData from "./BaseData";

//聊天数据
export default class ChatData extends BaseData{

    static Event = "ChatData_Event_Message";

    //世界消息列表
    datas:GUIChatMessageDTO[] = [];

    onInit() {
        //监听聊天消息
        app.socket.on(GAction.CHAT_RECEIVE_MESSAGE,this.onChatReceiveMessage,this,GActionType.GUIChatMessageDTO);
    }

    
    //接受聊天消息
    onChatReceiveMessage(info:GUIChatMessageDTO){
        console.log(`ChatData - onChatReceiveMessage`,info.message);
        this.datas.push(info);
        app.event.emit(ChatData.Event,info);
    }

    //发送消息
    onSend(message:GUIChatMessage){
        app.socket.Send(GAction.CHAT_MESSAGE,message,GActionType.GUIChatMessage);
    }

    //监听接受消息
    on(receive:Function,target?: any){
        app.event.on(ChatData.Event,receive,target)
    }
    //取消
    off(receive:Function,target?: any){
        app.event.off(ChatData.Event,receive,target)
    }

}

