using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Layout;
using ReactiveUI;
using System.IO;
using System.Security.Principal;

namespace VeloeMinecraftLauncher.ViewModels;

public class ErrorWindowViewModel : ViewModelBase
{ 
    public ErrorWindowViewModel()
    {
        ErrorMessage = "Error";
        ErrorTrace = "ErrorTrace\n1\n2\n3\n4\n5";
        IsErrorTraceVisible = false;
        ButtonText = "Open log";
        CenterMessage = true;
        _logfile = "";
    }

    public ErrorWindowViewModel(string errorMessage, string errorTrace = "", bool centerMessage = false, string logfile = "")
    {
        ErrorMessage = errorMessage;
        ErrorTrace = errorTrace;
        if (string.IsNullOrEmpty(errorTrace))
        {
            IsErrorTraceVisible = false;
            CenterMessage = true;
        }
        else
        {
            IsErrorTraceVisible = true;
            CenterMessage = false;
        }
        //IsErrorTraceVisible = isErrorTraceVisible;
        //CenterMessage = centerMessage;
        if (!string.IsNullOrEmpty(logfile))
        {
            _logfile = logfile;
            ButtonText = "Open folder";
            this.RaisePropertyChanged(nameof(IsLogButtonVisible));
        }

    }

    private string _errorMessage;
    private string _errorTrace;
    private bool _isErrorTraceVisible;
    private bool _centerMessage;
    private string _logfile;

    public string ErrorTrace
    {
        get => _errorTrace;
        set => this.RaiseAndSetIfChanged(ref _errorTrace, value);
    }

    public string ErrorMessage
    {
        get => _errorMessage;
        set => this.RaiseAndSetIfChanged(ref _errorMessage, value);
    }

    public bool CenterMessage
    {
        get => _centerMessage;
        set => this.RaiseAndSetIfChanged(ref _centerMessage, value);
    }

    public bool IsErrorTraceVisible
    {
        get => _isErrorTraceVisible;
        set => this.RaiseAndSetIfChanged(ref _isErrorTraceVisible, value);
    }

    public HorizontalAlignment HorizontalAlignment
    {
        get => CenterMessage ? HorizontalAlignment.Center : HorizontalAlignment.Stretch;
    }

    public VerticalAlignment VerticalAlignment
    {
        get => CenterMessage ? VerticalAlignment.Center : VerticalAlignment.Stretch;
    }

    public string ButtonText { get; set; } = string.Empty;

    public bool IsLogButtonVisible
    {
        get => !string.IsNullOrEmpty(_logfile);
    }

    public void OpenLogFile()
    {
        if (Path.Exists(_logfile))
        {
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = _logfile, UseShellExecute = true });
        }
    }

    public void CloseWindow(Window window)
    {
        window?.Close();
    }


}
