using System;
using System.Buffers;
using System.IO;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Compressors.LZMA;
using SharpCompress.Compressors.Xz;
using SharpCompress.Test.Mocks;
using Xunit;

namespace SharpCompress.Test.Streams;

public class LzmaStreamAsyncTests : TestBase
{
    [Fact]
    public async ValueTask TestLzma2Decompress()
    {
        using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "bad-1-lzma2-7.xz"));

        using var xz = new XZStream(stream);
        await Assert.ThrowsAnyAsync<SharpCompressException>(async () =>
            await xz.TransferToAsync(Stream.Null, long.MaxValue)
        );
    }

    [Fact]
    public async ValueTask TestLzma2Decompress1ByteAsync()
    {
        var properties = new byte[] { 0x01 };
        var compressedData = new byte[] { 0x01, 0x00, 0x00, 0x58, 0x00 };
        var lzma2Stream = new MemoryStream(compressedData);

        var decompressor = LzmaStream.Create(properties, lzma2Stream, 5, 1);
        var buffer = new byte[1];
        var bytesRead = await decompressor.ReadAsync(buffer, 0, 1).ConfigureAwait(false);
        Assert.Equal(1, bytesRead);
        Assert.Equal((byte)'X', buffer[0]);
    }

    [Fact]
    public async Task LzmaStream_SynchronousReadThenAsynchronousRead_Throws()
    {
        using var stream = CreateRawLzmaStream();
        var buffer = new byte[1];

        Assert.Equal(1, stream.Read(buffer, 0, buffer.Length));

        await Assert.ThrowsAsync<InvalidOperationException>(() =>
            stream.ReadAsync(buffer, 0, buffer.Length)
        );
    }

#if !LEGACY_DOTNET
    [Fact]
    public async Task LzmaStream_AsynchronousMemoryReadThenSynchronousRead_Throws()
    {
        using var stream = CreateRawLzmaStream();
        var buffer = new byte[1];

        Assert.Equal(1, await stream.ReadAsync(buffer.AsMemory()));

        Assert.Throws<InvalidOperationException>(() => stream.Read(buffer, 0, buffer.Length));
    }
#endif

    [Fact]
    public async Task LzmaStream_ZeroLengthReadsDoNotSelectReadMode()
    {
        using var stream = CreateRawLzmaStream();
        var empty = Array.Empty<byte>();

        Assert.Equal(0, stream.Read(empty, 0, 0));
        Assert.Equal(0, await stream.ReadAsync(empty, 0, 0));
        Assert.Equal(1, stream.ReadByte());

        await Assert.ThrowsAsync<InvalidOperationException>(() =>
            stream.ReadAsync(new byte[1], 0, 1)
        );
    }

    private static byte[] LzmaData { get; } =
    [
        0x5D,
        0x00,
        0x20,
        0x00,
        0x00,
        0x48,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x80,
        0x24,
        0x18,
        0x2F,
        0xEB,
        0x20,
        0x78,
        0xBA,
        0x78,
        0x70,
        0xDC,
        0x43,
        0x2C,
        0x32,
        0xC9,
        0xC3,
        0x97,
        0x4D,
        0x10,
        0x74,
        0xE2,
        0x20,
        0xBF,
        0x5A,
        0xB4,
        0xB3,
        0xC4,
        0x31,
        0x80,
        0x26,
        0x3E,
        0x6A,
        0xEA,
        0x51,
        0xFC,
        0xE4,
        0x8D,
        0x54,
        0x96,
        0x05,
        0xCC,
        0x78,
        0x59,
        0xAC,
        0xD4,
        0x21,
        0x65,
        0x8F,
        0xA9,
        0xC8,
        0x0D,
        0x9B,
        0xE2,
        0xC2,
        0xF9,
        0x7C,
        0x3C,
        0xDD,
        0x4D,
        0x38,
        0x04,
        0x0B,
        0xF8,
        0x0B,
        0x68,
        0xA5,
        0x93,
        0x6C,
        0x64,
        0xAC,
        0xCF,
        0x71,
        0x68,
        0xE8,
        0x69,
        0x25,
        0xC6,
        0x17,
        0x28,
        0xF1,
        0x7C,
        0xF1,
        0xDC,
        0x47,
        0x51,
        0x4D,
        0x1E,
        0x0E,
        0x0B,
        0x80,
        0x37,
        0x24,
        0x58,
        0x80,
        0xF7,
        0xB4,
        0xAC,
        0x54,
        0xF1,
        0x0F,
        0x7F,
        0x0F,
        0x0F,
        0xF5,
        0x9C,
        0xDE,
        0x54,
        0x4F,
        0xA3,
        0x7B,
        0x20,
        0xC5,
        0xA8,
        0x18,
        0x3B,
        0xED,
        0xDC,
        0x04,
        0xF6,
        0xFB,
        0x86,
        0xE0,
        0xAB,
        0xB6,
        0x87,
        0x99,
        0x92,
        0x43,
        0x7B,
        0x2C,
        0xCC,
        0x31,
        0x83,
        0x90,
        0xFF,
        0xF1,
        0x76,
        0x03,
        0x90,
    ];

    /// <summary>
    /// The decoded data for <see cref="LzmaData"/>.
    /// </summary>
    private static byte[] LzmaResultData { get; } =
    [
        0x01,
        0x00,
        0xFD,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0xFA,
        0x61,
        0x18,
        0x5F,
        0x02,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0x00,
        0x00,
        0x03,
        0x00,
        0x00,
        0x00,
        0x01,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3D,
        0x61,
        0xE5,
        0x5E,
        0x03,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x12,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0xE2,
        0x61,
        0x18,
        0x5F,
        0x04,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x12,
        0x00,
        0x00,
        0x00,
        0x29,
        0x00,
        0x00,
        0x00,
        0x01,
        0x00,
        0xFD,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x14,
        0x62,
        0x18,
        0x5F,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x03,
        0x00,
        0x00,
        0x00,
        0x40,
        0x00,
        0x00,
        0x00,
        0x09,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x7F,
        0x61,
        0xE5,
        0x5E,
        0x05,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3B,
        0x00,
        0x00,
        0x00,
        0xCB,
        0x15,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x7F,
        0x61,
        0xE5,
        0x5E,
        0x06,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3B,
        0x00,
        0x00,
        0x00,
        0xCB,
        0x15,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3D,
        0x61,
        0xE5,
        0x5E,
        0x07,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x12,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0xFC,
        0x96,
        0x40,
        0x5C,
        0x08,
        0x00,
        0x00,
        0x00,
        0x60,
        0x00,
        0x00,
        0x00,
        0xFF,
        0xFF,
        0xFF,
        0xFF,
        0x00,
        0x00,
        0x00,
        0x00,
        0xF8,
        0x83,
        0x12,
        0x00,
        0xD4,
        0x99,
        0x00,
        0x00,
        0x43,
        0x95,
        0x00,
        0x00,
        0xEB,
        0x7A,
        0x00,
        0x00,
        0x40,
        0x6F,
        0x00,
        0x00,
        0xD2,
        0x6F,
        0x00,
        0x00,
        0x67,
        0x74,
        0x00,
        0x00,
        0x02,
        0x69,
        0x00,
        0x00,
        0x76,
        0x79,
        0x00,
        0x00,
        0x98,
        0x66,
        0x00,
        0x00,
        0x23,
        0x25,
        0x00,
        0x00,
        0x01,
        0x00,
        0xFD,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3B,
        0x2F,
        0xC0,
        0x5F,
        0x09,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x03,
        0x00,
        0x00,
        0x00,
        0x69,
        0x00,
        0x3D,
        0x00,
        0x0A,
        0x00,
        0x00,
        0x00,
    ];

    [Fact]
    public async ValueTask TestLzmaBufferAsync()
    {
        var input = new MemoryStream(LzmaData);
        using var output = new MemoryStream();
        var properties = new byte[5];
        await input.ReadAsync(properties, 0, 5).ConfigureAwait(false);

        var fileLengthBytes = new byte[8];
        await input.ReadAsync(fileLengthBytes, 0, 8).ConfigureAwait(false);
        var fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

        var coder = new Decoder();
        coder.SetDecoderProperties(properties);
        coder.Code(input, output, input.Length, fileLength, null);

        Assert.Equal(output.ToArray(), LzmaResultData);
    }

    [Fact]
    public async ValueTask TestLzmaStreamEncodingWritesDataAsync()
    {
        using var inputStream = new MemoryStream(LzmaResultData);
        using MemoryStream outputStream = new();
        await using var lzmaStream = LzmaStream.Create(
            LzmaEncoderProperties.Default,
            false,
            new AsyncOnlyStream(outputStream, disposeStream: false)
        );
        await inputStream.CopyToAsync(lzmaStream).ConfigureAwait(false);
        await lzmaStream.DisposeAsync().ConfigureAwait(false);
        Assert.NotEqual(0, outputStream.Length);
    }

    [Fact]
    public async ValueTask TestLzmaEncodingAccuracyAsync()
    {
        var input = new MemoryStream(LzmaResultData);
        var compressed = new MemoryStream();
        var lzmaEncodingStream = LzmaStream.Create(
            LzmaEncoderProperties.Default,
            false,
            new AsyncOnlyStream(compressed, disposeStream: false)
        );
        await input.CopyToAsync(lzmaEncodingStream).ConfigureAwait(false);
        await lzmaEncodingStream.DisposeAsync().ConfigureAwait(false);
        compressed.Position = 0;

        var output = new MemoryStream();
        await DecompressLzmaStreamAsync(
                lzmaEncodingStream.Properties,
                compressed,
                compressed.Length,
                output,
                LzmaResultData.LongLength
            )
            .ConfigureAwait(false);

        Assert.Equal(output.ToArray(), LzmaResultData);
    }

    private static async Task DecompressLzmaStreamAsync(
        byte[] properties,
        Stream compressedStream,
        long compressedSize,
        Stream decompressedStream,
        long decompressedSize
    )
    {
        var lzmaStream = await LzmaStream.CreateAsync(
            properties,
            compressedStream,
            compressedSize,
            -1,
            null,
            false
        );

        var buffer = new byte[1024];
        long totalRead = 0;
        while (totalRead < decompressedSize)
        {
            var toRead = (int)Math.Min(buffer.Length, decompressedSize - totalRead);
            var read = await lzmaStream.ReadAsync(buffer, 0, toRead).ConfigureAwait(false);
            if (read > 0)
            {
                await decompressedStream.WriteAsync(buffer, 0, read).ConfigureAwait(false);
                totalRead += read;
            }
            else
            {
                break;
            }
        }
    }

    private static LzmaStream CreateRawLzmaStream()
    {
        var input = new MemoryStream(LzmaData);
        var properties = new byte[5];
        Buffer.BlockCopy(LzmaData, 0, properties, 0, properties.Length);
        var outputSize = BitConverter.ToInt64(LzmaData, 5);
        input.Position = 13;
        return LzmaStream.Create(properties, input, input.Length - input.Position, outputSize);
    }
}
