/*
 * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Function.h>
#include <AK/SinglyLinkedList.h>
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Streams/ReadableStreamGenericReader.h>

namespace Web::Streams {

struct ReadableStreamReadResult {
    JS::Value value;
    bool done;
};

namespace Detail {

class ReadableStreamPipeTo;

}

class ReadRequest : public JS::Cell {
    GC_CELL(ReadRequest, JS::Cell);

public:
    virtual ~ReadRequest() = default;

    virtual void on_chunk(JS::Value chunk) = 0;
    virtual void on_close() = 0;
    virtual void on_error(JS::Value error) = 0;
};

class ReadLoopReadRequest final : public ReadRequest {
    GC_CELL(ReadLoopReadRequest, ReadRequest);
    GC_DECLARE_ALLOCATOR(ReadLoopReadRequest);

public:
    // successSteps, which is an algorithm accepting a byte sequence
    using SuccessSteps = GC::Function<void(ByteBuffer)>;

    // failureSteps, which is an algorithm accepting a JavaScript value
    using FailureSteps = GC::Function<void(JS::Value error)>;

private:
    ReadLoopReadRequest(JS::Realm&, ReadableStreamDefaultReader&, GC::Ref<SuccessSteps>, GC::Ref<FailureSteps>);

    virtual void visit_edges(Visitor&) override;

    virtual void on_chunk(JS::Value chunk) override;
    virtual void on_close() override;
    virtual void on_error(JS::Value error) override;

    GC::Ref<JS::Realm> m_realm;
    GC::Ref<ReadableStreamDefaultReader> m_reader;
    ByteBuffer m_bytes;
    GC::Ref<SuccessSteps> m_success_steps;
    GC::Ref<FailureSteps> m_failure_steps;
};

// https://streams.spec.whatwg.org/#readablestreamdefaultreader
class ReadableStreamDefaultReader final
    : public Bindings::PlatformObject
    , public ReadableStreamGenericReaderMixin {
    WEB_PLATFORM_OBJECT(ReadableStreamDefaultReader, Bindings::PlatformObject);
    GC_DECLARE_ALLOCATOR(ReadableStreamDefaultReader);

public:
    static WebIDL::ExceptionOr<GC::Ref<ReadableStreamDefaultReader>> construct_impl(JS::Realm&, GC::Ref<ReadableStream>);

    virtual ~ReadableStreamDefaultReader() override = default;

    GC::Ref<WebIDL::Promise> read();

    void read_a_chunk(Fetch::Infrastructure::IncrementalReadLoopReadRequest& read_request);
    void read_all_bytes(GC::Ref<ReadLoopReadRequest::SuccessSteps>, GC::Ref<ReadLoopReadRequest::FailureSteps>);
    GC::Ref<WebIDL::Promise> read_all_bytes_deprecated();

    void release_lock();

    SinglyLinkedList<GC::Ref<ReadRequest>>& read_requests() { return m_read_requests; }

    void set_readable_stream_pipe_to_operation(Badge<Detail::ReadableStreamPipeTo>, GC::Ptr<JS::Cell> readable_stream_pipe_to_operation) { m_readable_stream_pipe_to_operation = readable_stream_pipe_to_operation; }

private:
    explicit ReadableStreamDefaultReader(JS::Realm&);

    virtual void initialize(JS::Realm&) override;

    virtual void visit_edges(Cell::Visitor&) override;

    SinglyLinkedList<GC::Ref<ReadRequest>> m_read_requests;

    GC::Ptr<JS::Cell> m_readable_stream_pipe_to_operation;
};

}
