using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Reactive;
using Avalonia.Controls;
using Avalonia.Interactivity;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using DynamicData;
using DynamicData.Binding;
using HetznerCloudApi;
using PSCHelpdesk.Plugins.HetznerServer.Models;
using PSCHelpdesk.Plugins.HetznerServer.Service;
using PSCHelpdesk.Plugins.HetznerServer.Views;
using PSCHelpdesk.Shared.Service;
using PSCHelpdesk.Shared.Setting;
using PSCHelpdesk.Shared.ViewModels;
using ReactiveUI;

namespace PSCHelpdesk.Plugins.HetznerServer.ViewModels;

public partial class ServerViewModel : ViewModelBase, IViewModelBase
{
    public readonly ServerService  ServerService;
    public ReactiveCommand<SelectionChangedEventArgs, Unit> SelectionChanged { get; }
    
    protected readonly ReadOnlyObservableCollection<Server> _server;
    public ReadOnlyObservableCollection<Server> Server => _server;
    
    private SettingsManager _settingsManager;
    public ServerViewModel(ServerService _serverService)
    {
        SelectedServer = new Server(1,"","","","","");
        SelectionChanged = ReactiveCommand.Create<SelectionChangedEventArgs>(selectionChanged);
        _settingsManager = (SettingsManager)Ioc.Default.GetService<ISettingsManager>();
        ServerService = _serverService;
        ServerService.SourceCache.Connect()
            // Sort Ascending on the OrderIndex property
            .Sort(SortExpressionComparer<Server>.Ascending(t => t.Id))
            .Filter(x => x.Name.Contains(_searchText, StringComparison.OrdinalIgnoreCase))
            // Bind to our ReadOnlyObservableCollection<T>
            .Bind(out _server)
            // Subscribe for changes
            .Subscribe();
    }
    
    private bool _isPagePaneOpen = true;
    public bool IsPagePaneOpen
    {
        get => _isPagePaneOpen;
        set => SetAndRaisePropertyChanged(ref _isPagePaneOpen, value);
    }
    
    private string _searchText = "";
    public string SearchText
    {
        get => _searchText;
        set
        {
            SetAndRaisePropertyChanged(ref _searchText, value);
            ServerService.SourceCache.Refresh();
        }
    }

    private Server _selectedServer;
    public Server SelectedServer
    {
        get => _selectedServer;
        set => SetAndRaisePropertyChanged(ref _selectedServer, value);
    }
    
    

    void selectionChanged(SelectionChangedEventArgs args)
    {
        this.SelectedServer = (args.Source as DataGrid).SelectedItem as Server;
        IsPagePaneOpen = true;
    }
    
    public void SSH(Server server)
    {
        Process.Start(_settingsManager.CoreSettings.SSHClientExecutable,string.Format(_settingsManager.CoreSettings.SSHClientExecutableArgs, server.Ipv4, _settingsManager.CoreSettings.PrivateSSHKeyPath, server.Name));

    }
    
    public void SCP(Server server)
    {
        Process.Start(_settingsManager.CoreSettings.SCPClientExecutable,string.Format(_settingsManager.CoreSettings.SCPClientExecutableArgs, server.Ipv4, _settingsManager.CoreSettings.PrivateSSHKeyPath, server.Name));

    }
    
    public UserControl GetViewControl()
    {
        return new ServerView();
    }
}