/**
 *  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 extends FeatureConfig
    perobjectconfig
    config(FutilityChat);

enum ChatColorSetting {
    CCS_DoNothing,
    CCS_TeamColorForced,
    CCS_ConfigColorForced,
    CCS_TeamColorCustom,
    CCS_ConfigColorCustom
};

/// How to color text chat messages?
///
/// 1. `CCS_DoNothing` - do not change color in any way;
/// 2. `CCS_TeamColorForced` - force players' team colors for
///     their messages;
/// 3. `CCS_ConfigColorForced` - force `configuredColor` value for
///     players' messages;
/// 4. `CCS_TeamColorCustom` - use players' team colors for
///     their messages by default, but allow to change color with formatted tags
///     (e.g. "Stop right there, {$crimson criminal} scum!");
/// 5. `CCS_ConfigColorCustom` - use `configuredColor` value for
///     messages by default, but allow to change color with formatted
///     tags (e.g. "Stop right there, {$crimson criminal} scum!");
///
/// Default is `CCS_DoNothing`, corresponding to vanilla behaviour.
var public config ChatColorSetting colorSetting;
/// Color that will be used if either of `CCS_ConfigColorForced` or
/// `CCS_ConfigColorCustom` options were used in `colorSetting`.
/// Default value is white: (R=255,G=255,B=255,A=255), has no vanilla
/// equivalent.
var public config Color configuredColor;
/// Allows to modify team color's value for the chat messages
/// (if either of `CCS_TeamColorForced` or `CCS_TeamColorCustom` options
/// were used) to be lighter or darker.
/// This value is clamped between -1 and 1:
///
/// * `0` means using the same color;
/// * range (0; 1) - gives you lighter colors (`1` being white);
/// * range (-1; 0) - gives you darker colors (`-1` being black);
///
/// Default value is `0.6`, has no vanilla equivalent.
var public config float teamColorModifier;

protected function HashTable ToData() {
    local HashTable data;
    local Text colorAsText;

    data = __().collections.EmptyHashTable();
    data.SetString(P("colorSetting"), StringFromColorSetting(colorSetting));
    colorAsText = _.color.ToText(configuredColor);
    data.SetItem(P("configuredColor"), colorAsText);
    _.memory.Free(colorAsText);
    data.SetFloat(P("teamColorModifier"), teamColorModifier);
    return data;
}

protected function FromData(HashTable source) {
    local Text storedText;

    if (source != none) {
        storedText = source.GetText(P("colorSetting"));
        colorSetting = ColorSettingFromText(storedText);
        _.memory.Free(storedText);
        storedText = source.GetText(P("configuredColor"));
        _.color.Parse(storedText, configuredColor);
        _.memory.Free(storedText);
        teamColorModifier = source.GetFloat(P("teamColorModifier"), 0.5);
    }
}

private function ChatColorSetting ColorSettingFromText(BaseText permissions) {
    if (permissions == none) {
        return CCS_DoNothing;
    }
    if (permissions.EndsWith(P("TeamColorForced"), SCASE_INSENSITIVE)) {
        return CCS_TeamColorForced;
    }
    if (permissions.EndsWith(P("ConfigColorForced"), SCASE_INSENSITIVE)) {
        return CCS_ConfigColorForced;
    }
    if (permissions.EndsWith(P("TeamColorCustom"), SCASE_INSENSITIVE)) {
        return CCS_TeamColorCustom;
    }
    if (permissions.EndsWith(P("ConfigColorCustom"), SCASE_INSENSITIVE)) {
        return CCS_ConfigColorCustom;
    }
    return CCS_DoNothing;
}

private function string StringFromColorSetting(ChatColorSetting permissions) {
    if (permissions == CCS_DoNothing) {
        return "DoNothing";
    }
    if (permissions == CCS_TeamColorForced) {
        return "TeamColorForced";
    }
    if (permissions == CCS_ConfigColorForced) {
        return "ConfigColorForced";
    }
    if (permissions == CCS_TeamColorCustom) {
        return "TeamColorCustom";
    }
    if (permissions == CCS_ConfigColorCustom) {
        return "ConfigColorCustom";
    }
    return "DoNothing";
}

protected function DefaultIt() {
    colorSetting = CCS_DoNothing;
    configuredColor = _.color.RGB(255, 255, 255);
    teamColorModifier = 0.6;
}

defaultproperties {
    configName = "FutilityChat"
    colorSetting = CCS_DoNothing
    configuredColor = (R=255,G=255,B=255,A=255)
    teamColorModifier = 0.6
}