using ExtensiblePortfolioSite.SDK.Plugins;

using System;
using System.IO;

namespace ExtensiblePortfolioSite.SDK.Resources
{
    /// <summary>
    /// A Remote File retrived from a HTTP Server
    /// </summary>
    [ResourceContainer("http", "https", "lhttp", "lhttps")]
    public class HTTPFile : IResource
    {
        /// <inheritdoc cref="IResource.PublicallyAccessible"/>
        public bool PublicallyAccessible { get; }
        /// <inheritdoc cref="IResource.WebCompatible"/>
        public bool WebCompatible => true;
        /// <inheritdoc cref="IResource.URI"/>
        public Uri URI { get; }

        /// <inheritdoc cref="HTTPFile"/>
        /// <param name="URI"></param>
        /// <exception cref="ArgumentException"></exception>
        public HTTPFile(Uri URI)
        {
            switch (URI.Scheme)
            {
                // remote "public" HTTP FILE
                case "http":
                case "https":
                    this.PublicallyAccessible = true;
                    this.URI = URI;
                    break;

                // local "non-public" HTTP FILE
                case "lhttp":
                case "lhttps":
                    this.PublicallyAccessible = false;
                    UriBuilder Builder = new UriBuilder(URI);
                    Builder.Scheme = URI.Scheme == "lhttp" ? "http" : "https";
                    this.URI = Builder.Uri;
                    break;

                default:
                    throw new ArgumentException("Provided URI is not a HTTPS URL", nameof(URI));
            }
        }

        /// <inheritdoc cref="IResource.OpenStream"/>
        public Stream OpenStream() => HTTP.GetStream(URI);
    }
}
