package com.indigitall.cordova.utils;

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

import androidx.annotation.Nullable;

public class CordovaPreferenceUtils {

    private static final String SHARED_PREFERENCES_NAME = "IndigitallCordovaPreferences";
    private static final String SHARED_PREFERENCES_URL_KEY = SHARED_PREFERENCES_NAME + ".getPushKey";
    private static final String SHARED_PREFERENCES_NATIVE_URL_KEY = SHARED_PREFERENCES_NAME + ".getNativePushKey";
    private static final String SHARED_PREFERENCES_PUSH_STRING = SHARED_PREFERENCES_NAME + ".getPushString";
    private static final String SHARED_PREFERENCES_GET_INAPP_ACTION = SHARED_PREFERENCES_NAME + ".getInAppAction";


    private static boolean savePreferences(Context context, Object param, String key) {
        SharedPreferences pref = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = null;
        if (pref != null) editor = pref.edit();
        if (editor != null) {
            if (param instanceof String) {
                return editor.putString (key,(String)param).commit();
            } else if (param instanceof Boolean) {
                return editor.putBoolean(key,(Boolean)param).commit();
            }

        }
        return false;
    }

    private static String getSharedPreferencesString(Context context, String key) {
        if (context != null){
            SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
            if (prefs != null) return prefs.getString(key, null);
        }
        return null;
    }

    private static Boolean getSharedPreferencesBoolean(Context context, String key) {
        if (context != null){
            SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
            if (prefs != null) return prefs.getBoolean(key, false);
        }
        return null;
    }

    public static String getPushUrl(Context context) {
        return getSharedPreferencesString(context, SHARED_PREFERENCES_URL_KEY);
    }

    public static void setPushUrl(Context context, String value) {
        savePreferences(context, value, SHARED_PREFERENCES_URL_KEY);
    }

    public static String getNativePushUrl(Context context) {
        return getSharedPreferencesString(context, SHARED_PREFERENCES_NATIVE_URL_KEY);
    }

    public static void setNativePushUrl(Context context, String value) {
        savePreferences(context, value, SHARED_PREFERENCES_NATIVE_URL_KEY);
    }

    public static String getPushString(Context context) {
        return getSharedPreferencesString(context, SHARED_PREFERENCES_PUSH_STRING);
    }

    public static void setPushString(Context context, String value) {
        savePreferences(context, value, SHARED_PREFERENCES_PUSH_STRING);
    }

    public static boolean getInAppAction(Context context) {
        return getSharedPreferencesBoolean(context, SHARED_PREFERENCES_GET_INAPP_ACTION);
    }

    public static void setInAppAction(Context context, boolean value) {
        savePreferences(context, value, SHARED_PREFERENCES_GET_INAPP_ACTION);
    }

}
