package com.wasu.cs.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wasu.widgets.focuswidget.CustomGridView;

import cn.com.wasu.main.R;

public class SearchInputLayout extends LinearLayout implements View.OnFocusChangeListener{
	
	private final int LIMIT_LENGTH = 15;

	/**
	 * search keyboard
	 */
	private final String[] CONTENT = new String[] {"A", "B", "C", "D", "E", "F",
												   "G", "H", "I", "J", "K", "L",
												   "M", "N", "O", "P", "Q", "R",
												   "S", "T", "U", "V", "W", "X",
												   "Y", "Z", "1", "2", "3", "4",
												   "5", "6", "7", "8", "9", "0",
												   };
	
	private EditText mSearchET;
	private CustomGridView mSearchGv;
	private ImageView del_iv;
	private ImageView del_all_iv;
	private Button del_tv;
	private Button del_all_tv;
	
	private ISearchInputCallback mCallback;

	public SearchInputLayout(Context context) {
		super(context);
		initLayout(context);
	}

	public SearchInputLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initLayout(context);
	}

	public SearchInputLayout(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initLayout(context);
	}

	private void initLayout(Context context) {
		setOrientation(VERTICAL);

		LayoutInflater.from(context).inflate(R.layout.layout_search_input, this, true);

		mSearchET = (EditText) findViewById(R.id.search_input_et);
		del_iv = (ImageView) findViewById(R.id.del_iv);
		del_all_iv = (ImageView) findViewById(R.id.del_all_iv);
		del_tv = (Button) findViewById(R.id.search_input_delete_btn);
		del_all_tv = (Button) findViewById(R.id.search_input_clean_btn);

		del_tv.setOnFocusChangeListener(this);
		del_all_tv.setOnFocusChangeListener(this);
		mSearchET.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if (null != mCallback && s.toString().length() <= LIMIT_LENGTH) {
					mCallback.onSearch(s.toString());
				}
			}
		});

		OnClickListener btnClickListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.search_input_clean_btn:
					mSearchET.setText("");
					break;

				/*case R.id.search_input_space_btn:
					mSearchET.setText(mSearchET.getText().toString() + " ");
					break;*/

				case R.id.search_input_delete_btn:
					String s = mSearchET.getText().toString();
					if (!TextUtils.isEmpty(s)) {
						mSearchET.setText(s.substring(0, s.length() - 1));
					}
					break;

				default:
					break;
				}
			}
		};

		Button cleanBtn = (Button) findViewById(R.id.search_input_clean_btn);
		cleanBtn.setOnClickListener(btnClickListener);
		//Button spaceBtn = (Button) findViewById(R.id.search_input_space_btn);
		//spaceBtn.setOnClickListener(btnClickListener);
		Button deleteBtn = (Button) findViewById(R.id.search_input_delete_btn);
		deleteBtn.setOnClickListener(btnClickListener);

		mSearchGv = (CustomGridView) findViewById(R.id.search_input_gv);
		// ...
		mSearchGv.setNextFocusRightId(R.id.search_result_fgv);
		initGridView(mSearchGv);
	}
	int select_position=-1;
	private void initGridView(final GridView gridView) {
		
		final GridAdapter adapter = new GridAdapter();
		adapter.notifyDataSetChanged();

		gridView.setNumColumns(6);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
//				mSearchET.setText(mSearchET.getText().toString() + (String) adapter.getItem(position));
				setSearchText((String) adapter.getItem(position));
			}
		});
		gridView.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!hasFocus){
					if(gridView.getSelectedView()!=null){//规避个别机器有选中状态，并且选中状态不能取消
						TextView view=(TextView)gridView.getSelectedView();
						view.setTextColor(Color.WHITE);
						select_position=gridView.getSelectedItemPosition();
					}
				}else{
					initGridView(gridView);//规避焦点回来后字体颜色为白色
				}
			}
		});
		gridView.requestFocus();
		if(select_position!=-1){
			gridView.setSelection(select_position);
		}
	}
	
	private void setSearchText(String s) {
		String text = mSearchET.getText().toString();
		int length = text.length();
		if (length > (LIMIT_LENGTH - 1)) {
			text = text.substring(0, LIMIT_LENGTH) + "...";
		} else {
			text += s;
		}
		mSearchET.setText(text);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	//	/**
//	 * set text watcher to watch text change
//	 * @param watcher
//	 */
//	public void addTextChangedListener(TextWatcher watcher) {
//		if (null == watcher) {
//			return;
//		}
//		mSearchET.addTextChangedListener(watcher);
//	}
	public void setCallback(ISearchInputCallback callback) {
		mCallback = callback;
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		switch (v.getId()) {
			case R.id.search_input_clean_btn:
				if(hasFocus){
					del_all_iv.setImageResource(R.drawable.del_all_yellow);
				}else {
					del_all_iv.setImageResource(R.drawable.del_all_white);
				}
				break;


			case R.id.search_input_delete_btn:
				if(hasFocus){
					del_iv.setImageResource(R.drawable.del_yellow);
				}else {
					del_iv.setImageResource(R.drawable.del_white);
				}
				break;

			default:
				break;
		}
	}

	public void delBtnRequestFocus(){
		del_tv.requestFocus();
	}

	public CustomGridView getSearchGridView() {
		return this.mSearchGv;
	}

	class GridAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return CONTENT.length;
		}

		@Override
		public Object getItem(int position) {
			return CONTENT[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				if(position>25){
					convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_search_input_num, parent, false);
				}else {
					convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_search_input, parent, false);
				}
				holder = new ViewHolder();
				convertView.setTag(holder);
				holder.mTv = (TextView) convertView.findViewById(R.id.item_search_input_tv);


			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.mTv.setText(CONTENT[position]);

			return convertView;
		}
	}

	class ViewHolder {
		TextView mTv;
	}

	public static interface ISearchInputCallback {
		public abstract void onSearch(String s);
	}

}
