#include "pch.h"

#include "legacy_app_preference_services.h"

#include "app.h"
#include "legacy_canvas_view_services.h"
#include "legacy_preference_storage.h"

namespace pp::panopainter {
namespace {

class LegacyAppPreferenceServices final : public pp::app::AppPreferenceServices {
public:
    explicit LegacyAppPreferenceServices(App& app) noexcept
        : app_(app)
    {
    }

    void apply_ui_scale(const pp::app::ScaleApplicationPlan& plan) override
    {
        app_.set_ui_scale(plan.scale);
    }

    void apply_viewport_scale(const pp::app::ScaleApplicationPlan& plan) override
    {
        const auto status = execute_legacy_canvas_view_density(app_, plan.scale);
        if (!status.ok())
            LOG("Viewport scale preference failed: %s", status.message);
    }

    void apply_interface_direction(const pp::app::InterfaceDirectionPlan& plan) override
    {
        app_.set_ui_rtl(plan.direction == pp::app::InterfaceDirection::right_to_left);
    }

    bool apply_vr_mode_preference(const pp::app::StoredBooleanPreferencePlan& plan) override
    {
        if (plan.value) {
            return app_.vr_start();
        }

        app_.vr_stop();
        return true;
    }

    void apply_vr_controllers_preference(const pp::app::StoredBooleanPreferencePlan& plan) override
    {
        app_.vr_controllers_enabled = plan.value;
        save_legacy_vr_controllers_enabled(plan.value);
    }

    void apply_timelapse_preference(const pp::app::TimelapsePreferencePlan& plan) override
    {
        if (plan.recording_action == pp::app::TimelapseRecordingAction::stop_recording) {
            app_.rec_stop();
        } else if (plan.recording_action == pp::app::TimelapseRecordingAction::start_recording) {
            app_.rec_start();
        }

        save_legacy_auto_timelapse_enabled(plan.enabled);
    }

    void apply_canvas_cursor_mode(const pp::app::StoredIntegerPreferencePlan& plan) override
    {
        const auto status = execute_legacy_canvas_cursor_mode(app_, plan.value);
        if (!status.ok())
            LOG("Canvas cursor mode preference failed: %s", status.message);
    }

private:
    App& app_;
};

} // namespace

pp::foundation::Status execute_legacy_ui_scale_preference(App& app, float requested_scale)
{
    LegacyAppPreferenceServices services(app);
    return pp::app::execute_ui_scale_preference(requested_scale, app.display_density, services);
}

pp::foundation::Status execute_legacy_viewport_scale_preference(App& app, float requested_scale)
{
    LegacyAppPreferenceServices services(app);
    return pp::app::execute_viewport_scale_preference(requested_scale, 1.0F, services);
}

pp::foundation::Status execute_legacy_interface_direction_preference(App& app, bool right_to_left)
{
    LegacyAppPreferenceServices services(app);
    return pp::app::execute_interface_direction_preference(right_to_left, services);
}

pp::foundation::Status execute_legacy_vr_mode_preference(App& app, bool enabled)
{
    LegacyAppPreferenceServices services(app);
    return pp::app::execute_vr_mode_preference(enabled, services);
}

pp::foundation::Status execute_legacy_vr_controllers_preference(App& app, bool enabled)
{
    LegacyAppPreferenceServices services(app);
    return pp::app::execute_vr_controllers_preference(enabled, services);
}

pp::foundation::Status execute_legacy_timelapse_preference(App& app, bool enabled)
{
    LegacyAppPreferenceServices services(app);
    return pp::app::execute_timelapse_preference(enabled, app.rec_running, services);
}

pp::foundation::Status execute_legacy_canvas_cursor_mode_preference(App& app, int mode)
{
    LegacyAppPreferenceServices services(app);
    return pp::app::execute_canvas_cursor_mode_preference(mode, services);
}

} // namespace pp::panopainter
