package oko.tm.oko_pro_classic;


import android.app.AlertDialog;
import android.content.DialogInterface;
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.preference.PreferenceScreen;
import android.view.Window;
import android.view.WindowManager;


public class SettingsApp extends PreferenceActivity implements Preference.OnPreferenceChangeListener {

    private String[] parametersListPreference = {"connection", "protocol",  };
    private String[] parametersEditTextPreference = { "server","phone","imei", "alive_timeout",
            "name_tamper","name_input1","name_input2","name_input3","name_input4","name_input5","name_input6","name_input7","name_input8",
            "name_input1_ext0","name_input2_ext0","name_input3_ext0","name_input4_ext0","name_input5_ext0","name_input6_ext0","name_input7_ext0","name_input8_ext0",
            "name_input1_ext1","name_input2_ext1","name_input3_ext1","name_input4_ext1","name_input5_ext1","name_input6_ext1","name_input7_ext1","name_input8_ext1",
            "name_input1_ext2","name_input2_ext2","name_input3_ext2","name_input4_ext2","name_input5_ext2","name_input6_ext2","name_input7_ext2","name_input8_ext2",
            "name_input1_ext3","name_input2_ext3","name_input3_ext3","name_input4_ext3","name_input5_ext3","name_input6_ext3","name_input7_ext3","name_input8_ext3",
            "name_input1_ext4","name_input2_ext4","name_input3_ext4","name_input4_ext4","name_input5_ext4","name_input6_ext4","name_input7_ext4","name_input8_ext4",
            "name_output1","name_output2","name_output3","name_output4","name_output5","name_output6","name_output7","name_output8",
            "name_output1_ext0","name_output2_ext0","name_output3_ext0","name_output4_ext0","name_output5_ext0","name_output6_ext0","name_output7_ext0","name_output8_ext0",
            "name_output1_ext1","name_output2_ext1","name_output3_ext1","name_output4_ext1","name_output5_ext1","name_output6_ext1","name_output7_ext1","name_output8_ext1",
            "name_output1_ext2","name_output2_ext2","name_output3_ext2","name_output4_ext2","name_output5_ext2","name_output6_ext2","name_output7_ext2","name_output8_ext2",
            "name_output1_ext3","name_output2_ext3","name_output3_ext3","name_output4_ext3","name_output5_ext3","name_output6_ext3","name_output7_ext3","name_output8_ext3",
            "name_output1_ext4","name_output2_ext4","name_output3_ext4","name_output4_ext4","name_output5_ext4","name_output6_ext4","name_output7_ext4","name_output8_ext4",
            "name_radio1","name_radio2","name_radio3","name_radio4","name_radio5","name_radio6","name_radio7","name_radio8","name_radio9","name_radio10","name_radio11","name_radio12","name_radio13","name_radio14","name_radio15","name_radio16",
            "name_termo1","name_termo2","name_termo3","name_termo4","name_termo5","name_termo6","name_termo7","name_termo8",
            "name_command1","name_command2","name_command3","name_command4","name_command5","name_command6","name_command7","name_command8",
            "code_command1","code_command2","code_command3","code_command4","code_command5","code_command6","code_command7","code_command8",
            "name_socket1_blk1","name_socket2_blk1","name_socket3_blk1","name_socket4_blk1","name_socket5_blk1","name_socket6_blk1","name_socket7_blk1","name_socket8_blk1",
            "name_socket1_blk2","name_socket2_blk2","name_socket3_blk2","name_socket4_blk2","name_socket5_blk2","name_socket6_blk2","name_socket7_blk2","name_socket8_blk2",
            "name_socket1_blk3","name_socket2_blk3","name_socket3_blk3","name_socket4_blk3","name_socket5_blk3","name_socket6_blk3","name_socket7_blk3","name_socket8_blk3",
            "name_socket1_blk4","name_socket2_blk4","name_socket3_blk4","name_socket4_blk4","name_socket5_blk4","name_socket6_blk4","name_socket7_blk4","name_socket8_blk4",

    };


    //String object_setting=MainActivity.APP_PREFERENCES;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

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

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

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



            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");
                    EditTextPreference alive_timeout = (EditTextPreference) preference.getPreferenceManager().findPreference("alive_timeout");

                    if(newValue.equals("1")){
                        //server.setEnabled(false);
                        alive_timeout.setEnabled(false);
                    }
                    else if(newValue.equals("2")){
                        //server.setEnabled(true);
                        alive_timeout.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());

                    if (obj.getKey().equals("connection")) {
                        EditTextPreference alive_timeout = (EditTextPreference) this.findPreference("alive_timeout");


                        if(obj.getValue().equals("1")){
                            alive_timeout.setEnabled(false);
                        }
                        else if(obj.getValue().equals("2")){
                            alive_timeout.setEnabled(true);
                        }
                        else if(obj.getValue().equals("3")){

                        }
                    }

                    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();
        }
    }

}
