/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */

package ti.modules.titanium.ui.widget.searchview;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.util.TiRHelper;
import org.appcelerator.titanium.util.TiRHelper.ResourceNotFoundException;
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.widget.searchbar.TiUISearchBar.OnSearchChangeListener;
import android.support.v7.widget.SearchView;
import android.widget.EditText;

public class TiUISearchView extends TiUIView implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
	private SearchView searchView;

	public static final String TAG = "SearchView";

	protected OnSearchChangeListener searchChangeListener;

	public TiUISearchView(TiViewProxy proxy) {
		super(proxy);

		searchView = new SearchView(proxy.getActivity());
		searchView.setOnQueryTextListener(this);
		searchView.setOnCloseListener(this);
		searchView.setOnQueryTextFocusChangeListener(this);

		setNativeView(searchView);

	}

	@Override
	public void processProperties(KrollDict props) {
		super.processProperties(props);

		// Check if the hint text is specified when the view is created.
		if (props.containsKey(TiC.PROPERTY_HINT_TEXT)) {
			searchView.setQueryHint(props.getString(TiC.PROPERTY_HINT_TEXT));			
		} 
		if (props.containsKey(TiC.PROPERTY_VALUE)) {
			searchView.setQuery(props.getString(TiC.PROPERTY_VALUE), false);			
		} 
		if (props.containsKey(TiC.PROPERTY_ICONIFIED)) {
			searchView.setIconified(props.getBoolean(TiC.PROPERTY_ICONIFIED));
		} 
		if (props.containsKey(TiC.PROPERTY_ICONIFIED_BY_DEFAULT)) {
			searchView.setIconifiedByDefault(props.getBoolean(TiC.PROPERTY_ICONIFIED_BY_DEFAULT));
		}
		if (props.containsKey(TiC.PROPERTY_SUBMIT_ENABLED)) {
			searchView.setSubmitButtonEnabled((props.getBoolean(TiC.PROPERTY_SUBMIT_ENABLED)));			
		}
		if (props.containsKey(TiC.PROPERTY_COLOR)) {
			try {
				int id = TiRHelper.getResource("id.search_src_text");
				EditText text = (EditText) searchView.findViewById(id);
				if (text != null) {
					text.setTextColor(TiConvert.toColor(props, TiC.PROPERTY_COLOR));
				}
			} catch (ResourceNotFoundException e) {
				Log.e(TAG, "Could not find SearchView EditText");
			}
		}
	}

	@Override
	public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy) {

		if (key.equals(TiC.PROPERTY_COLOR)) {
			try {
				int id = TiRHelper.getResource("id.search_src_text");
				EditText text = (EditText) searchView.findViewById(id);
				if (text != null) {
					text.setTextColor(TiConvert.toColor((String) newValue));
				}
			} catch (ResourceNotFoundException e) {
				Log.e(TAG, "Could not find SearchView EditText");
			}
		} else if (key.equals(TiC.PROPERTY_HINT_TEXT)) {
			searchView.setQueryHint((String) newValue);
		} else if (key.equals(TiC.PROPERTY_VALUE)) {
			searchView.setQuery((String) newValue, false);			
		} else if (key.equals(TiC.PROPERTY_ICONIFIED)) {
			searchView.setIconified(TiConvert.toBoolean(newValue));
		} else if (key.equals(TiC.PROPERTY_ICONIFIED_BY_DEFAULT)) {
			searchView.setIconifiedByDefault(TiConvert.toBoolean(newValue));
		} else if  (key.equals(TiC.PROPERTY_SUBMIT_ENABLED)) {
			searchView.setSubmitButtonEnabled(TiConvert.toBoolean(newValue));			
		} else {
			super.propertyChanged(key, oldValue, newValue, proxy);
		}
	}

	@Override
	public boolean onClose() {
		fireEvent(TiC.EVENT_CANCEL, null);
		return false;
	}

	@Override
	public boolean onQueryTextChange(String query) {
		proxy.setProperty(TiC.PROPERTY_VALUE, query);
		if (searchChangeListener != null) {
			searchChangeListener.filterBy(query);
		}
		fireEvent(TiC.EVENT_CHANGE, null);
		return false;
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		TiUIHelper.showSoftKeyboard(nativeView, false);
		fireEvent(TiC.EVENT_SUBMIT, null);
		return false;
	}

	public void setOnSearchChangeListener(OnSearchChangeListener listener) {
		searchChangeListener = listener;
	}



}

@@ -1083,16 +1083,62 @@
 hView;%0A%0A
+%09private boolean changeEventEnabled = false;%0A%0A
 %09public 
@@ -1801,24 +1801,50 @@
 _TEXT));
-%09
+%0A
 %09%09
+%7D
 %0A%09%09
-%7D 
+changeEventEnabled = false;
 %0A%09%09if (p
@@ -1950,24 +1950,49 @@
  false);
-%09
+%0A
 %09%09
+%7D
 %0A%09%09
-%7D 
+changeEventEnabled = true;
 %0A%09%09if (p
@@ -2108,17 +2108,16 @@
 D));%0A%09%09%7D
- 
 %0A%09%09if (p
@@ -2407,19 +2407,16 @@
 BLED)));
-%09%09%09
 %0A%09%09%7D%0A%09%09i
@@ -3458,19 +3458,16 @@
  false);
-%09%09%09
 %0A%09%09%7D els
@@ -3824,19 +3824,16 @@
 Value));
-%09%09%09
 %0A%09%09%7D els
@@ -4190,16 +4190,43 @@
 y);%0A%09%09%7D%0A
+%09%09if (changeEventEnabled)%0A%09
 %09%09fireEv

FIX ANDROID SEARCHVIEW : prevent change event to fire when value prop is set on initialization.