#include "pch.h"

#include "legacy_preference_storage.h"

#include "serializer.h"
#include "settings.h"

namespace pp::panopainter {
namespace {

constexpr const char* kRunCounterKey = "run_counter";
constexpr const char* kAutoTimelapseKey = "auto-timelapse";
constexpr const char* kVrControllersEnabledKey = "vr-controllers-enabled";
constexpr const char* kViewportScaleKey = "vp-scale";
constexpr const char* kShowCursorKey = "show-cursor";
constexpr const char* kUiRightToLeftKey = "ui-rtl";
constexpr const char* kUiStateKey = "ui";
constexpr const char* kUiScaleKey = "ui-scale";
constexpr const char* kWindowShowCommandKey = "window-show-cmd";
constexpr const char* kWindowRectKey = "window-rect";
constexpr const char* kWhatsNewIdKey = "whatsnew-id";

bool has_legacy_preference_key(const char* key)
{
    return Settings::has(key);
}

int read_legacy_integer_preference(const char* key)
{
    return Settings::value<Serializer::Integer>(key);
}

int read_legacy_integer_preference_or(const char* key, int default_value)
{
    return Settings::value_or<Serializer::Integer>(key, default_value);
}

float read_legacy_float_preference(const char* key)
{
    return Settings::value<Serializer::Float>(key);
}

float read_legacy_float_preference_or(const char* key, float default_value)
{
    return Settings::value_or<Serializer::Float>(key, default_value);
}

bool read_legacy_boolean_preference_or(const char* key, bool default_value)
{
    return Settings::value_or<Serializer::Boolean>(key, default_value);
}

glm::ivec4 read_legacy_ivec4_preference(const char* key)
{
    return Settings::value<Serializer::IVec4>(key);
}

std::shared_ptr<Serializer::Descriptor> read_legacy_descriptor_preference(const char* key)
{
    return Settings::get<Serializer::Descriptor>(key);
}

void set_legacy_integer_preference(const char* key, int value)
{
    Settings::set(key, Serializer::Integer(value));
}

void set_legacy_boolean_preference(const char* key, bool value)
{
    Settings::set(key, Serializer::Boolean(value));
}

void set_legacy_float_preference(const char* key, float value)
{
    Settings::set(key, Serializer::Float(value));
}

void set_legacy_ivec4_preference(const char* key, const glm::ivec4& value)
{
    Settings::set(key, Serializer::IVec4(value));
}

void set_legacy_descriptor_preference(const char* key, const Serializer::Descriptor& value)
{
    Settings::set(key, value);
}

void save_legacy_boolean_preference(const char* key, bool value)
{
    set_legacy_boolean_preference(key, value);
    Settings::save();
}

void save_legacy_float_preference(const char* key, float value)
{
    set_legacy_float_preference(key, value);
    Settings::save();
}

} // namespace

bool load_legacy_preferences()
{
    return Settings::load();
}

LegacyStartupPreferenceSnapshot read_legacy_startup_preferences(bool default_vr_controllers_enabled)
{
    return {
        read_legacy_integer_preference(kRunCounterKey),
        read_legacy_boolean_preference_or(kAutoTimelapseKey, true),
        read_legacy_boolean_preference_or(kVrControllersEnabledKey, default_vr_controllers_enabled),
    };
}

LegacyCanvasPreferenceSnapshot read_legacy_canvas_preferences()
{
    return {
        read_legacy_float_preference_or(kViewportScaleKey, 1.0F),
        read_legacy_integer_preference_or(kShowCursorKey, 0),
    };
}

LegacyUiPreferenceSnapshot read_legacy_ui_preferences()
{
    LegacyUiPreferenceSnapshot snapshot;
    snapshot.has_rtl = has_legacy_preference_key(kUiRightToLeftKey);
    if (snapshot.has_rtl)
        snapshot.rtl = read_legacy_integer_preference(kUiRightToLeftKey);
    if (has_legacy_preference_key(kUiStateKey))
        snapshot.state = read_legacy_descriptor_preference(kUiStateKey);
    return snapshot;
}

LegacyWindowPreferenceSnapshot read_legacy_window_preferences(int default_show_command)
{
    LegacyWindowPreferenceSnapshot snapshot;
    snapshot.has_ui_scale = has_legacy_preference_key(kUiScaleKey);
    if (snapshot.has_ui_scale)
        snapshot.ui_scale = read_legacy_float_preference(kUiScaleKey);
    snapshot.show_command = read_legacy_integer_preference_or(kWindowShowCommandKey, default_show_command);
    snapshot.has_window_rect = has_legacy_preference_key(kWindowRectKey);
    if (snapshot.has_window_rect)
        snapshot.window_rect = read_legacy_ivec4_preference(kWindowRectKey);
    return snapshot;
}

void save_legacy_ui_scale_preference(float scale)
{
    set_legacy_float_preference(kUiScaleKey, scale);
    Settings::save();
}

void set_legacy_ui_state_preferences(const Serializer::Descriptor& state, bool right_to_left)
{
    set_legacy_descriptor_preference(kUiStateKey, state);
    set_legacy_boolean_preference(kUiRightToLeftKey, right_to_left);
}

void set_legacy_window_preferences(int show_command, const glm::ivec4& window_rect)
{
    set_legacy_integer_preference(kWindowShowCommandKey, show_command);
    set_legacy_ivec4_preference(kWindowRectKey, window_rect);
}

int legacy_whatsnew_id_or(int default_value)
{
    return read_legacy_integer_preference_or(kWhatsNewIdKey, default_value);
}

void clear_legacy_whatsnew_id()
{
    Settings::unset(kWhatsNewIdKey);
}

void set_legacy_whatsnew_id(int page_id)
{
    set_legacy_integer_preference(kWhatsNewIdKey, page_id);
}

void set_legacy_startup_run_counter(int value)
{
    set_legacy_integer_preference(kRunCounterKey, value);
}

void save_legacy_vr_controllers_enabled(bool enabled)
{
    save_legacy_boolean_preference(kVrControllersEnabledKey, enabled);
}

void save_legacy_auto_timelapse_enabled(bool enabled)
{
    save_legacy_boolean_preference(kAutoTimelapseKey, enabled);
}

void save_legacy_canvas_viewport_density(float density)
{
    save_legacy_float_preference(kViewportScaleKey, density);
}

void save_legacy_canvas_cursor_mode(int mode)
{
    set_legacy_integer_preference(kShowCursorKey, mode);
    Settings::save();
}

bool save_legacy_preferences()
{
    return Settings::save();
}

} // namespace pp::panopainter
