#pragma once
#include <myapp/async/pipe.h>
#include <memory>
#include <uvw.hpp>

namespace myapp::async {

class uv_pipe final : public pipe
{
public:
    explicit uv_pipe(std::shared_ptr<uvw::PipeHandle>);
    ~uv_pipe();

    uv_pipe(uv_pipe &&) noexcept = default;
    uv_pipe& operator=(uv_pipe &&) noexcept = default;

    uvw::PipeHandle& handle() noexcept;
    uvw::PipeHandle const& handle() const noexcept;

    std::unique_ptr<pipe> accept() override;
    void bind(std::string name) override;
    void connect(std::string name, std::function<void()> callback) override;
    void listen(int backlog, std::function<void()> callback) override;
    void open(int fd) override;
    void close() override;

    void shutdown(std::function<void()>) override;
    void write(std::string, std::function<void()>) override;
    void read(std::function<void(std::string)> data, std::function<void()> end) override;
    void stop_read() override;

    void on_error(std::function<void( std::function<void()> )>) override;

private:
    std::shared_ptr<uvw::PipeHandle> pipe_;
};

} /* namespace myapp::async */
