using System;
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.IO;
using SharpCompress.Readers;

namespace SharpCompress.Common;

public partial class EntryStream : AsyncDisposableStream
{
    private readonly IReader _reader;
    private readonly Stream _stream;
    private bool _completed;
    private bool _isDisposed;

    internal EntryStream(IReader reader, Stream stream)
    {
        _reader = reader;
        _stream = stream;
    }

    protected override void Dispose(bool disposing)
    {
        if (_isDisposed)
        {
            return;
        }
        _isDisposed = true;
        if (!(_completed || _reader.Cancelled))
        {
            SkipEntry();
        }

        //Need a safe standard approach to this - it's okay for compression to overreads. Handling needs to be standardised
        if (_stream is IStreamStack ss)
        {
            if (
                ss.GetStream<SharpCompress.Compressors.Deflate.DeflateStream>()
                is SharpCompress.Compressors.Deflate.DeflateStream deflateStream
            )
            {
                deflateStream.Flush(); //Deflate over reads. Knock it back
            }
            else if (
                ss.GetStream<SharpCompress.Compressors.LZMA.LzmaStream>()
                is SharpCompress.Compressors.LZMA.LzmaStream lzmaStream
            )
            {
                lzmaStream.Flush(); //Lzma over reads. Knock it back
            }
        }
        base.Dispose(disposing);
        _stream.Dispose();
    }

    public override bool CanRead => true;

    public override bool CanSeek => false;

    public override bool CanWrite => false;

    public override void Flush() { }

    public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;

    public override long Length => _stream.Length;

    public override long Position
    {
        get => _stream.Position; //throw new NotSupportedException();
        set => throw new NotSupportedException();
    }

    public override int ReadByte()
    {
        var value = _stream.ReadByte();
        if (value == -1)
        {
            _completed = true;
        }
        return value;
    }

    public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();

    public override void SetLength(long value) => throw new NotSupportedException();

    public override void Write(byte[] buffer, int offset, int count) =>
        throw new NotSupportedException();

    public override Task WriteAsync(
        byte[] buffer,
        int offset,
        int count,
        CancellationToken cancellationToken
    ) => throw new NotSupportedException();
}
