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

namespace Sonex.Client.Views;

public sealed partial class AppLogDetailsView : UserControl, IView
{
    private string? _currentKey;
    private bool _hasChanges;

    public event EventHandler? StateChanged;

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

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

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

    public void Reload()
    {
        if (string.IsNullOrWhiteSpace(_currentKey))
        {
            Clear();
            return;
        }

        Load(_currentKey);
    }

    public void Load(string key)
    {
        _currentKey = key;
        var result = AppLoggerRecord.Get(key).GetAwaiter().GetResult();

        if (result.Success && result.Item is not null)
        {
            LoadValues(result.Item);
        }
        else
        {
            ClearValues();
            ErrorIdProperty.Value = key;
        }

        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(AppLoggerRecord log)
    {
        ErrorIdProperty.Value = log.ErrorId;
        OccurredAtProperty.Value = AppLoggerRecord.FormatOccurredAt(log.OccurredAt);
        LevelProperty.Value = log.Level;
        IsUnhandledProperty.Value = log.IsUnhandled;
        ApplicationNameProperty.Value = log.ApplicationName;
        AppVersionProperty.Value = log.AppVersion;
        InstanceIdProperty.Value = log.InstanceId;
        UserNameProperty.Value = log.UserName;
        MachineNameProperty.Value = log.MachineName;
        OsVersionProperty.Value = log.OsVersion;
        CurrentPageTitleProperty.Value = Text(log.CurrentPageTitle);
        CurrentPageProperty.Value = Text(log.CurrentPage);
        SourceProperty.Value = log.Source;
        OperationProperty.Value = log.Operation;
        MessageProperty.Value = log.Message;
        ExceptionProperty.Value = Text(log.Exception);
    }

    private void ClearValues()
    {
        ErrorIdProperty.Value = string.Empty;
        OccurredAtProperty.Value = string.Empty;
        LevelProperty.Value = string.Empty;
        IsUnhandledProperty.Value = null;
        IsUnhandledProperty.Text = string.Empty;
        ApplicationNameProperty.Value = string.Empty;
        AppVersionProperty.Value = string.Empty;
        InstanceIdProperty.Value = string.Empty;
        UserNameProperty.Value = string.Empty;
        MachineNameProperty.Value = string.Empty;
        OsVersionProperty.Value = string.Empty;
        CurrentPageTitleProperty.Value = string.Empty;
        CurrentPageProperty.Value = string.Empty;
        SourceProperty.Value = string.Empty;
        OperationProperty.Value = string.Empty;
        MessageProperty.Value = string.Empty;
        ExceptionProperty.Value = string.Empty;
    }

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