package com.cloudsolutions.HMGPatientApp.PermissionManager;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.HandlerThread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;


/**
 * This preferences for app level
 */

public class AppPreferences {

    public static final String PREF_NAME = "PenguinINUI_AppPreferences";
    public static final int MODE = Context.MODE_PRIVATE;

    public static final String campusIdKey = "campusId";

    public static final String LANG = "Lang";

    public static final String settingINFO = "SETTING-INFO";

    public static final String userName = "userName";
    public static final String passWord = "passWord";

    private static HandlerThread handlerThread;
    private static Handler handler;

    static {
        handlerThread = new HandlerThread("PreferencesHandlerThread");
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper());
    }



    public static SharedPreferences getPreferences(final Context context) {
        return context.getSharedPreferences(AppPreferences.PREF_NAME, AppPreferences.MODE);
    }

    public static SharedPreferences.Editor getEditor(final Context context) {
        return getPreferences(context).edit();
    }


    public static void writeInt(final Context context, final String key, final int value) {
        handler.post(() -> {
            SharedPreferences.Editor editor = getEditor(context);
            editor.putInt(key, value);
            editor.apply();
        });
    }


    public static int readInt(final Context context, final String key, final int defValue) {
        Callable<Integer> callable = () -> {
            SharedPreferences preferences = getPreferences(context);
            return preferences.getInt(key, -1);
        };

        Future<Integer> future = new FutureTask<>(callable);
        handler.post((Runnable) future);

        try {
            return future.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace(); // Handle the exception appropriately
        }

        return -1; // Return the default value in case of an error
    }

    public static int getCampusId(final Context context) {
            return  readInt(context,campusIdKey,-1);
    }



    public static void writeString(final Context context, final String key, final String value) {
        handler.post(() -> {
            SharedPreferences.Editor editor = getEditor(context);
            editor.putString(key, value);
            editor.apply();
        });
    }


    public static String readString(final Context context, final String key, final String defValue) {
        Callable<String> callable = () -> {
            SharedPreferences preferences = getPreferences(context);
            return preferences.getString(key, defValue);
        };

        Future<String> future = new FutureTask<>(callable);
        handler.post((Runnable) future);

        try {
            return future.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace(); // Handle the exception appropriately
        }

        return defValue; // Return the default value in case of an error
    }


    public static void writeBoolean(final Context context, final String key, final boolean value) {
        handler.post(() -> {
            SharedPreferences.Editor editor = getEditor(context);
            editor.putBoolean(key, value);
            editor.apply();
        });
    }

    public static boolean readBoolean(final Context context, final String key, final boolean defValue) {
        Callable<Boolean> callable = () -> {
            SharedPreferences preferences = getPreferences(context);
            return preferences.getBoolean(key, defValue);
        };

        Future<Boolean> future = new FutureTask<>(callable);
        handler.post((Runnable) future);

        try {
            return future.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace(); // Handle the exception appropriately
        }

        return defValue; // Return the default value in case of an error
    }
    
}
