/**
 *  Config class for storing map lists.
 *      Copyright 2022-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 FutilityChat_Feature extends Feature
    dependson(FutilityChat);

var private /*config*/ FutilityChat.ChatColorSetting colorSetting;
var private /*config*/ Color configuredColor;
var private /*config*/ float teamColorModifier;

/// Keep track of whether we connected to necessary signals, so that we can
/// connect to them or disconnect from them once setting get updated
var private bool connectedToSignal;

protected function OnEnabled() {
    if (colorSetting != CCS_DoNothing) {
        _.chat.OnMessage(self).connect = ReformatChatMessage;
    }
}

protected function OnDisabled() {
    if (colorSetting != CCS_DoNothing) {
        _.chat.OnMessage(self).Disconnect();
    }
}

protected function SwapConfig(FeatureConfig config)
{
    local bool configRequiresSignal;
    local FutilityChat newConfig;

    newConfig = FutilityChat(config);
    if (newConfig == none) {
        return;
    }
    colorSetting = newConfig.colorSetting;
    configuredColor = newConfig.configuredColor;
    teamColorModifier = newConfig.teamColorModifier;
    configRequiresSignal = (colorSetting != CCS_DoNothing);
}

private function bool ReformatChatMessage(
    EPlayer sender,
    MutableText message,
    bool teamMessage
) {
    local Text messageCopy;
    local BaseText.Formatting defaultFormatting;

    if (sender == none)                 return true;
    if (message == none)                return true;
    if (colorSetting == CCS_DoNothing)  return true;

    defaultFormatting.isColored = true;
    if (colorSetting == CCS_TeamColorForced || colorSetting == CCS_TeamColorCustom) {
        defaultFormatting.color = ModColor(sender.GetTeamColor());
    } else {
        defaultFormatting.color = configuredColor;
    }
    if (message.StartsWith(P("|"))) {
        message.Remove(0, 1);
    } else if (colorSetting != CCS_TeamColorForced && colorSetting != CCS_ConfigColorForced) {
        messageCopy = message.Copy();
        class'FormattingStringParser'.static.ParseFormatted(messageCopy, message.Clear());
        _.memory.Free(messageCopy);
    }
    message.ChangeDefaultFormatting(defaultFormatting);
    return true;
}

private function Color ModColor(Color inputColor) {
    local Color mixColor;
    local Color outputColor;
    local float clampedModifier;

    if (Abs(teamColorModifier) < 0.001) {
        return inputColor;
    }
    clampedModifier = FClamp(teamColorModifier, -1.0, 1.0);
    if (clampedModifier > 0) {
        mixColor = _.color.White;
    } else {
        mixColor = _.color.Black;
        clampedModifier *= -1.0;
    }
    outputColor.R = Lerp(clampedModifier, inputColor.R, mixColor.R);
    outputColor.G = Lerp(clampedModifier, inputColor.G, mixColor.G);
    outputColor.B = Lerp(clampedModifier, inputColor.B, mixColor.B);
    outputColor.A = inputColor.A;
    return outputColor;
}

defaultproperties {
    configClass = class'FutilityChat'
}