package de.hsosnabrueck.iui.informatik.vma.hipsterbility.activities;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;
import com.loopj.android.http.TextHttpResponseHandler;
import de.hsosnabrueck.iui.informatik.vma.hipsterbility.R;
import de.hsosnabrueck.iui.informatik.vma.hipsterbility.helper.Util;
import de.hsosnabrueck.iui.informatik.vma.hipsterbility.rest.HipsterbilityRestClient;

/**
 * Created by Albert Hoffmann on 26.02.14.
 */
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    SharedPreferences prefs;
    ProgressDialog progressDialog;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
//        TODO: change to non-deprecated Methods
        addPreferencesFromResource(R.xml.settings);
        Preference button = findPreference(getString(R.string.pref_key_button_test_server));
        assert button != null;
        button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference arg0) {
                testServerConnection();
                return true;
            }
        });
        Preference rootFeatures = findPreference(getString(R.string.pref_key_enable_root));
        if(rootFeatures!=null && !Util.isDeviceRooted()){
            rootFeatures.setEnabled(false);
        }
    }

    private void testServerConnection() {
        showProgressDialog();
        HipsterbilityRestClient.get("ping", null, new TextHttpResponseHandler(){
            @Override
            public void onFailure(String responseBody, Throwable error) {
                super.onFailure(responseBody, error);
                showToast(getString(R.string.msg_server_not_found) + ": " + error.getMessage());
            }

            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, String responseBody) {
                super.onSuccess(statusCode, headers, responseBody);
                showToast(getString(R.string.msg_server_found) + " Response from Server: " + statusCode);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                dismissProgressDialog();
            }
        });
    }

    private void showToast(String msg){
        Toast.makeText(this, msg,Toast.LENGTH_LONG).show();
    }

    private void showProgressDialog(){
        progressDialog = new ProgressDialog(this);
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.setMessage(getString(R.string.settings_progress_testing_server));
        progressDialog.show();
    }

    private void dismissProgressDialog(){
        if(progressDialog != null && progressDialog.isShowing())
            progressDialog.dismiss();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        String server, port, timeout, retries, connections;
        server = getString(R.string.pref_key_server);
        

        if (
                key.equals(getString(R.string.pref_key_server)) ||
                key.equals(getString(R.string.pref_key_port))
                ){
            HipsterbilityRestClient.setServer(
                    prefs.getString(getString(R.string.pref_key_server),""),
                    prefs.getInt(getString(R.string.pref_key_port), 3000));
        } else if (
                key.equals(getString(R.string.pref_key_retries)) ||
                key.equals(getString(R.string.pref_key_timeout))
                ){
            HipsterbilityRestClient.setMaxRetriesAndTimeout(
                    prefs.getInt(getString(R.string.pref_key_retries), 0),
                    prefs.getInt(getString(R.string.pref_key_timeout), 300)
            );
        } else if (key.equals(getString(R.string.pref_key_max_connection))){
            HipsterbilityRestClient.setMaxConnections(
                    prefs.getInt(getString(R.string.pref_key_max_connection), 1)
            );
        }
    }
}
@@ -3468,289 +3468,353 @@
     
-%0A%0A        if (%0A                key.equals(getString(R.string.pref_key_server)) %7C%7C%0A                key.equals(getString(R.string.pref_key_port))%0A                )%7B%0A            HipsterbilityRestClient.setServer(%0A                    prefs.getString(getString(R.string.pref_key_
+port = getString(R.string.pref_key_port);%0A        retries = getString(R.string.pref_key_retries);%0A        timeout = getString(R.string.pref_key_timeout);%0A        connections = getString(R.string.pref_key_max_connection);%0A%0A        if ( key.equals(server) %7C%7C key.equals(port) )%7B%0A            HipsterbilityRestClient.setServer(prefs.getString(
 server
-)
 ,%22%22)
@@ -3839,60 +3839,53 @@
     
-prefs.getInt(getString(R.string.pref_key_
+Integer.valueOf(prefs.getString(
 port
-)
 , 
+%22
 3000
+%22)
 ));%0A
@@ -3935,44 +3935,16 @@
 als(
-getString(R.string.pref_key_
 retries)
 ) %7C%7C
@@ -3939,17 +3939,16 @@
 retries)
-)
  %7C%7C%0A    
@@ -3974,44 +3974,16 @@
 als(
-getString(R.string.pref_key_
 timeout)
 )%0A  
@@ -3978,17 +3978,16 @@
 timeout)
-)
 %0A       
@@ -4083,60 +4083,53 @@
     
-prefs.getInt(getString(R.string.pref_key_
+Integer.valueOf(prefs.getString(
 retries
-)
 , 
-0
+%220%22)
 ),%0A 
@@ -4151,62 +4151,55 @@
     
-prefs.getInt(getString(R.string.pref_key_
+Integer.valueOf(prefs.getString(
 timeout
-)
 , 
+%22
 300
+%22)
 )%0A  
@@ -4245,40 +4245,8 @@
 als(
-getString(R.string.pref_key_max_
 conn
@@ -4251,17 +4251,17 @@
 nnection
-)
+s
 ))%7B%0A    
@@ -4335,53 +4335,40 @@
     
-prefs.getInt(getString(R.string.pref_key_max_
+Integer.valueOf(prefs.getString(
 conn
@@ -4377,12 +4377,15 @@
 tion
-), 1
+s, %221%22)
 )%0A  

Fix for crash on settings change