/*Copyright 2011 indiandragon

 This file is part of j2me2AB.

 J2meAB is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 j2me2AB is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with j2me2AB.  If not, see <http://www.gnu.org/licenses/>.
 */
package javax.microedition.lcdui;

import java.util.ArrayList;
import java.util.Iterator;

import android.R;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.text.TextUtils;
import android.text.method.KeyListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ScrollView;

public class List extends Screen implements Choice {

	public static Command SELECT_COMMAND = null;// new Command("OK", Command.OK,
												// 0);
	String title;
	int listType;
	int fitPolicy;
	// boolean bUpdate = true;
	boolean bUpdateSelection = true;
	int iSelectedIndex = -1;
	java.util.List<ListItem> arrayListItems = new ArrayList<ListItem>();

	public List(String title, int listType) {
		SELECT_COMMAND = new Command("OK", Command.OK, 0);
		this.title = title;
		this.listType = listType;

		f = Font.getDefaultFont();
		UpdateView();

	}

	public List(String title, int listType, String[] stringElements,
			Image[] imageElements) {
		SELECT_COMMAND = new Command("OK", Command.OK, 0);
		this.title = title;
		this.listType = listType;
		f = Font.getDefaultFont();
		UpdateView();

		for (int i = 0; i < stringElements.length; i++) {
			ListItem templistitem = new ListItem(stringElements[i],
					imageElements != null ? imageElements[i] : null, false);
			if (arrayListItems.size() == 0) {
				templistitem.bSelected = true;
			}
			templistitem.font = f;
			arrayListItems.add(templistitem);
		}
		UpdateView();
	}

	public void UpdateView() {
		bUpdate = true;
	}

	public void UpdateSelection() {
		bUpdateSelection = true;
	}

	LinearLayout linear = null;
	boolean quicksearch = false;

	public void setQuickSearch(boolean flag) {
		quicksearch = flag;
	}

	public boolean getQuickSearch() {
		return quicksearch;
	}

	AutoCompleteTextView textViews = null;
	TextView textView;
	ListView listView;
	public boolean center = false;

	@Override
	public View getView() {
		// /System.out.println(this.getTitle() + ".getView()");
		if (bUpdate == true) {
			linear = new LinearLayout(UIGlobal.APP_CONTEXT);
			linear.invalidate();

			linear.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT));
			linear.setOrientation(LinearLayout.VERTICAL);
			linear.removeAllViews();
			linear.setBackgroundColor(UIGlobal.BACKGROUND);
			
            
            
			textView = new TextView(UIGlobal.APP_CONTEXT);
			textView.setTypeface(UIGlobal.APP_TYPEFACE, Typeface.BOLD);
			textView.setTextSize(f.getSize());
			textView.setTextColor(UIGlobal.TEXT_NORMAL);
			textView.setText(this.title);
			// /textView.setTypeface(null);
			linear.addView(textView);
			if (quicksearch) {
				String[] count = new String[arrayListItems.size()];
				for (int i = 0; i < arrayListItems.size(); i++) {
					ListItem templistitem =  arrayListItems.get(i);
					count[i] = templistitem.stringPart;
				}
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(
						UIGlobal.APP_CONTEXT,
						android.R.layout.simple_dropdown_item_1line, count);

				textViews = new AutoCompleteTextView(UIGlobal.APP_CONTEXT);
				textViews.setAdapter(adapter);
				textViews.setTypeface(UIGlobal.APP_TYPEFACE, Typeface.BOLD);
				textViews.setTextSize(f.getSize());
				textViews.setTextColor(UIGlobal.TEXT_NORMAL);
				textViews.setThreshold(1);

				textViews.setOnClickListener(new OnClickListener() {
					public void onClick(View vs) {

						// System.out.println(textViews.getText());
						for (int i = 0; i < arrayListItems.size(); i++) {
							ListItem templistitem =  arrayListItems
									.get(i);
							if (templistitem.stringPart
									.equalsIgnoreCase(textViews.getText()
											.toString())) {
								textViews.setText("");
								selectItem(i, true);
							}
						}
					}
				});

				linear.addView(textViews);
			}
			MIDPListAdapter midplistadapter = new MIDPListAdapter(
					arrayListItems);

			midplistadapter.center = center;

			listView = new ListView(UIGlobal.APP_CONTEXT);
			listView.setAdapter(midplistadapter);

			listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			listView.setStackFromBottom(false);
			listView.setClickable(true);
			listView.setDivider(new ColorDrawable(Color.LTGRAY));
			listView.setDividerHeight(3);
			listView.setFooterDividersEnabled(true);
			listView.setHeaderDividersEnabled(true);
			listView.setCacheColorHint(00000000);
			listView.requestFocusFromTouch();

			if (arrayListItems.size() > 0) {

				listView.setSelection(0);
				setSelectedIndex(0, true);
				iSelectedIndex = 0;
			}

			AdapterView.OnItemClickListener mItemClickedHandler = new AdapterView.OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v,
						int position, long id) {
					iSelectedIndex = position;
					selectItem(iSelectedIndex, true);
				}
			};
			listView.setOnItemClickListener(mItemClickedHandler);

			linear.addView(listView);

			// System.out.println("adapter size " + adapter.getCount());
			bUpdate = false;
			// /linear.invalidate();

			linear.setVisibility(VISIBLE);
			linear.requestLayout();

			linear.invalidate();

		}
		//linear.setBackgroundResource(UIGlobal.bg);
		return linear;
	}

	public void RefreshList() {

		// System.out.println(" RefreshList iSelectedIndex " + iSelectedIndex);
		for (int i = 0; i < arrayListItems.size(); i++) {
			ListItem templistitem =  arrayListItems.get(i);
			if (i != iSelectedIndex) {
				if (templistitem.bSelected == true) {
					templistitem.bSelected = false;
					arrayListItems.set(i, templistitem);
				}
			} else {
				if (templistitem.bSelected == false) {
					templistitem.bSelected = true;
					arrayListItems.set(i, templistitem);
				}
			}
		}
	}

	public int append(String stringPart, Image imagePart) {
		ListItem templistitem = new ListItem(stringPart, imagePart, false);
		if (arrayListItems.size() == 0) {
			templistitem.bSelected = true;
		}
		templistitem.font = f;
		arrayListItems.add(templistitem);
		UpdateView();
		return (arrayListItems.size() - 1);
	}

	public void delete(int elementNum) {
		arrayListItems.remove(elementNum);
		UpdateView();
	}

	public void deleteAll() {
		arrayListItems.clear();
		UpdateView();
	}

	public void addCommand(Command cmd) {
		super.addCommand(cmd);
		if (cmd.getLabel().toUpperCase().equals("NEXT")
				|| cmd.getLabel().toUpperCase().equals("SELECT")) // First
		// command
		// is linked
		// to Select
		// Command
		{
			SELECT_COMMAND = cmd;
		}
	}

	public Image getImage(int elementNum) {
		ListItem templistitem =  arrayListItems.get(elementNum);
		return templistitem.imagePart;
	}

	public int getSelectedFlags(boolean[] selectedArray_return) {
		RefreshList();
		int intSelectedCount = 0;
		int i = 0;
		Iterator<ListItem> itr = arrayListItems.iterator();
		while (itr.hasNext()) {
			ListItem templistitem =  itr.next();
			selectedArray_return[i] = templistitem.bSelected;
			if (templistitem.bSelected) {
				intSelectedCount++;
			}
		}
		return intSelectedCount;
	}

	public int getSelectedIndex() {
		RefreshList();
		return iSelectedIndex;
	}

	public String getString(int elementNum) {
		ListItem templistitem =  arrayListItems.get(elementNum);
		return templistitem.stringPart;
	}

	public void insert(int elementNum, String stringPart, Image imagePart) {
		ListItem templistitem = new ListItem(stringPart, imagePart, false);
		templistitem.font = f;
		arrayListItems.add(elementNum, templistitem);
		UpdateView();
	}

	public boolean isSelected(int elementNum) {
		RefreshList();
		ListItem templistitem =  arrayListItems.get(elementNum);
		return templistitem.bSelected;
	}

	public void set(int elementNum, String stringPart, Image imagePart) {
		ListItem templistitem = new ListItem(stringPart, imagePart, false);
		templistitem.font = f;
		arrayListItems.set(elementNum, templistitem);
		UpdateView();
	}

	public void setSelectCommand(Command command) {
		SELECT_COMMAND = command;
	}

	public void setSelectedFlags(boolean[] selectedArray) {
		for (int i = 0; i < arrayListItems.size(); i++) {
			ListItem templistitem =  arrayListItems.get(i);
			if (templistitem.bSelected != selectedArray[i]) {
				templistitem.bSelected = selectedArray[i];
				// templistitem.font = Font.getDefaultFont();
				arrayListItems.set(i, templistitem);
			}
		}
	}

	public void setTitle(String s) {
		this.title = s;
	}

	@Override
	public String getTitle() {
		return this.title;
	}

	public int size() {
		return arrayListItems.size();
	}

	public void selectItem(int elementNum, boolean selected) {
		setSelectedIndex(elementNum, selected);
		if ((this.commandListener != null) && (SELECT_COMMAND != null)) {
			this.commandListener.commandAction(SELECT_COMMAND, this);
		} else if ((this.commandListener != null) && (aListCommands.size() > 1)) {
			this.commandListener.commandAction((Command) aListCommands.get(0),
					this);
		}
	}

	@Override
	public void setSelectedIndex(int elementNum, boolean selected) {

		for (int i = 0; i < arrayListItems.size(); i++) {
			ListItem templistitem =  arrayListItems.get(i);
			if (elementNum == i) {
				templistitem.bSelected = selected;
				// templistitem.font = Font.getDefaultFont();
				arrayListItems.set(i, templistitem);
			} else {
				if (selected == true) {
					if (templistitem.bSelected == true) // Make all other
					// selection false
					{
						templistitem.bSelected = false;
						// templistitem.font = Font.getDefaultFont();
						arrayListItems.set(i, templistitem);
					}
				}
			}
		}
		iSelectedIndex = elementNum;
	}

	public void setFont(int elementNum, Font f) {

		ListItem templistitem =  arrayListItems.get(elementNum);
		templistitem.font = f;
		arrayListItems.set(elementNum, templistitem);

	}

	@Override
	public int getFitPolicy() {
		return fitPolicy;
	}

	@Override
	public void setFitPolicy(int fitPolicy) {
		this.fitPolicy = fitPolicy;
	}

	public void SelectNextItem() {
		int iCurrentlySelectedIndex = iSelectedIndex;
		if (iCurrentlySelectedIndex == (arrayListItems.size() - 1)) {
			setSelectedIndex(0, true);
		} else {
			setSelectedIndex(iCurrentlySelectedIndex + 1, true);
		}
	}

	public void SelectPreviousItem() {
		int iCurrentlySelectedIndex = iSelectedIndex;
		if (iCurrentlySelectedIndex == 0) {
			setSelectedIndex(arrayListItems.size() - 1, true);
		} else {
			setSelectedIndex(iCurrentlySelectedIndex - 1, true);
		}
	}

	@Override
	public Font getFont(int elementNum) {
		return ( arrayListItems.get(elementNum)).font;
	}

	Font f = null;

	public void setFont(Font f) {

		this.f = f;

	}

	public Font getFont() {
		return f;
	}

}
