package com.artursworld.nccn.controller.util;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;

import com.artursworld.nccn.controller.config.App;

import java.util.Set;

public class Share {

    private static String CLASS_NAME = Share.class.getSimpleName();

    /**
     * Reads value by key from shared preferences
     *
     * @param key the key to check
     * @return the value which stands for the key
     */
    public static String getStringByKey(String key) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(App.getAppContext());
        String restoredText = prefs.getString(key, null);
        //String logMessage = "get global value by key(key=" + key + ", value=" + restoredText + ")";
        //Log.i(CLASS_NAME, logMessage);
        return restoredText;
    }

    /**
     * Reads value from shared preferences by resourceId
     *
     * @param keyId the resource key ID
     * @return the value which stands for the key
     */
    public static String getStringByKey(int keyId) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(App.getAppContext());
        String key = App.getAppContext().getResources().getString(keyId);
        String restoredText = prefs.getString(key, null);
        String logMessage = "get global value by keyId(=" + key + ", value=" + restoredText + ")";
        //Log.i(CLASS_NAME, logMessage);
        return restoredText;
    }

    /**
     * Reads value from shared preferences by resourceId
     *
     * @param keyId the resource key ID
     * @return the value which stands for the key
     */
    public static Set<String> getStringSetByKey(int keyId) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(App.getAppContext());
        String key = App.getAppContext().getResources().getString(keyId);
        Set<String> restoredText = prefs.getStringSet(key, null);
        String logMessage = "get global value by keyId(=" + key + ", value=" + restoredText + ")";
        //Log.i(CLASS_NAME, logMessage);
        return restoredText;
    }

    /**
     * Writes key-value pair into shared preferences
     *
     * @param key the key
     * @param value the value of type string
     * @return True if the function (put key-value pair into shared preferances) was succesful). Otherwise false
     */
    public static boolean putString(final String key, final String value) {
        boolean isFunctionSuccesful = false;
        SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(App.getAppContext()).edit();
        if (key != null && value != null) {
            prefs.putString(key, value);
            prefs.commit();
            isFunctionSuccesful = true;
        }
        return isFunctionSuccesful;
    }

    /**
     * Writes value into shared preferences
     *
     * @param key
     * @param value
     */
    public static void putBoolean(final String key, final boolean value) {
        AsyncTask a = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... unusedParams) {
                SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(App.getAppContext()).edit();
                if (key != null) {
                    prefs.putBoolean(key, value);
                    prefs.commit();
                    //String logMessage = "set global value(key=" + key + ", value=" + value + ")";
                    //Log.i(CLASS_NAME, logMessage);
                }
                return null;
            }
        }.execute();

        try {
            a.get();
        }
        catch (Exception e){
            String logMessage = "could not set global value(key=" + key + ", value=" + value + ") ";
            Log.e(CLASS_NAME, logMessage + e.getLocalizedMessage());
        }
    }

    /**
     * Writes value into shared preferences
     *
     * @param key
     * @param value
     */
    public static void putStringSet(final String key, final Set<String> value) {
        AsyncTask a = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... unusedParams) {
                SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(App.getAppContext()).edit();
                if (key != null && value != null) {
                    prefs.putStringSet(key, value);
                    prefs.commit();
                    prefs.apply();
                    //String logMessage = "set global value(key=" + key + ", value=" + value + ")";
                    //Log.i(CLASS_NAME, logMessage);
                }
                return null;
            }
        }.execute();

        try {
            a.get();
        }
        catch (Exception e){
            String logMessage = "could not set global value(key=" + key + ", value=" + value + ") ";
            Log.e(CLASS_NAME, logMessage + e.getLocalizedMessage());
        }
    }

    public static boolean getBooleanByKey(int keyId) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(App.getAppContext());
        String key = App.getAppContext().getResources().getString(keyId);
        boolean retBool = prefs.getBoolean(key, false);
        return retBool;
    }
}

@@ -2263,24 +2263,26 @@
  @param key 
+  
 the key%0A    
@@ -2785,32 +2785,59 @@
 prefs.commit();%0A
+            prefs.apply();%0A
             isFu
@@ -3105,150 +3105,14 @@
     
-AsyncTask a = new AsyncTask%3CVoid, Void, Void%3E() %7B%0A            @Override%0A            protected Void doInBackground(Void... unusedParams) %7B%0A    
+try %7B%0A
     
@@ -3219,36 +3219,32 @@
 text()).edit();%0A
-    
             if (
@@ -3266,36 +3266,32 @@
                 
-    
 prefs.putBoolean
@@ -3312,36 +3312,32 @@
                 
-    
 prefs.commit();%0A
@@ -3332,32 +3332,59 @@
 s.commit();%0A    
+            prefs.apply();%0A
                 
@@ -3471,36 +3471,32 @@
                 
- 
-   
 //Log.i(CLASS_NA
@@ -3528,131 +3528,20 @@
     
-    %7D%0A                return null;%0A            %7D%0A        %7D.execute();%0A%0A        try %7B%0A            a.get();%0A        %7D%0A       
+%7D%0A%0A        %7D
  cat
@@ -3548,32 +3548,33 @@
 ch (Exception e)
+ 
 %7B%0A            St
@@ -3944,150 +3944,14 @@
     
-AsyncTask a = new AsyncTask%3CVoid, Void, Void%3E() %7B%0A            @Override%0A            protected Void doInBackground(Void... unusedParams) %7B%0A    
+try %7B%0A
     
@@ -4058,36 +4058,32 @@
 text()).edit();%0A
-    
             if (
@@ -4122,36 +4122,32 @@
                 
-    
 prefs.putStringS
@@ -4170,36 +4170,32 @@
                 
-    
 prefs.commit();%0A
@@ -4202,36 +4202,32 @@
                 
- 
-   
 prefs.apply();%0A 
@@ -4217,36 +4217,32 @@
  prefs.apply();%0A
-    
                 
@@ -4329,36 +4329,32 @@
                 
-    
 //Log.i(CLASS_NA
@@ -4386,122 +4386,8 @@
     
-    %7D%0A                return null;%0A            %7D%0A        %7D.execute();%0A%0A        try %7B%0A            a.get();%0A        
 %7D%0A  
@@ -4391,16 +4391,18 @@
 %0A       
+ %7D
  catch (
@@ -4413,16 +4413,17 @@
 ption e)
+ 
 %7B%0A      

remove useless async stuff