using System.Data;

namespace TravelAgencyWinForms.Utils;

public static class CalculatedFields
{
    private static readonly Dictionary<string, string> Labels = new(StringComparer.OrdinalIgnoreCase)
    {
        ["age_years"] = "Возраст сотрудника",
        ["duration_days"] = "Длительность тура",
        ["days_overdue"] = "Дней просрочки",
        ["is_overdue"] = "Просрочено"
    };

    public static bool IsCalculated(string columnName)
    {
        return Labels.ContainsKey(columnName);
    }

    public static List<string> GetColumns(DataTable table)
    {
        return table.Columns
            .Cast<DataColumn>()
            .Where(column => IsCalculated(column.ColumnName))
            .Select(column => column.ColumnName)
            .ToList();
    }

    public static string GetLabel(string columnName)
    {
        return Labels.TryGetValue(columnName, out var label) ? label : columnName;
    }

    public static string FormatValue(string columnName, object? value)
    {
        if (value == null || value == DBNull.Value)
            return "—";

        if (columnName.Equals("is_overdue", StringComparison.OrdinalIgnoreCase))
        {
            if (value is bool boolValue)
                return boolValue ? "Да" : "Нет";

            return value.ToString()?.Equals("true", StringComparison.OrdinalIgnoreCase) == true ? "Да" : "Нет";
        }

        if (columnName.Equals("age_years", StringComparison.OrdinalIgnoreCase))
            return value + " лет";

        if (columnName.Equals("duration_days", StringComparison.OrdinalIgnoreCase))
            return value + " дн.";

        if (columnName.Equals("days_overdue", StringComparison.OrdinalIgnoreCase))
            return value + " дн.";

        return value.ToString() ?? "—";
    }
}
