using System;
using UnityEngine;
namespace jeanf.universalplayer
{
///
/// Remembers the player's last desktop control mode so a session can start in
/// the mode they last used (Keyboard&Mouse vs Gamepad) instead of always
/// defaulting to Keyboard&Mouse. VR is never persisted here — it is decided by
/// headset presence at launch.
///
/// Storage is local () by default. A project that keeps
/// the preference in a database can set (read) and
/// (write); the external source wins over local memory,
/// and local memory remains the offline fallback.
///
public static class ControlModePreference
{
private const string PrefKey = "jeanf.universalplayer.lastDesktopMode";
/// Optional read override (e.g. a database). When it returns a value, that value wins over local memory.
public static Func ExternalProvider;
/// Optional write sink (e.g. a database) called alongside the local save.
public static Action ExternalSink;
/// The remembered desktop mode, or null if none is stored. Only ever KeyboardMouse or Gamepad.
public static BroadcastControlsStatus.ControlScheme? Load()
{
var external = ExternalProvider?.Invoke();
if (external.HasValue && IsDesktop(external.Value)) return external.Value;
if (!PlayerPrefs.HasKey(PrefKey)) return null;
var raw = PlayerPrefs.GetInt(PrefKey, -1);
if (!Enum.IsDefined(typeof(BroadcastControlsStatus.ControlScheme), raw)) return null;
var scheme = (BroadcastControlsStatus.ControlScheme)raw;
return IsDesktop(scheme) ? scheme : (BroadcastControlsStatus.ControlScheme?)null;
}
/// Persists a desktop mode. Non-desktop schemes (XR, FreeCam) are ignored so the last desktop choice survives VR/FreeCam sessions.
public static void Save(BroadcastControlsStatus.ControlScheme scheme)
{
if (!IsDesktop(scheme)) return;
PlayerPrefs.SetInt(PrefKey, (int)scheme);
PlayerPrefs.Save();
ExternalSink?.Invoke(scheme);
}
/// Forgets the locally stored desktop mode (used by tests and "reset to defaults").
public static void Clear() => PlayerPrefs.DeleteKey(PrefKey);
private static bool IsDesktop(BroadcastControlsStatus.ControlScheme scheme) =>
scheme == BroadcastControlsStatus.ControlScheme.KeyboardMouse ||
scheme == BroadcastControlsStatus.ControlScheme.Gamepad;
}
}