/**
 *      API that provides functions for working with chat.
 *      Copyright 2022 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 OldSchoolZedsAddon_Feature extends Feature;

struct FlagUpdateDefinition
{
    var string          flagName;
    var string          variableName;
    var class<Actor>    relevantClass;
};
var private array<FlagUpdateDefinition> flagToUpdate;

protected function OnEnabled()
{
    _server.unreal.GetGameType().AddToPackageMap("KFOldSchoolZeds");
    Log("BEFORE!!!" @ _server.unreal.GetKFGameType().MonsterCollection @ _server.unreal.GetKFGameType().InitSquads.length);
    _server.unreal.GetGameType().AddMutator(string(class'KF25OSMut'));
    Log("PUHPUH!!!" @ _server.unreal.mutator.FindInstance(class'KF25OSMut'));
    Log("AFTER!!!" @ _server.unreal.GetKFGameType().MonsterCollection @ _server.unreal.GetKFGameType().InitSquads.length);
}

protected function SwapConfig(FeatureConfig config)
{
    local KF25OSMut             mutatorInstance;
    local OldSchoolZedsAddon    newConfig;

    newConfig = OldSchoolZedsAddon(config);
    if (newConfig == none) {
        return;
    }
    mutatorInstance =
        KF25OSMut(_server.unreal.mutator.FindInstance(class'KF25OSMut'));
    UpdateFlags(mutatorInstance, newConfig);
    UpdateDefaultMutatorFlags(newConfig);
    UpdateDefaultActorsFlags(newConfig);
}

//  Necessary boiler plate code for updating default values
private function UpdateDefaultMutatorFlags(OldSchoolZedsAddon newConfig)
{
    if (newConfig == none) {
        return;
    }
    class'KF25OSMut'.default.bDisableIncendiaryRounds =
        newConfig.bDisableIncendiaryRounds;
    class'KF25OSMut'.default.bDisableIncendiaryResistance =
        newConfig.bDisableIncendiaryResistance;
    class'KF25OSMut'.default.bEnableOldBloatPuke =
        newConfig.bEnableOldBloatPuke;
    class'KF25OSMut'.default.bEnableOldCrawlerBehaviour =
        newConfig.bEnableOldCrawlerBehaviour;
    class'KF25OSMut'.default.bEnableSirenNadeBoom =
        newConfig.bEnableSirenNadeBoom;
    class'KF25OSMut'.default.bEnableOldScrakeBehavior =
        newConfig.bEnableOldScrakeBehavior;
    class'KF25OSMut'.default.bEnableOldFleshpoundBehavior =
        newConfig.bEnableOldFleshpoundBehavior;
    class'KF25OSMut'.default.bNerfEP = newConfig.bNerfEP;
}

//  Necessary boiler plate code for updating default values
private function UpdateDefaultActorsFlags(OldSchoolZedsAddon newConfig)
{
    if (newConfig == none) {
        return;
    }
    class'ZombieRangedPoundOS'.default.bDisableIncendiaryRounds =
        newConfig.bDisableIncendiaryRounds;
    class'ZombieRangedPoundOS'.default.bDisableIncendiaryResistance =
        newConfig.bDisableIncendiaryResistance;
    class'ZombieBloatOS'.default.bEnableOldBloatPuke =
        newConfig.bEnableOldBloatPuke;
    class'ControllerCrawlerOS'.default.bEnableOldCrawlerBehaviour =
        newConfig.bEnableOldCrawlerBehaviour;
    class'ZombieSirenOS'.default.bEnableSirenNadeBoom =
        newConfig.bEnableSirenNadeBoom;
    class'ZombieScrakeOS'.default.bEnableOldScrakeBehavior =
        newConfig.bEnableOldScrakeBehavior;
    class'ZombieFleshPoundOS'.default.bEnableOldFleshpoundBehavior =
        newConfig.bEnableOldFleshpoundBehavior;
    class'ControllerFleshpoundOS'.default.bEnableOldFleshpoundBehavior =
        newConfig.bEnableOldFleshpoundBehavior;
    class'ZombieExplosivesPoundOS'.default.bNerfed = newConfig.bNerfEP;
    class'GunnerGLProjectile'.default.bNerfed = newConfig.bNerfEP;
}

private function UpdateFlags(
    KF25OSMut           mutatorInstance,
    OldSchoolZedsAddon  config)
{
    local int       i;
    local string    flagName, variableName, flagValue;

    if (config == none) {
        return;
    }
    for (i = 0; i < flagToUpdate.length; i += 1)
    {
        flagName    = flagToUpdate[i].flagName;
        flagValue   = config.GetPropertyText(flagToUpdate[i].flagName);
        variableName = flagToUpdate[i].variableName;
        if (variableName == "") {
            variableName = flagToUpdate[i].flagName;
        }
        if (mutatorInstance != none){
            mutatorInstance.SetPropertyText(flagName, flagValue);
        }
        if (flagToUpdate[i].relevantClass != none)
        {
            UpdateActorsFlag(
                flagToUpdate[i].relevantClass,
                variableName,
                flagValue);
        }
    }
}

private function UpdateActorsFlag(
    class<Actor>    classToUpdate,
    string          variableName,
    string          variableValue)
{
    local LevelInfo level;
    local Actor     nextActorInstance;

    if (classToUpdate == none) {
        return;
    }
    level = _server.unreal.GetLevel();
    foreach level.DynamicActors(classToUpdate, nextActorInstance) {
        nextActorInstance.SetPropertyText(variableName, variableValue);
    }
}

defaultproperties
{
    configClass = class'OldSchoolZedsAddon'
    flagToUpdate(0) = (flagName="bDisableIncendiaryRounds",relevantClass=class'ZombieRangedPoundOS')
    flagToUpdate(1) = (flagName="bDisableIncendiaryResistance",relevantClass=class'ZombieRangedPoundOS')
    flagToUpdate(2) = (flagName="bNerfEP",variableName="bNerfed",relevantClass=class'ZombieExplosivesPoundOS')
    flagToUpdate(3) = (flagName="bNerfEP",variableName="bNerfed",relevantClass=class'GunnerGLProjectile')
    flagToUpdate(4) = (flagName="bEnableOldBloatPuke",relevantClass=class'ZombieBloatOS')
    flagToUpdate(5) = (flagName="bEnableOldCrawlerBehaviour",relevantClass=class'ControllerCrawlerOS')
    flagToUpdate(6) = (flagName="bEnableSirenNadeBoom",relevantClass=class'ZombieSirenOS')
    flagToUpdate(7) = (flagName="bEnableOldScrakeBehavior",relevantClass=class'ZombieScrakeOS')
    flagToUpdate(8) = (flagName="bEnableOldFleshpoundBehavior",relevantClass=class'ZombieFleshPoundOS')
    flagToUpdate(9) = (flagName="bEnableOldFleshpoundBehavior",relevantClass=class'ControllerFleshpoundOS')
}