using Avalonia.Controls;
using Avalonia.Layout;
using ReactiveUI;
using System.IO;

namespace VeloeMinecraftLauncher.ViewModels;

public enum MessageBoxButtons
{
    Ok,
    OkCancel,
    YesNo
}
public class MessageWindowViewModel : ViewModelBase
{ 
    public MessageWindowViewModel()
    {
        SetButtons(MessageBoxButtons.Ok);
        Title = "Message";
        Message = "Error";
        ErrorTrace = "ErrorTrace\n1\n2\n3\n4\n5";
        IsErrorTraceVisible = false;
        ButtonText = "Open log";
        CenterMessage = true;
        _logfile = "";
    }

    public MessageWindowViewModel(string message, string title, MessageBoxButtons buttons = MessageBoxButtons.Ok, string errorTrace = "", string logfile = "")
    {
        SetButtons(buttons);
        Title = title;
        Message = message;
        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 _message = string.Empty;
    private string _errorTrace = string.Empty;
    private bool _isErrorTraceVisible = false;
    private bool _centerMessage = false;
    private string _logfile = string.Empty;

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

    public string Message
    {
        get => _message;
        set => this.RaiseAndSetIfChanged(ref _message, 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 Title { get; set; } = "Message";
    public string ButtonText { get; set; } = string.Empty;
    public string ButtonOk { get; set; } = "Ok";
    public string ButtonCancel { get; set; } = "Cancel";
    public bool IsCancelButtonVisible { get; set; } = false;

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

    private void SetButtons(MessageBoxButtons buttons)
    {
        switch (buttons)
        {
            case MessageBoxButtons.Ok:
                ButtonOk = "Ok";
                IsCancelButtonVisible = false;
                break;
            case MessageBoxButtons.OkCancel:
                ButtonOk = "Ok";
                ButtonCancel = "Cancel";
                IsCancelButtonVisible = true;
                break;
            case MessageBoxButtons.YesNo:
                ButtonOk = "Yes";
                ButtonCancel = "No";
                IsCancelButtonVisible = true;
                break;
        }
    }

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

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

    public void CloseWindowOk(object window)
    {
        ((Window)window)?.Close(true);
    }

    public void CloseWindowCancel(object window)
    {
        ((Window)window)?.Close(false);
    }
}
