import { getRepository } from 'typeorm';
import { GameEntityTypeEntity } from '../entities/game-entity-type.entity';
import { logSystem } from './log';

export class PreloadSystem {
    private gameEntityTypesMap: {
        [key: string]: number;
    } = {};

    async init(): Promise<void> {
        const gameEntities = await getRepository(GameEntityTypeEntity).find();
        for (const gameEntity of gameEntities) {
            this.gameEntityTypesMap[gameEntity.name] = gameEntity.id;
        }

        logSystem.initLog('Preload System', `Loaded ${gameEntities.length} game entity types.`);
    }

    getGameEntityTypeId(name: string): number {
        return this.gameEntityTypesMap[name];
    }

    getGameEntityTypeById(id: number): string {
        return Object.entries(this.gameEntityTypesMap).find((type) => type[1] === id)?.[0] ?? 'unknown';
    }
}

export const preloadSystem = new PreloadSystem();
