using System.IO.Compression;
using HeyRed.Mime;

namespace Shoko;

[MediaType("application/gzip")]
class GzipMediaHandler : MediaHandler
{
    MediaHandler newHandler;
    public GzipMediaHandler(ProtoHandler content)
    {
        Content = content;
    }

    public override async Task Load()
    {
        var stream = new GZipStream(Content.Content, CompressionMode.Decompress);
        var mem = new MemoryStream();
        await stream.CopyToAsync(mem);
        mem.Position = 0;
        Content.MediaType = MimeGuesser.GuessMimeType(mem);
        Content.Content = mem;

        newHandler = GetHandler(Content);
        await newHandler.Load();
        OnLoaded();
    }

    public override void Render()
    {
        if(newHandler?.IsLoaded ?? false)
            newHandler.Render();
    }

    public override void MenuBar()
    {
        if(newHandler?.IsLoaded ?? false)
            newHandler.MenuBar();
    }
}