using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Threading;
using PSCHelpdesk.Plugins.HetznerServer.Models;
using PSCHelpdesk.Plugins.HetznerServer.ViewModels;

namespace PSCHelpdesk.Plugins.HetznerServer.Views;

public partial class ServerView : UserControl
{
    public ServerView()
    {
        InitializeComponent();
    }

    private void openSSH(object? sender, RoutedEventArgs e)
    {
        ((ServerViewModel)DataContext).SSH(((Button)sender).DataContext as Server);
    }
    
    private void openSCP(object? sender, RoutedEventArgs e)
    {
        ((ServerViewModel)DataContext).SCP(((Button)sender).DataContext as Server);
    }

    private void InputElement_OnDoubleTapped(object? sender, TappedEventArgs e)
    {
        Uri? uri = new Uri("https://" + ((TextBox)sender).Text);
        if (uri is not null)
        {
            Dispatcher.UIThread.Post(async () =>
            {
                bool success = await TopLevel.GetTopLevel(this)!.Launcher.LaunchUriAsync(uri);
            });
        }
    }
}