package oko.tm.oko_pro_classic;


import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.Window;

public class SettingsObj extends PreferenceActivity implements Preference.OnPreferenceChangeListener {

    private String[] parametersListPreference = {"connection_", };
    private String[] parametersEditTextPreference = { "port", "server","object_name1","object_name2","object_name3","object_name4","object_name5",
            "object_name6","object_name7","object_name8","object_name9","object_name10",
    };


    String object_setting=StartActivity.START_PREFERENCES;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

        super.onCreate(savedInstanceState);

        //getActionBar().setDisplayHomeAsUpEnabled(true);

        try {
            Intent intent = getIntent();
            //object setting
            object_setting = intent.getStringExtra(StartActivity.OBJECT_SETTING);

            PreferenceManager manager = getPreferenceManager();
            manager.setSharedPreferencesName(object_setting);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preference_start);



            for (int i = 0; i < parametersListPreference.length; i++)
                getObjPref("ListPreference", parametersListPreference[i]);

            for (int i = 0; i < parametersEditTextPreference.length; i++)
                getObjPref("EditTextPreference", parametersEditTextPreference[i]);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public boolean onPreferenceChange(Preference preference,  Object newValue) {
        try {

            if (preference.getClass().getName().equals("android.preference.ListPreference")) {
                ListPreference lp = (ListPreference) preference.getPreferenceManager().findPreference(preference.getKey());
                preference.setSummary(lp.getEntries()[Integer.parseInt(newValue.toString()) - 1]);


                if (preference.getKey().equals("connection")) {
                    EditTextPreference server= (EditTextPreference) preference.getPreferenceManager().findPreference("server");

                    if(newValue.equals("1")){
                        server.setEnabled(false);
                    }
                    else if(newValue.equals("2")){
                        server.setEnabled(true);
                    }
                    else if(newValue.equals("3")){

                    }
                }

            } else {

                String PrefKey = preference.getKey();


                preference.setSummary((CharSequence) newValue);

            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;


    }

    protected void getObjPref( String type,  String PrefKey) {
        String val;
        try {

            if (type.equals("ListPreference"))
            {
                ListPreference obj = (ListPreference) this.findPreference(PrefKey);
                if (obj != null) {
                    if (obj.getEntry() != null)
                        obj.setSummary(obj.getEntry());

                    obj.setOnPreferenceChangeListener(this);
                }
            } else if (type.equals("EditTextPreference")) {
                EditTextPreference obj = (EditTextPreference) this.findPreference(PrefKey);
                if (obj != null) {
                    val = obj.getText();

                    obj.setSummary(val);
                    obj.setOnPreferenceChangeListener(this);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

