package com.olmatix.ui.activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

import android.support.v7.app.ActionBar;
import android.util.Log;

import com.olmatix.service.OlmatixService;
import com.olmatix.lesjaw.olmatix.R;
import com.olmatix.ui.fragment.SettingsFragment;

import java.util.List;


/**
 * Created by Lesjaw on 02/12/2016.
 */

public class SettingsActivity extends SettingsFragment {




    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();

                // For all other preferences, set the summary to the value's
                // simple string representation.
            preference.setSummary(stringValue);

            SharedPreferences sharedPref = preference.getSharedPreferences();
            boolean mSwitch_Conn = sharedPref.getBoolean("switch_conn", true);


            if (mSwitch_Conn) {
                Log.d("DEBUG", "SwitchConnPreff: " + mSwitch_Conn);
               // MQTTConnect mStartConnect = new MQTTConnect();
                //mStartConnect.startConnect();
            }
                if (!mSwitch_Conn){
                    Log.d("DEBUG", "SwitchConnPreff: "+ mSwitch_Conn);
                }

            return true;
        }


    };

    private static boolean isXLargeTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
    }

    private static void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

        // Trigger the listener immediately with the preference's
        // current value.
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));



    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupActionBar();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            // Show the Up button in the action bar.
            //actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public boolean onIsMultiPane() {
        return isXLargeTablet(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onBuildHeaders(List<PreferenceActivity.Header> target) {
        loadHeadersFromResource(R.xml.pref_headers, target);
    }



    protected boolean isValidFragment(String fragmentName) {
        return PreferenceFragment.class.getName().equals(fragmentName)
                || NetworkPreferenceFragment.class.getName().equals(fragmentName)
                || AboutPreferenceFragment.class.getName().equals(fragmentName);

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class NetworkPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_network);
            setHasOptionsMenu(true);

            // Bind the summaries of EditText/List/Dialog/Ringtone preferences
            // to their values. When their values change, their summaries are
            // updated to reflect the new value, per the Android Design
            // guidelines.
           // bindPreferenceSummaryToValue(findPreference("server_conn"));
            bindPreferenceSummaryToValue(findPreference("server_address"));
            bindPreferenceSummaryToValue(findPreference("server_port"));
            bindPreferenceSummaryToValue(findPreference("user_name"));
            bindPreferenceSummaryToValue(findPreference("password"));
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class AboutPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //Intent i = new Intent(this,AboutActivity.class);
            //startActivity(i);
        }
    }


}

@@ -438,16 +438,60 @@
 nager;%0A%0A
+import android.preference.SwitchPreference;%0A
 import a
@@ -1437,17 +1437,16 @@
 true);%0A%0A
-%0A
         
@@ -1444,24 +1444,26 @@
 %0A           
+/*
  if (mSwitch
@@ -1555,127 +1555,10 @@
     
-   // MQTTConnect mStartConnect = new MQTTConnect();%0A                //mStartConnect.startConnect();%0A            %7D%0A    
+%7D%0A
     
@@ -1585,20 +1585,16 @@
 _Conn)%7B%0A
-    
         
@@ -1664,21 +1664,19 @@
         
-    %7D
+%7D*/
 %0A%0A      
@@ -2519,19 +2519,16 @@
 , %22%22));%0A
-%0A%0A%0A
     %7D%0A%0A 
@@ -2663,24 +2663,26 @@
 ctionBar();%0A
+%0A%0A
     %7D%0A%0A    @
@@ -3379,17 +3379,16 @@
     %7D%0A%0A%0A
-%0A
     prot
@@ -4645,24 +4645,111 @@
 password%22));
+%0A%0A            SwitchPreference pref = (SwitchPreference) findPreference(%22switch_conn%22);
 %0A        %7D%0A 
@@ -5095,17 +5095,16 @@
 ivity(i)
-;
 %0A       

clean up