using System;
using System.IO;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Platform.Storage;
using Nucs.JsonSettings;
using PSCHelpdesk.Models;
using PSCHelpdesk.ViewModels;

namespace PSCHelpdesk.Views;

public partial class SettingsView : UserControl
{
    public SettingsView()
    {
        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.OpenFilePickerAsync(new FilePickerOpenOptions
        {
            Title = "Choose File",
            AllowMultiple = false
        });

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

    private void SaveSettings_OnClick(object? sender, RoutedEventArgs e)
    {
        LocalSetting settings = new LocalSetting();
        Directory.CreateDirectory(Path.GetDirectoryName(settings.FileName));
        SettingsViewModel model = (SettingsViewModel)this.DataContext;
        settings.GlobalConfigFilePath = model.GlobalConfigFilePath;
        settings.SCPClientExecutable = model.ScpClientPath;
        settings.SSHClientExecutable = model.SshClientPath;
        settings.SCPClientExecutableArgs = model.ScpClientPathArgs;
        settings.SSHClientExecutableArgs = model.SshClientPathArgs;
        settings.PrivateSSHKeyPath = model.PrivateSSHKeyPath;
        settings.Save();
    }

    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)
        {
            SettingsViewModel model = (SettingsViewModel)this.DataContext;
            model.SshClientPath = file[0].Path.ToString();
        }
    }

    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)
        {
            SettingsViewModel model = (SettingsViewModel)this.DataContext;
            model.ScpClientPath = file[0].Path.ToString();
        }
    }

    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 File",
            AllowMultiple = false
        });

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