package com.example.aperture.core;

import android.app.Activity;
import android.app.ListActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.os.Bundle;

import android.provider.MediaStore;

import android.speech.RecognizerIntent;

import android.text.InputType;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.ToggleButton;

import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;


public class AerialFaithActivity extends ListActivity {

    private final static int REQUEST_SPEECH = 1;
    private final static int REQUEST_IMAGE = 2;
    private final static int REQUEST_VIDEO = 4;


    private Set<ComponentName> mModules = new TreeSet<ComponentName>();

    private RelativeLayout header = null;
    private SearchView polybox = null;


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

        loadModuleListFromPreferences();
        initializeHeaderView();

        // XXX get rid of this once the response intents are implemented
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1,
                new String[] {}));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu m) {
        getMenuInflater().inflate(R.menu.main, m);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem mi) {
        if(mi.getItemId() == R.id.resolve_stalemates) {
            Intent resolveStalematesIntent = new Intent(
                    this, StalemateResolutionActivity.class);
            startActivity(resolveStalematesIntent);
            return true;
        }
        return super.onOptionsItemSelected(mi);
    }


    @Override
    protected void onActivityResult(int request, int response, Intent data) {
        if(response != Activity.RESULT_OK) {
            return;
        }

        if(request == REQUEST_SPEECH) {
            ArrayList<String> results = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            if(results == null || results.size() == 0) {
                return;
            }
            String bestMatch = results.get(0);
            polybox.setQuery(bestMatch, false);
        }
        else if(request == REQUEST_IMAGE) {
            android.widget.Toast.makeText(this, "NotImplemented (img)",
            android.widget.Toast.LENGTH_SHORT).show();
        }
        else if(request == REQUEST_VIDEO) {
            android.widget.Toast.makeText(this, "NotImplemented (vid)",
            android.widget.Toast.LENGTH_SHORT).show();
        }
    }


    private void loadModuleListFromPreferences() {
/*
        // Deprecated after finding out about PreferenceActivity.
        SharedPreferences prefs = getSharedPreferences(
                ModuleManagement.PREFERENCES_NAME,
                Context.MODE_PRIVATE);

        Set<String> enabledModules = prefs.getStringSet(
                ModuleManagement.ENABLED_MODULES,
                new TreeSet<String>());

        mModules.clear();
        for(String name: enabledModules) {
            mModules.add(ComponentName.unflattenFromString(name));
        }
//*/
    }


    private void initializeHeaderView() {
        header = (RelativeLayout)getLayoutInflater().inflate(
                R.layout.main, getListView(), false);
        getListView().addHeaderView(header);

        polybox = (SearchView)header.findViewById(android.R.id.text1);
        polybox.setInputType(InputType.TYPE_CLASS_TEXT |
                InputType.TYPE_TEXT_VARIATION_URI);

        // @android:id/button1
        // @android:id/button2
        // keyboard type
        RadioGroup grp = (RadioGroup)header.findViewById(R.id.text1_inputtype);
        grp.setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup rg, int id) {
                if(id == android.R.id.button1) {
                    AerialFaithActivity.this.polybox.setInputType(
                            InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_URI);
                }
                else if(id == android.R.id.button2) {
                    AerialFaithActivity.this.polybox.setInputType(
                            InputType.TYPE_CLASS_PHONE);
                }
            }
        });

        // @id/button3 : speech recognition
        Button b3 = (Button)header.findViewById(R.id.button3);
        b3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent speechIntent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                startActivityForResult(speechIntent, REQUEST_SPEECH);
            }
        });

        // @id/button4 : image
        Button b4 = (Button)header.findViewById(R.id.button4);
        b4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent cameraIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, REQUEST_IMAGE);
            }
        });

        // @id/button5 : video
        Button b5 = (Button)header.findViewById(R.id.button5);
        b5.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent cameraIntent = new Intent(
                        MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(cameraIntent, REQUEST_VIDEO);
            }
        });
    }

}

@@ -259,24 +259,70 @@
 os.Bundle;%0A%0A
+import android.preference.PreferenceManager;%0A%0A
 import andro
@@ -1339,49 +1339,8 @@
 );%0A%0A
-        loadModuleListFromPreferences();%0A
     
@@ -1371,241 +1371,126 @@
 ();%0A
-%0A
     
-    // XXX get rid of this once the response intents are implemented%0A        setListAdapter(new ArrayAdapter%3CString%3E(this,%0A                android.R.layout.simple_list_item_1, android.R.id.text1,%0A                new String%5B%5D %7B%7D)
+%7D%0A%0A%0A    @Override%0A    protected void onResume() %7B%0A        super.onResume();%0A        loadModuleListFromPreferences(
 );%0A 
@@ -2983,224 +2983,111 @@
 ) %7B%0A
-/*%0A        // Deprecated after finding out about PreferenceActivity.%0A        SharedPreferences prefs = getSharedPreferences(%0A                ModuleManagement.PREFERENCES_NAME,%0A                Context.MODE_PRIVATE
+        SharedPreferences prefs =%0A                PreferenceManager.getDefaultSharedPreferences(this
 );%0A
-%0A
     
@@ -3102,24 +3102,17 @@
 String%3E 
-enabledM
+m
 odules =
@@ -3169,15 +3169,16 @@
 ent.
-ENABLED
+LAST_ALL
 _MOD
@@ -3182,32 +3182,16 @@
 MODULES,
-%0A               
  new Tre
@@ -3207,25 +3207,24 @@
 ng%3E());%0A
-%0A
         
 mModules
@@ -3219,22 +3219,60 @@
     
-mModules.clear
+ArrayList%3CString%3E components = new ArrayList%3CString%3E
 ();%0A
@@ -3300,16 +3300,9 @@
 me: 
-enabledM
+m
 odul
@@ -3323,77 +3323,262 @@
     
-mModules.add(ComponentName.unflattenFromString(name));%0A        %7D%0A//*/
+if(prefs.getBoolean(name, false))%0A                components.add(name);%0A        %7D%0A        setListAdapter(new ArrayAdapter%3CString%3E(this,%0A                android.R.layout.simple_list_item_1,%0A                android.R.id.text1,%0A                components));
 %0A   

Make main activity respond to changes in module preferences.