/*
 * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Utf16String.h>
#include <AK/WeakPtr.h>
#include <LibCore/File.h>
#include <LibCore/Process.h>
#include <LibIPC/Connection.h>
#include <LibIPC/Transport.h>
#include <LibWebView/Forward.h>
#include <LibWebView/ProcessType.h>

namespace WebView {

struct ProcessOutputCapture {
    OwnPtr<Core::File> stdout_file;
    OwnPtr<Core::File> stderr_file;
};

class WEBVIEW_API Process {
    AK_MAKE_NONCOPYABLE(Process);
    AK_MAKE_DEFAULT_MOVABLE(Process);

public:
    Process(ProcessType type, RefPtr<IPC::ConnectionBase> connection, Core::Process process);
    ~Process();

    template<typename ClientType>
    struct ProcessAndClient;

    template<typename ClientType, typename... ClientArguments>
    static ErrorOr<ProcessAndClient<ClientType>> spawn(ProcessType type, Core::ProcessSpawnOptions const& options, bool capture_output, ClientArguments&&... client_arguments);

    ProcessType type() const { return m_type; }

    Optional<Utf16String> const& title() const { return m_title; }
    void set_title(Optional<Utf16String> title) { m_title = move(title); }

    template<typename ConnectionFromClient>
    Optional<ConnectionFromClient&> client()
    {
        if (auto strong_connection = m_connection.strong_ref())
            return as<ConnectionFromClient>(*strong_connection);
        return {};
    }

    pid_t pid() const { return m_process.pid(); }

    ProcessOutputCapture& output_capture() { return m_output_capture; }
    ProcessOutputCapture const& output_capture() const { return m_output_capture; }

    struct ProcessPaths {
        ByteString socket_path;
        ByteString pid_path;
    };
    static ErrorOr<ProcessPaths> paths_for_process(StringView process_name);
    static ErrorOr<Optional<pid_t>> get_process_pid(StringView process_name, StringView pid_path);
    static ErrorOr<int> create_ipc_socket(ByteString const& socket_path);

private:
    struct ProcessAndIPCTransport {
        Core::Process process;
        NonnullOwnPtr<IPC::Transport> transport;
        ProcessOutputCapture output_capture;
    };
    static ErrorOr<ProcessAndIPCTransport> spawn_and_connect_to_process(Core::ProcessSpawnOptions const& options, bool capture_output);

    Core::Process m_process;
    ProcessType m_type;
    Optional<Utf16String> m_title;
    WeakPtr<IPC::ConnectionBase> m_connection;
    ProcessOutputCapture m_output_capture;
};

template<typename ClientType>
struct Process::ProcessAndClient {
    Process process;
    NonnullRefPtr<ClientType> client;
};

template<typename ClientType, typename... ClientArguments>
ErrorOr<Process::ProcessAndClient<ClientType>> Process::spawn(ProcessType type, Core::ProcessSpawnOptions const& options, bool capture_output, ClientArguments&&... client_arguments)
{
    auto [core_process, transport, output_capture] = TRY(spawn_and_connect_to_process(options, capture_output));
    auto client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ClientType { move(transport), forward<ClientArguments>(client_arguments)... }));

    Process process { type, client, move(core_process) };
    process.m_output_capture = move(output_capture);
    return ProcessAndClient<ClientType> { move(process), client };
}

}
