package lan.my.zooty.andyscan;

import android.preference.PreferenceActivity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.ListPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
import android.content.ActivityNotFoundException;

public class AndyScanPrefsActivity
   extends PreferenceActivity 
      implements OnSharedPreferenceChangeListener {

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

      addPreferencesFromResource(R.xml.prefs);
      Preference localDir = (Preference)findPreference("local_dir_edit_key");

      localDir.setOnPreferenceClickListener(
         new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
               Intent intent =
                  new Intent("org.openintents.action.PICK_DIRECTORY");
               try {
                  startActivityForResult(intent, 1);
               } catch (ActivityNotFoundException e) {
                  doToast(R.string.no_filemanager_installed);
               }
               return true;
            }
         });

      initSummaries(getPreferenceScreen());
      getPreferenceScreen().getSharedPreferences().
         registerOnSharedPreferenceChangeListener(this);
   }

   void
   doToast(int resId) {
      Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)
   {
      if (resultCode == RESULT_OK && data != null) {
         Uri dirUri = data.getData();
         if (dirUri != null) {
            String dirPath = dirUri.getPath();
            if (dirPath != null) {
               SharedPreferences.Editor ed = getPreferenceScreen().getEditor();
               ed.putString("local_dir_edit_key", dirPath);
               ed.commit();
            }
         }
      }
   }

   private void initSummaries(PreferenceGroup pg) {
      for (int i = 0; i < pg.getPreferenceCount(); ++i) {
         Preference p = pg.getPreference(i);
         if (p instanceof PreferenceGroup) {
            initSummaries((PreferenceGroup) p); // recursion
         } else {
            setSummary(p);
         }
      }
   }

   private void setSummary(Preference pref) {
      if (pref instanceof ListPreference) {
         ListPreference listPref = (ListPreference) pref;
         pref.setSummary(listPref.getEntry());
      } else if (pref instanceof EditTextPreference) {
         EditTextPreference editPref = (EditTextPreference) pref;
         pref.setSummary(editPref.getText());
      } else if (pref.getKey().equals("local_dir_edit_key")) {
         pref.setSummary(
            pref.getSharedPreferences().getString("local_dir_edit_key", ""));
      }
   }

   public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
      Preference pref = findPreference(key);
      setSummary(pref);
   }
}
