/**
 * Author: dkanus
 * Home repo: https://www.insultplayers.ru/git/AcediaFramework/AcediaCore
 * License: GPL
 * Copyright 2020-2023 Anton Tarasenko
 *------------------------------------------------------------------------------
 * This file is part of Acedia.
 *
 * Acedia is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License, or
 * (at your option) any later version.
 *
 * Acedia is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Acedia.  If not, see <https://www.gnu.org/licenses/>.
 */
class Global extends Object;

//! Class singleton instance of an object that would hold references to any API that do not depend
//! on [`Actor`]s.
//!
//! To overcome cumbersome syntax of UnrealScript we gather all functions we want to be global into
//! special "API objects" and store their single references inside this one, [`Global`]'s instance.
//! Providing reference to properly initialized [`Global`] object to all [`AcediaObject`]s and
//! [`AcediaActor`]s will give them convenient accessors to all Acedia API.
//!
//! [`Global`] is expected to behave like a singleton and will store its main instance in this
//! variable's default value.

// For getting instance of [`Global`] from any object.
var protected Global myself;

var public UnflectApi       unflect;
var public SideEffectAPI    sideEffects;
var public RefAPI           ref;
var public BoxAPI           box;
var public MathAPI          math;
var public LoggerAPI        logger;
var public CollectionsAPI   collections;
var public AliasesAPI       alias;
var public TextAPI          text;
var public MemoryAPI        memory;
var public ConsoleAPI       console;
var public ChatAPI          chat;
var public ColorAPI         color;
var public UserAPI          users;
var public PlayersAPI       players;
var public JsonAPI          json;
var public SchedulerAPI     scheduler;
var public CommandAPI       commands;
var public AvariceAPI       avarice;

var public AcediaEnvironment    environment;

/// Returns instance of the [`Global`] object.
///
/// [`Global`] is supposed to be used as a singleton, meaning that only one instance of it should be
/// created.
/// This method creates and returns such instance.
/// In case it was already created, that instance will be returned from now one.
public final static function Global GetInstance() {
    if (default.myself == none) {
        // `...Global`s are special and exist outside main Acedia's object infrastructure,
        //so we allocate it without using [`MemoryAPI`] methods.
        default.myself = new class'Global';
        default.myself.Initialize();
    }
    return default.myself;
}

/// Initializes [`Global`] by creating all API from base realm.
protected function Initialize() {
    //  Special case that we cannot spawn with memory API since it obviously
    //  does not exist yet!
    memory = new class'MemoryAPI';
    memory._constructor();
    //  `TextAPI` and `CollectionsAPI` need to be loaded before `LoggerAPI`
    sideEffects = SideEffectAPI(memory.Allocate(class'SideEffectAPI'));
    ref = RefAPI(memory.Allocate(class'RefAPI'));
    box = BoxAPI(memory.Allocate(class'BoxAPI'));
    text = TextAPI(memory.Allocate(class'TextAPI'));
    math = MathAPI(memory.Allocate(class'MathAPI'));
    collections = CollectionsAPI(memory.Allocate(class'CollectionsAPI'));
    unflect = UnflectAPI(memory.Allocate(class'UnflectAPI'));
    json = JsonAPI(memory.Allocate(class'JsonAPI'));
    logger = LoggerAPI(memory.Allocate(class'LoggerAPI'));
    color = ColorAPI(memory.Allocate(class'ColorAPI'));
    alias = AliasesAPI(memory.Allocate(class'AliasesAPI'));
    console = ConsoleAPI(memory.Allocate(class'ConsoleAPI'));
    chat = ChatAPI(memory.Allocate(class'ChatAPI'));
    users = UserAPI(memory.Allocate(class'UserAPI'));
    players = PlayersAPI(memory.Allocate(class'PlayersAPI'));
    scheduler = SchedulerAPI(memory.Allocate(class'SchedulerAPI'));
    avarice = AvariceAPI(memory.Allocate(class'AvariceAPI'));
    commands = CommandAPI(memory.Allocate(class'CommandAPI'));
    environment = AcediaEnvironment(memory.Allocate(class'AcediaEnvironment'));
}

/// Drops references to all API from base realm, including self-reference, previously returned by
/// [`Global::GetInstance()`] method.
public function DropCoreAPI() {
    unflect._drop();
    memory = none;
    unflect = none;
    sideEffects = none;
    ref = none;
    box = none;
    text = none;
    math = none;
    collections = none;
    logger = none;
    alias = none;
    console = none;
    chat = none;
    color = none;
    users = none;
    players = none;
    json = none;
    scheduler = none;
    commands = none;
    avarice = none;
    environment = none;
    default.myself = none;
}

defaultproperties {
}