package invalid.lena.scrcpy;

import android.content.Context;
import android.content.SharedPreferences;

// Thin wrapper around SharedPreferences for the small set of user
// choices the app exposes. No reactivity, no DataStore - the values
// are read once when a Session starts and applied to the scrcpy
// server command line.
public final class Settings {

    private static final String FILE = "scrcpy-android";

    public static final String VIDEO_CODEC    = "video_codec";
    public static final String AUDIO_CODEC    = "audio_codec";
    public static final String MAX_SIZE       = "max_size";        // px, long edge; 0 = no cap
    public static final String VIDEO_BIT_RATE = "video_bit_rate";  // bits/sec
    public static final String MAX_FPS        = "max_fps";         // fps; 0 = unlimited
    public static final String HINT_BACK_SHOWN = "hint_back_shown"; // first-run UI hint
    public static final String STATUS_BAR      = "status_bar";      // show status bar while mirroring

    public static final String DEFAULT_VIDEO_CODEC    = "h264";
    public static final String DEFAULT_AUDIO_CODEC    = "raw";
    public static final int    DEFAULT_MAX_SIZE       = 0;
    public static final int    DEFAULT_VIDEO_BIT_RATE = 8_000_000;
    public static final int    DEFAULT_MAX_FPS        = 0;
    // Hidden while mirroring by default: an always-on bar over the video
    // is intrusive. It still auto-shows when not CONNECTED so Reconnect
    // stays reachable. Record lives in the bar, so enable this to keep it.
    public static final boolean DEFAULT_STATUS_BAR    = false;

    private Settings() {}

    public static SharedPreferences prefs(Context ctx) {
        return ctx.getApplicationContext().getSharedPreferences(FILE, Context.MODE_PRIVATE);
    }

    public static String videoCodec(Context ctx) {
        return prefs(ctx).getString(VIDEO_CODEC, DEFAULT_VIDEO_CODEC);
    }

    public static String audioCodec(Context ctx) {
        return prefs(ctx).getString(AUDIO_CODEC, DEFAULT_AUDIO_CODEC);
    }

    public static int maxSize(Context ctx) {
        return prefs(ctx).getInt(MAX_SIZE, DEFAULT_MAX_SIZE);
    }

    public static int videoBitRate(Context ctx) {
        return prefs(ctx).getInt(VIDEO_BIT_RATE, DEFAULT_VIDEO_BIT_RATE);
    }

    public static int maxFps(Context ctx) {
        return prefs(ctx).getInt(MAX_FPS, DEFAULT_MAX_FPS);
    }

    public static void setVideoCodec(Context ctx, String v) {
        prefs(ctx).edit().putString(VIDEO_CODEC, v).apply();
    }

    public static void setAudioCodec(Context ctx, String a) {
        prefs(ctx).edit().putString(AUDIO_CODEC, a).apply();
    }

    public static void setMaxSize(Context ctx, int v) {
        prefs(ctx).edit().putInt(MAX_SIZE, v).apply();
    }

    public static void setVideoBitRate(Context ctx, int v) {
        prefs(ctx).edit().putInt(VIDEO_BIT_RATE, v).apply();
    }

    public static void setMaxFps(Context ctx, int v) {
        prefs(ctx).edit().putInt(MAX_FPS, v).apply();
    }

    public static boolean hintBackShown(Context ctx) {
        return prefs(ctx).getBoolean(HINT_BACK_SHOWN, false);
    }

    public static void setHintBackShown(Context ctx, boolean v) {
        prefs(ctx).edit().putBoolean(HINT_BACK_SHOWN, v).apply();
    }

    public static boolean showStatusBar(Context ctx) {
        return prefs(ctx).getBoolean(STATUS_BAR, DEFAULT_STATUS_BAR);
    }

    public static void setShowStatusBar(Context ctx, boolean v) {
        prefs(ctx).edit().putBoolean(STATUS_BAR, v).apply();
    }
}
