import { GameObject } from "../GameObject/GameObject";

/**
 * @type Map<Number, GameObject>
 */
let GameObjects = new Map();

/**
 * @type Map<Number, GameObject>
 */
let TickGameObjects = new Map();

/**
 * @type Array<{major: Number, minor: Number}>
 */
let CleaningQueue = new Array();

let currentMajorId = 0;
let currentMinorId = 0;

function incId() {
    currentMinorId++;
    if(currentMinorId === Number.MAX_SAFE_INTEGER)
    {
        currentMinorId = 0;
        currentMajorId++;
    }
}

/**
 * adds game object to game state and inits it
 * @param {GameObject} gameObject 
 */
export function addGameObjectToGameState(gameObject) {
    gameObject.init_byGameState(currentMajorId, currentMinorId);
    GameObjects.set(currentMinorId, gameObject);
    if(gameObject.isTickEnabled())
    {
        TickGameObjects.set(currentMinorId, gameObject);
    }
    incId();
    gameObject.onInit();
}

export function gameStateObjectsCleaner() {
    let counter = 0;
    for (const i of CleaningQueue) {
        GameObjects.delete(i.minor);
        TickGameObjects.delete(i.minor);
        counter++;
    }
    CleaningQueue = new Array();
}


export function addToCleaningQueue(major_id, minor_id) {
    CleaningQueue.push({major: major_id, minor: minor_id});
}

/**
 * 
 * @returns all game objects
 */
export function getGameObjects() {
    return GameObjects;
}
/**
 * 
 * @returns all game objects
 */
export function getTickGameObjects() {
    return TickGameObjects;
}

