/**
 * Author: dkanus
 * Home repo: https://www.insultplayers.ru/git/AcediaFramework/AcediaCore
 * License: GPL
 * Copyright 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 CommandRegistrationJob extends SchedulerJob
    dependson(CommandAPI);

var private CommandAPI.AsyncTask nextItem;

// Expecting 300 units of work, this gives us registering 20 commands per tick
const ADDING_COMMAND_COST = 15;
// Adding voting option is approximately the same as adding a command's
// single sub-command - we'll estimate it as 1/3rd of the full value
const ADDING_VOTING_COST = 5;
// Authorizing is relatively cheap, whether it's commands or voting
const AUTHORIZING_COST = 1;

protected function Constructor() {
    nextItem = _.commands._popPending();
}

protected function Finalizer() {
    _.memory.Free3(nextItem.entityName, nextItem.userGroup, nextItem.configName);
    nextItem.entityClass = none;
    nextItem.entityName = none;
    nextItem.userGroup = none;
    nextItem.configName = none;
}

public function bool IsCompleted() {
    return (nextItem.entityName == none);
}

public function DoWork(int allottedWorkUnits) {
    while (allottedWorkUnits > 0 && nextItem.entityName != none) {
        if (nextItem.type == CAJT_AddCommand) {
            allottedWorkUnits -= ADDING_COMMAND_COST;
            _.commands.AddCommand(class<Command>(nextItem.entityClass), nextItem.entityName);
            _.memory.Free(nextItem.entityName);
        } else if (nextItem.type == CAJT_AddVoting) {
            allottedWorkUnits -= ADDING_VOTING_COST;
            _.commands.AddVoting(class<Voting>(nextItem.entityClass), nextItem.entityName);
            _.memory.Free(nextItem.entityName);
        } else if (nextItem.type == CAJT_AuthorizeCommand) {
            allottedWorkUnits -= AUTHORIZING_COST; 
            _.commands.AuthorizeCommandUsage(
                nextItem.entityName,
                nextItem.userGroup,
                nextItem.configName);
            _.memory.Free3(nextItem.entityName, nextItem.userGroup, nextItem.configName);
        } else /*if (nextItem.type == CAJT_AuthorizeVoting)*/ {
            allottedWorkUnits -= AUTHORIZING_COST; 
            _.commands.AuthorizeVotingUsage(
                nextItem.entityName,
                nextItem.userGroup,
                nextItem.configName);
            _.memory.Free3(nextItem.entityName, nextItem.userGroup, nextItem.configName);
        }
        nextItem = _.commands._popPending();
    }
}

defaultproperties {
}