/*
 * Copyright (C) 2013 Ronald Taeza
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gmail.taneza.ronald.carbs;

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Loader;
import android.database.Cursor;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;
import android.app.LoaderManager;
//todo
//import android.support.v7.app.ActionBar;

import com.commonsware.cwac.loaderex.SQLiteCursorLoader;
import com.gmail.taneza.ronald.carbs.FoodDbAdapter.Language;

import org.droidparts.widget.ClearableEditText;

// todo: save the language setting
// todo: show current language setting as icon on action bar (NL / EN)
// todo: when clicking language setting, show only non-active item

public class FoodListActivity extends ListActivity 
    implements LoaderManager.LoaderCallbacks<Cursor> {

	private FoodDbAdapter mDbHelper;
    private SimpleCursorAdapter mAdapter;
	private ClearableEditText mSearchEditText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.food_list);
		
        mDbHelper = new FoodDbAdapter(this, FoodDbAdapter.Language.DUTCH);
        mDbHelper.open();
        
        addSearchTextListener();

        initListAdapter();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle presses on the action bar items
	    switch (item.getItemId()) {
	        case R.id.menu_language_english:	        	
	        	mDbHelper.setLanguage(Language.ENGLISH);
	        	initListAdapter();
	            return true;
	        case R.id.menu_language_dutch:
	        	mDbHelper.setLanguage(Language.DUTCH);
	        	initListAdapter();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		String queryString = mDbHelper.getFoodNameQueryString(mSearchEditText.getText().toString().trim());		
		return new SQLiteCursorLoader(this, mDbHelper, queryString, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		// Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		// This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
        mAdapter.swapCursor(null);		
	}
	
	private void initListAdapter() {
        String[] from = mDbHelper.getColumnStringArray();
        int[] to = new int[] { R.id.name, R.id.carbs_amount};
        
        // Create an empty adapter we will use to display the loaded data.
        mAdapter = new SimpleCursorAdapter(this,
        		R.layout.food_row, null, from, to, 0);
        setListAdapter(mAdapter);
        
        initLoader();
	}
	
	private void initLoader() {
		getLoaderManager().restartLoader(0, null, this);
	}
	
	private void addSearchTextListener() {
		mSearchEditText = (ClearableEditText) findViewById(R.id.search_text);
		mSearchEditText.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
				// Abstract Method of TextWatcher Interface.
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// Abstract Method of TextWatcher Interface.
			}

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				initLoader();
			}
		});
	}
}

@@ -1175,21 +1175,24 @@
 xt;%0A%0A// 
-todo:
+TODOS%0A//
  save th
@@ -1212,22 +1212,16 @@
 tting%0A//
- todo:
  show cu
@@ -1282,71 +1282,52 @@
 %0A// 
-todo: when clicking language setting, show only non-active item
+rename activity_main.xml to main_actions.xml
 %0A%0Apu
@@ -2147,24 +2147,51 @@
 tem item) %7B%0A
+%09%09Language newLanguage;%0A%09%09%0A
 %09    // Hand
@@ -2299,26 +2299,16 @@
 english:
-%09        %09
 %0A%09      
@@ -2310,37 +2310,27 @@
        %09
-mDbHelper.set
+new
 Language
 (Languag
@@ -2313,33 +2313,35 @@
     %09newLanguage
-(
+ = 
 Language.ENGLISH
@@ -2340,17 +2340,16 @@
 .ENGLISH
-)
 ;%0A%09     
@@ -2356,51 +2356,13 @@
    %09
-initListAdapter();%0A%09            return true
+break
 ;%0A%09 
@@ -2413,29 +2413,19 @@
    %09
-mDbHelper.set
+new
 Language
 (Lan
@@ -2420,17 +2420,19 @@
 Language
-(
+ = 
 Language
@@ -2437,17 +2437,16 @@
 ge.DUTCH
-)
 ;%0A%09     
@@ -2453,51 +2453,13 @@
    %09
-initListAdapter();%0A%09            return true
+break
 ;%0A%09 
@@ -2536,16 +2536,180 @@
 %0A%09    %7D%0A
+%09    %0A%09    if (mDbHelper.getLanguage() != newLanguage) %7B%0A        %09mDbHelper.setLanguage(newLanguage);%0A        %09initListAdapter();%0A%09    %7D%0A%09    %0A        return true;%0A
 %09%7D%0A%09%0A%09@O
@@ -3846,19 +3846,22 @@
         
-ini
+restar
 tLoader(
@@ -3882,19 +3882,22 @@
 te void 
-ini
+restar
 tLoader(
@@ -4459,19 +4459,22 @@
 ) %7B%0A%09%09%09%09
-ini
+restar
 tLoader(

Set language only if the user selects a different one.