#include "platform_api/platform_policy.h"

#include <string>

namespace pp::platform {

PlatformFamily current_platform_family() noexcept
{
#if defined(__IOS__)
    return PlatformFamily::ios;
#elif defined(__OSX__)
    return PlatformFamily::macos;
#elif defined(__ANDROID__)
    return PlatformFamily::android;
#elif defined(__LINUX__)
    return PlatformFamily::linux_desktop;
#elif defined(__WEB__)
    return PlatformFamily::webgl;
#elif defined(_WIN32)
    return PlatformFamily::windows;
#else
    return PlatformFamily::generic_desktop;
#endif
}

const char* xr_runtime_backend_name(XrRuntimeBackend backend) noexcept
{
    switch (backend)
    {
    case XrRuntimeBackend::openxr:
        return "openxr";
    case XrRuntimeBackend::openvr:
        return "openvr";
    case XrRuntimeBackend::none:
    default:
        return "none";
    }
}

XrRuntimeSelection select_desktop_xr_runtime(
    bool openxr_available,
    bool openvr_available,
    bool allow_legacy_openvr_fallback) noexcept
{
    if (openxr_available)
        return { XrRuntimeBackend::openxr, false };

    if (allow_legacy_openvr_fallback && openvr_available)
        return { XrRuntimeBackend::openvr, true };

    return {};
}

bool platform_deletes_recorded_files_on_clear(PlatformFamily family) noexcept
{
    return family == PlatformFamily::ios || family == PlatformFamily::macos;
}

bool platform_publishes_exported_images(PlatformFamily family) noexcept
{
    return family == PlatformFamily::ios;
}

bool platform_flushes_persistent_storage(PlatformFamily family) noexcept
{
    return family == PlatformFamily::webgl;
}

bool platform_enables_live_asset_reloading(PlatformFamily family) noexcept
{
    return family == PlatformFamily::windows || family == PlatformFamily::macos;
}

bool platform_uses_asset_file_mtime_reload(PlatformFamily family) noexcept
{
    return family == PlatformFamily::windows || family == PlatformFamily::macos;
}

bool platform_saves_native_ui_state(PlatformFamily family) noexcept
{
    return family == PlatformFamily::windows || family == PlatformFamily::macos;
}

bool platform_supports_working_directory_picker(PlatformFamily family) noexcept
{
    return family == PlatformFamily::windows || family == PlatformFamily::macos;
}

bool platform_uses_prepared_file_writes(PlatformFamily family) noexcept
{
    return family == PlatformFamily::ios || family == PlatformFamily::webgl;
}

bool platform_uses_work_directory_document_export_collections(PlatformFamily family) noexcept
{
    return family == PlatformFamily::ios;
}

bool platform_uses_ppbr_export_data_directory_override(PlatformFamily family) noexcept
{
    return family == PlatformFamily::macos;
}

bool platform_supports_sonarpen(PlatformFamily family) noexcept
{
    return family == PlatformFamily::ios;
}

int platform_default_canvas_resolution(PlatformFamily family) noexcept
{
    return family == PlatformFamily::webgl ? 512 : 1536;
}

bool platform_draws_canvas_tip_for_pointer(
    PlatformFamily family,
    bool is_mouse,
    bool is_stylus,
    bool is_left_button_release) noexcept
{
    if (family == PlatformFamily::ios)
        return is_mouse && !is_left_button_release;
    return is_mouse || is_stylus;
}

std::vector<std::string> platform_document_browse_roots(
    PlatformFamily family,
    std::string_view work_path,
    std::string_view data_path)
{
    if (family == PlatformFamily::ios)
    {
        return {
            std::string(work_path),
            std::string(data_path) + "/Inbox",
        };
    }

    return { std::string(work_path) };
}

PreparedFileTarget plan_platform_writable_file(
    PlatformFamily family,
    std::string_view type,
    std::string_view default_name,
    std::string_view data_path,
    std::string_view temporary_path)
{
    const std::string name = std::string(default_name) + "." + std::string(type);

    if (family == PlatformFamily::ios)
    {
        return {
            std::string(temporary_path) + "/" + name,
            name,
            true,
        };
    }

    if (family == PlatformFamily::webgl)
    {
        return {
            std::string(data_path) + "/" + name,
            name,
            false,
        };
    }

    return {};
}

}
