package com.miraclegarden.smsmessage;

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

/**
 * App 保活相关设置（SharedPreferences，可在设置页修改）。
 */
public final class KeepAliveSettings {

    private static final String PREF = "keep_alive_settings";

    private static final String KEY_ENABLED = "enabled";
    private static final String KEY_INTERVAL_MINUTES = "interval_minutes";
    private static final String KEY_COOLDOWN_MINUTES = "cooldown_minutes";
    private static final String KEY_PREFER_ROOT = "prefer_root";
    private static final String KEY_RETURN_HOME = "return_home";
    private static final String KEY_KILL_BEFORE_LAUNCH = "kill_before_launch";
    private static final String KEY_STEALTH_MODE = "stealth_mode";

    public static final int MODE_STEALTH = 0;
    public static final int MODE_AGGRESSIVE = 1;

    public static final int WORK_MANAGER_MIN_MINUTES = 15;

    public static final int[] INTERVAL_OPTIONS = {1, 3, 5, 10, 15, 30, 45, 60};
    public static final int[] COOLDOWN_OPTIONS = {1, 3, 5, 10, 15, 20, 30};

    private KeepAliveSettings() {
    }

    private static SharedPreferences prefs(Context context) {
        return context.getApplicationContext().getSharedPreferences(PREF, Context.MODE_PRIVATE);
    }

    public static boolean isEnabled(Context context) {
        return prefs(context).getBoolean(KEY_ENABLED, AppConfig.ENABLE_MONITORED_APP_KEEP_ALIVE);
    }

    public static void setEnabled(Context context, boolean enabled) {
        prefs(context).edit().putBoolean(KEY_ENABLED, enabled).apply();
    }

    public static int getIntervalMinutes(Context context) {
        int value = prefs(context).getInt(KEY_INTERVAL_MINUTES, AppConfig.MONITORED_APP_KEEP_ALIVE_MINUTES);
        return Math.max(1, value);
    }

    public static void setIntervalMinutes(Context context, int minutes) {
        prefs(context).edit().putInt(KEY_INTERVAL_MINUTES, Math.max(1, minutes)).apply();
    }

    public static int getCooldownMinutes(Context context) {
        int value = prefs(context).getInt(KEY_COOLDOWN_MINUTES, AppConfig.MONITORED_APP_WAKE_COOLDOWN_MINUTES);
        return Math.max(1, value);
    }

    public static void setCooldownMinutes(Context context, int minutes) {
        prefs(context).edit().putInt(KEY_COOLDOWN_MINUTES, Math.max(1, minutes)).apply();
    }

    public static boolean preferRoot(Context context) {
        return prefs(context).getBoolean(KEY_PREFER_ROOT, AppConfig.KEEP_ALIVE_PREFER_ROOT);
    }

    public static void setPreferRoot(Context context, boolean preferRoot) {
        prefs(context).edit().putBoolean(KEY_PREFER_ROOT, preferRoot).apply();
    }

    public static boolean returnHome(Context context) {
        return prefs(context).getBoolean(KEY_RETURN_HOME, AppConfig.KEEP_ALIVE_RETURN_HOME);
    }

    public static void setReturnHome(Context context, boolean returnHome) {
        prefs(context).edit().putBoolean(KEY_RETURN_HOME, returnHome).apply();
    }

    /** 启动前 force-stop，确保冷启动并重新加载 Hook（会稍慢） */
    public static boolean killBeforeLaunch(Context context) {
        return prefs(context).getBoolean(KEY_KILL_BEFORE_LAUNCH, false);
    }

    public static void setKillBeforeLaunch(Context context, boolean killBeforeLaunch) {
        prefs(context).edit().putBoolean(KEY_KILL_BEFORE_LAUNCH, killBeforeLaunch).apply();
    }

    /** 静默：进程在就不弹 App；激进：每次都唤起界面 */
    public static boolean isStealthMode(Context context) {
        return prefs(context).getInt(KEY_STEALTH_MODE, MODE_STEALTH) == MODE_STEALTH;
    }

    public static void setStealthMode(Context context, boolean stealth) {
        prefs(context).edit()
                .putInt(KEY_STEALTH_MODE, stealth ? MODE_STEALTH : MODE_AGGRESSIVE)
                .apply();
    }

    public static int indexOfInterval(int minutes) {
        for (int i = 0; i < INTERVAL_OPTIONS.length; i++) {
            if (INTERVAL_OPTIONS[i] == minutes) {
                return i;
            }
        }
        return 0;
    }

    public static int indexOfCooldown(int minutes) {
        for (int i = 0; i < COOLDOWN_OPTIONS.length; i++) {
            if (COOLDOWN_OPTIONS[i] == minutes) {
                return i;
            }
        }
        return 1;
    }
}
