using Avalonia.Controls;
using Avalonia.Platform.Storage;
using EdTestEmulator.Models;
using EdTestEmulator.Services;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace EdTestEmulator.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        public string Greeting => "Welcome to Avalonia!";

        public ObservableCollection<ProcedureRow> Procedures { get; }
        public ObservableCollection<MessageRow> Messages { get; }
        public Dictionary<MessageRow,string> XmlMessages { get; } 

        public MessageRow SelectedMessage { get; set; }
        public ProcedureRow SelectedProcedure { get; set; }

        public string ExchangePath { get; set; } = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/InputDir";

        private MessageProcessingService _service;

        public MainWindowViewModel()
        {
            Procedures = new() { new ProcedureRow() { Type = "DT", DateStart = DateTime.Today, DeclarantName = "TestName", ProcedureUUID = Guid.NewGuid().ToString(), Organization = "OOO Roga i Kopita" } };
            Messages = new();
            XmlMessages = new();

            _service = new MessageProcessingService(Procedures, Messages, XmlMessages, ExchangePath);

            this.RaisePropertyChanged(nameof(Procedures));
        }

        public void SelectMessageManual(Window window)
        {
            Task.Run(async () =>
            {
                var storageProvider = ((Window)window).StorageProvider;
                var initPath = String.Empty;

                FilePickerFileType xmlFilter = new("XML")
                {
                    Patterns = new[] { "*.xml"}
                };

                var folderPickerOpenOptions = new FilePickerOpenOptions()
                {
                    AllowMultiple = false,
                    FileTypeFilter = new List<FilePickerFileType>() { xmlFilter },
                    SuggestedStartLocation = await storageProvider.TryGetFolderFromPathAsync(initPath)
                };

                var result = await storageProvider.OpenFilePickerAsync(folderPickerOpenOptions);

                if (result is null || result.Count != 1 || result.First().Path.AbsolutePath == String.Empty)
                    return;

                var xmlFilePath = result.First().Path.LocalPath;

                File.Copy(xmlFilePath, ExchangePath + "/" + Path.GetFileName(xmlFilePath));
            });
        }

        public void OpenExchangeFolder()
        {
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = ExchangePath, UseShellExecute = true });
        }

        public void OpenXmlMessage()
        {
            var vm = new DocumentWindowViewModel();
            vm.XmlString = XmlMessages[SelectedMessage];

            vm.Title = SelectedMessage.Description;

            vm.XlstPath = "C:\\Users\\Veloe\\source\\repos\\EdTestEmulator\\EdTestEmulator\\Assets\\EdVersions\\xslt_out_5_21_0\\1006107E_ESADout_CU.xslt";

            vm.ConvertToHtml();

            var window = new DocumentWindow();
            window.DataContext = vm;
            window.Show();
        }
    }
}
