using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using CommunityToolkit.Mvvm.DependencyInjection;
using PSCHelpdesk.Shared.Service;
using PSCHelpdesk.Shared.Setting;
using PSCHelpdesk.ViewModels;

namespace PSCHelpdesk.Views;

public partial class SettingsGlobalView : UserControl
{
    public SettingsGlobalView()
    {
        InitializeComponent();
        
    }
    
    private async void ChooseGlobalSettingsFile_OnClick(object? sender, RoutedEventArgs e)
    {
        var topLevel = TopLevel.GetTopLevel(this);

        // Start async operation to open the dialog.
        var file = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
        {
            Title = "Choose Folder",
            AllowMultiple = false
        });

        if (file is not null && file.Count > 0)
        {
            SettingsGlobalViewModel model = (SettingsGlobalViewModel)this.DataContext;
            model.GlobalConfigFilePath = file[0].Path.AbsolutePath;
        }
    }

    private void SaveSettings_OnClick(object? sender, RoutedEventArgs e)
    {
        SettingsManager settingsManager = (SettingsManager)Ioc.Default.GetService<ISettingsManager>();
        SettingsGlobalViewModel model = (SettingsGlobalViewModel)this.DataContext;
        settingsManager.CoreSettings.GlobalConfigFilePath = model.GlobalConfigFilePath;
        settingsManager.CoreSettings.SCPClientExecutable = model.ScpClientPath;
        settingsManager.CoreSettings.SSHClientExecutable = model.SshClientPath;
        settingsManager.CoreSettings.SCPClientExecutableArgs = model.ScpClientPathArgs;
        settingsManager.CoreSettings.SSHClientExecutableArgs = model.SshClientPathArgs;
        settingsManager.CoreSettings.PrivateSSHKeyPath = model.PrivateSSHKeyPath;
        settingsManager.SaveSettings();
    }

    private async void ChooseSshClientFile_OnClick(object? sender, RoutedEventArgs e)
    {
        var topLevel = TopLevel.GetTopLevel(this);

        // Start async operation to open the dialog.
        var file = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
        {
            Title = "Choose File",
            AllowMultiple = false
        });

        if (file is not null && file.Count > 0)
        {
            SettingsGlobalViewModel model = (SettingsGlobalViewModel)this.DataContext;
            model.SshClientPath = file[0].Path.AbsolutePath;
        }
    }

    private async void ChooseScpClientFile_OnClick(object? sender, RoutedEventArgs e)
    {
        var topLevel = TopLevel.GetTopLevel(this);

        // Start async operation to open the dialog.
        var file = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
        {
            Title = "Choose File",
            AllowMultiple = false
        });

        if (file is not null && file.Count > 0)
        {
            SettingsGlobalViewModel model = (SettingsGlobalViewModel)this.DataContext;
            model.ScpClientPath = file[0].Path.AbsolutePath;
        }
    }

    private async void ChoosePrivateSshFile_OnClick(object? sender, RoutedEventArgs e)
    {
        var topLevel = TopLevel.GetTopLevel(this);

        // Start async operation to open the dialog.
        var file = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
        {
            Title = "Choose Folder",
            AllowMultiple = false
        });

        if (file is not null && file.Count > 0)
        {
            SettingsGlobalViewModel model = (SettingsGlobalViewModel)this.DataContext;
            model.PrivateSSHKeyPath = file[0].Path.AbsolutePath;
        }
    }
}