using Inspectron.Settings;
using System.ComponentModel;
using System.Globalization;
using VisionBuilder.UI.Common.Utils;

namespace VisionBuilder.UI.Common;

public class UIConfiguration: ISettings
{
    /// <summary>
    /// Max errors to be displayed in the UI.
    /// </summary>
    public int MaxErrorCount { get; set; } = 100;

    public string AdminPassword { get; set; } = "";

    public bool ProtectSettingsWithPassword { get; set; }=false;

    public bool ShowPauseButton { get; set; }=false;

    public bool ShowTestModeButton { get; set; } = false;

    public List<ErrorShortName> ErrorShortNames { get; set; } = new List<ErrorShortName>();

    public void RegisterSettings(InspectronSettings settings)
    {
        settings.RegisterSimple(this, () => this.MaxErrorCount, "Errors", "Max errors count");
        settings.RegisterSimple(this, () => this.AdminPassword, "System", "Password");
        settings.RegisterSimple(this, () => this.ProtectSettingsWithPassword, "System", nameof(ProtectSettingsWithPassword));
        settings.RegisterSimple(this, () => this.ErrorShortNames, "Errors", "Error short names");
        settings.RegisterSimple(this, () => this.ShowPauseButton, "System", nameof(ShowPauseButton));
        settings.RegisterSimple(this, () => this.ShowTestModeButton, "System", nameof(ShowTestModeButton));
    }


}

public class ErrorShortName
{
    public string FullName { get; set; }
    public string ShortName { get; set; }

    public override string ToString()
    {
        return $"{FullName} -> {ShortName}";
    }
}

public class ErrorShortNameConverter : GeneralListTypeConverter<ErrorShortName>
{

    protected override ErrorShortName ConvertFromString(ITypeDescriptorContext context, CultureInfo culture, string stringValue)
    {
        if (string.IsNullOrEmpty(stringValue))
            return null;

        var parts = stringValue.Split(',');
        if (parts.Length == 2)
        {
            return new ErrorShortName
            {
                FullName = parts[0].Trim(),
                ShortName = parts[1].Trim()
            };
        }

        return null;
    }

    protected override string ConvertToString(ITypeDescriptorContext context, CultureInfo culture, ErrorShortName errorShortName)
    {
        if (errorShortName == null)
            return null;

        return $"{errorShortName.FullName},{errorShortName.ShortName}";
    }
}