#pragma once

#include <functional>
#include <string>
#include <string_view>
#include <vector>

namespace pp::platform {

using PickedPathCallback = std::function<void(std::string path)>;
using PreparedFileCallback = std::function<void(std::string path, bool saved)>;

struct PreparedFileTarget {
    std::string path;
    std::string suggested_name;
    bool write_on_background_thread = false;
};

struct PlatformStoragePaths {
    std::string data_path;
    std::string work_path;
    std::string recording_path;
    std::string temporary_path;
};

class PlatformServices {
public:
    virtual ~PlatformServices() = default;

    [[nodiscard]] virtual PlatformStoragePaths prepare_storage_paths() = 0;
    virtual void log_stacktrace() = 0;
    virtual void trigger_crash_test() = 0;
    [[nodiscard]] virtual std::string clipboard_text() = 0;
    [[nodiscard]] virtual bool set_clipboard_text(std::string_view text) = 0;
    virtual void set_cursor_visible(bool visible) = 0;
    virtual void set_virtual_keyboard_visible(bool visible) = 0;
    virtual void attach_ui_thread() = 0;
    virtual void detach_ui_thread() = 0;
    virtual void acquire_render_context() = 0;
    virtual void release_render_context() = 0;
    virtual void present_render_context() = 0;
    virtual void bind_default_render_target() = 0;
    virtual void bind_main_render_target() = 0;
    virtual void apply_render_platform_hints() = 0;
    virtual void install_render_debug_callback() = 0;
    virtual void begin_render_capture_frame() = 0;
    virtual void end_render_capture_frame() = 0;
    [[nodiscard]] virtual bool deletes_recorded_files_on_clear() = 0;
    virtual void clear_recorded_files(std::string_view recording_path) = 0;
    virtual void publish_exported_image(std::string_view path) = 0;
    virtual void flush_persistent_storage() = 0;
    [[nodiscard]] virtual std::vector<std::string> document_browse_roots(
        std::string_view work_path,
        std::string_view data_path) = 0;
    virtual void save_ui_state() = 0;
    [[nodiscard]] virtual bool enables_live_asset_reloading() = 0;
    virtual void update_platform_frame(float delta_time_seconds) = 0;
    virtual void report_rendered_frames(int frames) = 0;
    virtual void display_file(std::string_view path) = 0;
    virtual void share_file(std::string_view path) = 0;
    virtual void request_app_close() = 0;
    [[nodiscard]] virtual bool start_vr_mode() = 0;
    virtual void stop_vr_mode() = 0;
    virtual void pick_image(PickedPathCallback callback) = 0;
    virtual void pick_file(std::vector<std::string> file_types, PickedPathCallback callback) = 0;
    virtual void pick_save_file(std::vector<std::string> file_types, PickedPathCallback callback) = 0;
    virtual void pick_directory(PickedPathCallback callback) = 0;
    [[nodiscard]] virtual bool supports_working_directory_picker() = 0;
    [[nodiscard]] virtual std::string format_working_directory_path(std::string_view path) = 0;
    [[nodiscard]] virtual bool uses_prepared_file_writes() = 0;
    [[nodiscard]] virtual bool uses_work_directory_document_export_collections() = 0;
    [[nodiscard]] virtual bool disables_network_tls_verification() = 0;
    [[nodiscard]] virtual bool uses_ppbr_export_data_directory_override() = 0;
    [[nodiscard]] virtual bool supports_sonarpen() = 0;
    virtual void start_sonarpen() = 0;
    [[nodiscard]] virtual int default_canvas_resolution() = 0;
    [[nodiscard]] virtual bool draws_canvas_tip_for_pointer(
        bool is_mouse,
        bool is_stylus,
        bool is_left_button_release) = 0;
    [[nodiscard]] virtual float adjust_canvas_input_pressure(float pressure) = 0;
    [[nodiscard]] virtual PreparedFileTarget prepare_writable_file(
        std::string_view type,
        std::string_view default_name,
        std::string_view data_path,
        std::string_view temporary_path) = 0;
    virtual void save_prepared_file(
        std::string_view path,
        std::string_view suggested_name,
        PreparedFileCallback callback) = 0;
};

class WebPlatformServices {
public:
    virtual ~WebPlatformServices() = default;

    virtual void publish_exported_image(std::string_view path) = 0;
    virtual void flush_persistent_storage() = 0;
    [[nodiscard]] virtual int default_canvas_resolution() = 0;
    virtual void save_prepared_file(
        std::string_view path,
        std::string_view suggested_name,
        PreparedFileCallback callback) = 0;
};

[[nodiscard]] WebPlatformServices& default_web_platform_services();
[[nodiscard]] WebPlatformServices* injected_web_platform_services() noexcept;
void set_injected_web_platform_services(WebPlatformServices* services) noexcept;
[[nodiscard]] WebPlatformServices& resolve_web_platform_services(WebPlatformServices& fallback) noexcept;
[[nodiscard]] WebPlatformServices& active_web_platform_services();

}
