using Microsoft.UI.Xaml.Controls;
using Sonex.Data.Records;
using System;
using System.Globalization;

namespace Sonex.Client.Views;

public sealed partial class ServiceDetailsView : UserControl, IView
{
    private ServiceRecord? _currentService;
    private bool _hasChanges;

    public event EventHandler? StateChanged;

    public ServiceDetailsView()
    {
        InitializeComponent();
        ClearValues();
    }

    public bool HasChanges => _hasChanges;
    public bool CanSave => CanEdit && HasChanges;
    public bool CanEdit => false;

    public void Clear()
    {
        _currentService = null;
        ClearValues();
        MarkSaved();
    }

    public void Reload()
    {
        if (_currentService is null)
        {
            Clear();
            return;
        }

        Load(_currentService);
    }

    public void Load(string key)
    {
        _currentService = null;
        ClearValues();
        NameProperty.Value = key;
        MarkSaved();
    }

    public void Load(ServiceRecord service)
    {
        ArgumentNullException.ThrowIfNull(service);

        _currentService = service;
        LoadValues(service);
        MarkSaved();
    }

    public void Save()
    {
        MarkSaved();
    }

    public void MarkChanged()
    {
        _hasChanges = true;
        StateChanged?.Invoke(this, EventArgs.Empty);
    }

    public void MarkSaved()
    {
        _hasChanges = false;
        StateChanged?.Invoke(this, EventArgs.Empty);
    }

    private void LoadValues(ServiceRecord service)
    {
        DisplayNameProperty.Value = Text(service.DisplayName);
        NameProperty.Value = service.Name;
        CaptionProperty.Value = Text(service.Caption);
        DescriptionProperty.Value = Text(service.Description);
        StatusProperty.Value = Text(service.Status);
        StateProperty.Value = Text(service.State);
        StartedProperty.Value = service.Started;
        ProcessIdProperty.Value = Text(service.ProcessId);
        ExitCodeProperty.Value = Text(service.ExitCode);
        ServiceExitCodeProperty.Value = Text(service.ServiceSpecificExitCode);
        StartModeProperty.Value = Text(service.StartMode);
        StartNameProperty.Value = Text(service.StartName);
        ErrorControlProperty.Value = Text(service.ErrorControl);
        DelayedAutoStartProperty.Value = service.DelayedAutoStart;
        AcceptPauseProperty.Value = service.AcceptPause;
        AcceptStopProperty.Value = service.AcceptStop;
        PathNameProperty.Value = Text(service.PathName);
    }

    private void ClearValues()
    {
        DisplayNameProperty.Value = string.Empty;
        NameProperty.Value = string.Empty;
        CaptionProperty.Value = string.Empty;
        DescriptionProperty.Value = string.Empty;
        StatusProperty.Value = string.Empty;
        StateProperty.Value = string.Empty;
        StartedProperty.Value = null;
        StartedProperty.Text = string.Empty;
        ProcessIdProperty.Value = string.Empty;
        ExitCodeProperty.Value = string.Empty;
        ServiceExitCodeProperty.Value = string.Empty;
        StartModeProperty.Value = string.Empty;
        StartNameProperty.Value = string.Empty;
        ErrorControlProperty.Value = string.Empty;
        DelayedAutoStartProperty.Value = null;
        DelayedAutoStartProperty.Text = string.Empty;
        AcceptPauseProperty.Value = null;
        AcceptPauseProperty.Text = string.Empty;
        AcceptStopProperty.Value = null;
        AcceptStopProperty.Text = string.Empty;
        PathNameProperty.Value = string.Empty;
    }

    private static string ToYesNo(bool? value)
    {
        return value.HasValue
            ? value.Value ? "Yes" : "No"
            : string.Empty;
    }

    private static string Text(object? value)
    {
        return Convert.ToString(value, CultureInfo.CurrentCulture) ?? string.Empty;
    }
}
