import { SevBack } from "../common/Xys";
import { GameServerModel } from "../server/GameServerModel";
import { LoginServerModel } from "../server/LoginServerModel";
import DialogModel from "./model/DialogModel";
import GuideModel from "./model/GuideModel";
import IDataModel from "../frameWork/model/IDataModel";
import LoadingModel from "./model/LoadingModel";
import LoginModel from "./model/Login/LoginModel";
import PlatformModel from "./model/PlatformModel";
import RedDotModel from "./model/RedDotModel";
import { AudioModel } from "./model/System/AudioModel";
import { SettingModel } from "./model/System/SettingModel";
import SystemModel from "./model/System/SystemModel";
import TaskModel from "./model/Task/TaskModel";
import TimeModel from "./model/TimeModel";
import UserModel from "./model/User/UserModel";
import WindowModel from "./model/WindowModel";
import BattleModel from "./model/Battle/BattleModel";
import OrderModel from "./model/OrderModel";
import ItemModel from "./model/Item/ItemModel";
import PageModel from "./model/PageModel";
import GridMapModel from "./model/GridMap/GridMapModel";
import ChatModel from "./model/Chat/ChatModel";

class GameDataCenter {
    _tModel: Array<IDataModel> = [];

    _rspModel: Map<string, IDataModel[]> = new Map<string, IDataModel[]>()

    sevBack: SevBack = null;

    audio: AudioModel = null
    setting: SettingModel = null
    system: SystemModel = null
    login: LoginModel = null
    time: TimeModel = null
    window: WindowModel = null
    loading: LoadingModel = null
    plat: PlatformModel = null
    reddot: RedDotModel = null
    user: UserModel;
    guide: GuideModel;
    task: TaskModel;
    dialog: DialogModel;
    gameServer: GameServerModel;
    loginServer: LoginServerModel;
    battle: BattleModel;
    order: OrderModel;
    page: PageModel;
    item: ItemModel;
    gridMap: GridMapModel;
    chat: ChatModel;
    constructor() {
        if (CC_PREVIEW) window["gdc"] = this;
    }

    newModel<T extends IDataModel>(c: { new(): T }): T {
        let obj = new c()
        this._tModel.push(obj);
        return obj
    }

    clear() {
        this._tModel.forEach(m => {
            m.clear();
        });
    }
    //初始化登录需要的模块
    initLoadModule() {
        this._tModel = []
        this.sevBack = null
        this.audio = this.newModel(AudioModel)
        this.setting = this.newModel(SettingModel)
        this.login = this.newModel(LoginModel)
        this.user = this.newModel(UserModel)
        this.plat = this.newModel(PlatformModel)
        this.window = this.newModel(WindowModel)
        this.loading = this.newModel(LoadingModel)
        this.time = this.newModel(TimeModel)
        this.loginServer = this.newModel(LoginServerModel);

        this.initLoadRspModel()
    }

    initModule() {
        this.system = this.newModel(SystemModel)
        this.order = this.newModel(OrderModel)
        this.task = this.newModel(TaskModel)
        this.chat = this.newModel(ChatModel);
        this.item = this.newModel(ItemModel);
        this.guide = this.newModel(GuideModel)
        this.dialog = this.newModel(DialogModel)
        this.gameServer = this.newModel(GameServerModel);
        this.battle = this.newModel(BattleModel);
        this.page = this.newModel(PageModel)
        this.gridMap = this.newModel(GridMapModel)
        // 红点注册必须在其他model之后
        this.reddot = this.newModel(RedDotModel);
    }

    initLoadRspModel() {
        this._rspModel.set("win", [this.window])
        this._rspModel.set("time", [this.time])
    }

    setRspModel(actName: string, model: IDataModel) {
        if (this._rspModel.has(actName)) {
            //该act已经有model监听，直接push
            let modelList = this._rspModel.get(actName)
            if (modelList.indexOf(model) < 0) {
                modelList.push(model)
                this._rspModel.set(actName, modelList)
            } else {
                console.warn("模块:", model.getModelName(), " 已注册了=>", actName)
            }
        } else {
            this._rspModel.set(actName, [model])
        }

    }
}

export default new GameDataCenter();