using System.Reflection;
using System;

namespace Shoko;

[Protocol("http")]
[Protocol("https")]
class HttpProtoHandler : ProtoHandler
{
    public HttpProtoHandler(Uri url)
    {
        URL = url;
    }
    long _totalBytes = -1;
    public override long TotalBytes => _totalBytes;

    public override async Task Load()
    {
        var client = new HttpClient(){
            BaseAddress = URL
        };
        client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 shoko/" + Assembly.GetExecutingAssembly().GetName().Version.ToString());

        var response = await client.GetAsync(URL, HttpCompletionOption.ResponseHeadersRead);

        Headers = response.Content.Headers;
        _totalBytes = response.Content.Headers.ContentLength ?? -1;
        MediaType = response.Content.Headers.ContentType.MediaType ?? "text/html";
        MediaTypeParams = response.Content.Headers.ContentType.Parameters.Select(x => new KeyValuePair<string, string>(x.Name, x.Value));
        Status = string.Format("{0} {1}", (int)response.StatusCode, response.StatusCode.ToString());
        var download = await response.Content.ReadAsStreamAsync();
        Content = await Download(download);
        OnLoaded();
    }
    public override void Render()
    {
    }
}