package com.dspread.pos.data.local;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;

/**
 * Centralized SharedPreferences manager with type-safe methods
 */
public class PreferencesManager {
    private static final String PREFS_NAME = "PosAppPrefs";
    private static volatile PreferencesManager instance;

    private final SharedPreferences sharedPreferences;
    private final SharedPreferences.Editor editor;

    private PreferencesManager(@NonNull Context context) {
        sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public static synchronized PreferencesManager getInstance(Context context) {
        if (instance == null) {
            instance = new PreferencesManager(context.getApplicationContext());
        }
        return instance;
    }

    // String operations
    public void saveString(@NonNull String key, String value) {
        editor.putString(key, value).apply();
    }

    public String getString(@NonNull String key, String defaultValue) {
        return sharedPreferences.getString(key, defaultValue);
    }
    // Add these methods for better type handling
    public String getStringSafe(String key, String defaultValue) {
        try {
            return sharedPreferences.getString(key, defaultValue);
        } catch (ClassCastException e) {
            return defaultValue;
        }
    }

    public int getIntSafe(String key, int defaultValue) {
        try {
            return sharedPreferences.getInt(key, defaultValue);
        } catch (ClassCastException e) {
            // Try to get as String and parse
            try {
                String value = sharedPreferences.getString(key, String.valueOf(defaultValue));
                return Integer.parseInt(value);
            } catch (Exception ex) {
                return defaultValue;
            }
        }
    }


    // Integer operations
    public void saveInt(@NonNull String key, int value) {
        editor.putInt(key, value).apply();
    }

    public int getInt(@NonNull String key, int defaultValue) {
        return sharedPreferences.getInt(key, defaultValue);
    }

    // Boolean operations
    public void saveBoolean(@NonNull String key, boolean value) {
        editor.putBoolean(key, value).apply();
    }

    public boolean getBoolean(@NonNull String key, boolean defaultValue) {
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    // Float operations
    public void saveFloat(@NonNull String key, float value) {
        editor.putFloat(key, value).apply();
    }

    public float getFloat(@NonNull String key, float defaultValue) {
        return sharedPreferences.getFloat(key, defaultValue);
    }

    // Long operations
    public void saveLong(@NonNull String key, long value) {
        editor.putLong(key, value).apply();
    }

    public long getLong(@NonNull String key, long defaultValue) {
        return sharedPreferences.getLong(key, defaultValue);
    }

    // Complex object operations (JSON)
    public <T> void saveObject(@NonNull String key, @NonNull T object) {
        String json = GsonHelper.toJson(object);
        saveString(key, json);
    }

    public <T> T getObject(@NonNull String key, @NonNull Class<T> classOfT, T defaultValue) {
        String json = getString(key, null);
        if (json != null) {
            return GsonHelper.fromJson(json, classOfT);
        }
        return defaultValue;
    }

    // Clear specific key
    public void remove(@NonNull String key) {
        editor.remove(key).apply();
    }

    // Clear all preferences
    public void clearAll() {
        editor.clear().apply();
    }

    // In PreferencesManager.java add these methods:

    public void saveConfigurationJson(String json) {
        editor.putString("raw_json", json).apply();
    }

    public String getConfigurationJson() {
        return sharedPreferences.getString("raw_json", null);
    }

    public boolean hasConfiguration() {
        return sharedPreferences.contains("raw_json");
    }
}