using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Http.Core;
using Http.Core.Exceptions;
using Http.Core.Extensions;
using PCUT.Extensions;
using Windows.Security.Cryptography.Certificates;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using static Http.Core.Constants.HttpConstants;

namespace PCUT.Factories
{
    class FileFactory
    {
        public static Task<Stream> GetFileAsync()
        {
            return LocalFileFactory.Instance.GetFileAsync();
        }

        public static Task<Stream> GetFileAsync(string id)
        {
            return RemoteFileFactory.Instance.GetFileAsync(id);
        }

        public static async Task<(XDocument Data, double Width, double Height)> PreProcessSvgAsync(Stream stream)
        {
            try
            {
                var document = await XDocument.LoadAsync(stream, LoadOptions.None, default);
                //document.IdentifyComponents(path: "g", "path", "g");
                //document.ApplyCssStyle();
                //document.Root.PropagateTransform();
                //var (width, height) = document.SetAutoSize();
                document.PreProcessSvg(out var width, out var height);
                return (document, width, height);
            }
            catch
            {
                return default;
            }
        }
    }

    interface IFileFactory
    {
        Task<Stream> GetFileAsync(string id = null);
    }

    class LocalFileFactory : IFileFactory
    {
        public static readonly IFileFactory Instance = new LocalFileFactory();
        public async Task<Stream> GetFileAsync(string id = null)
        {
            try
            {
                var file = await CreatePicker().PickSingleFileAsync();
                if (file == null)
                    return null;
                var stream = await file.OpenReadAsync();
                return stream.AsStream();
            }
            catch
            {
                return null;
            }
        }

        private static FileOpenPicker CreatePicker()
        {
            var picker = new FileOpenPicker();
            picker.ViewMode = PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = PickerLocationId.Downloads;
            picker.FileTypeFilter.Add(".svg");
            return picker;
        }
    }
    class RemoteFileFactory : IFileFactory
    {
        public static readonly IFileFactory Instance = new RemoteFileFactory();
        public async Task<Stream> GetFileAsync(string id = null)
        {
            using (var httpClient = HttpClientFactory.CreateClient(ClientNames.ApiClient))
            {
                try
                {
                    var stream = new MemoryStream();
                    var response = await httpClient.DownloadAsync(Api.Download.FormatRoute(id), stream);
                    if (response)
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        return stream;
                    }
                    else
                    {
                        stream.Dispose();
                    }    
                }
                catch (Exception ex) when (!(ex is AppOutdatedException))
                {
                }
            }
            return null;
        }
    }
}   
