using PCUT.Entities;
using PCUT.Extensions;
using PCUT.Factories;
using PCUT.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using static System.Net.WebRequestMethods;

// The Content Dialog item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace PCUT.Pages
{
    public sealed partial class FileEditDialog : ContentDialog
    {
        private FileEditViewModel ViewModel { get; set; }
        private StorageFile _file;
        private readonly string _id;
        public FileEditDialog(string id)
        {
            this.InitializeComponent();
            ViewModel = new FileEditViewModel();

            _id = id;

            this.DataContext = ViewModel;
        }

        private async void ContentDialog_Loaded(object sender, RoutedEventArgs e)
        {
            ViewModel.MetadataModel.BindAll(Type, Brand, Series, Model, SubType, Year);
            ViewModel.Categories.Bind(Category);

            await ViewModel.GetCategoriesAsync();
            await ViewModel.MetadataModel.LoadAllMetadataAsync();

            await ViewModel.LoadFileData(_id);
        }

        private void Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //ViewModel.MetadataModel.UnselectAll();
        }

        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            var deferral = args.GetDeferral();
            var success = await ViewModel.UpdateFileAsync(_id, _file, ThumbnailPreview);
            args.Cancel = !success;
            deferral.Complete();
        }

        private async void UploadFile_CLick(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker
            {
                ViewMode = PickerViewMode.List,
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary
            };
            openPicker.FileTypeFilter.Add(".svg");
            _file = await openPicker.PickSingleFileAsync();

            if (_file == null)
            {
                var noFileDialog = new MessageDialog("No file was selected");
                await noFileDialog.ShowAsync();
                return;
            }

            using (var stream = await _file.OpenReadAsync())
            {
                var (doc, _, _) = await FileFactory.PreProcessSvgAsync(stream.AsStream());
                var source = new SvgImageSource();
                ViewModel.PreviewSource = source;
                _ = source.LoadSvgAsync(doc);
            }
        }
    }
}
