/*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.Hashtable;

//import com.example.datecalc.R;



import android.view.View;
import android.widget.LinearLayout.LayoutParams;

public class Display {

	public static final int ALERT = 1;
	public static final int CHOICE_GROUP_ELEMENT = 2;
	public static final int COLOR_BACKGROUND = 0;
	public static final int COLOR_FOREGROUND = 1;
	public static final int COLOR_BORDER = 2;
	public static final int COLOR_HIGHLIGHTED_BACKGROUND = 3;
	public static final int COLOR_HIGHLIGHTED_FOREGROUND = 4;
	public static final int COLOR_HIGHLIGHTED_BORDER = 5;
	public static final int LIST_ELEMENT = 0;
	public Displayable CURRENT_DISPLAYABLE = null;
	public Displayable PREV_DISPLAYABLE = null;
	//LayoutParams layaut = new LayoutParams(LayoutParams.MATCH_PARENT,
	//		LayoutParams.MATCH_PARENT);
	
	public static Display getDisplay(Object dateCalc) {
	   Display display = new Display();
		Instances.setDisplay(display);
		return display;
	}

	public Displayable getCurrent() {
		return CURRENT_DISPLAYABLE;
	}

	public void setCurrent(Alert alert, Displayable nextDisplayable) {
		alert.setNextdisplayable(nextDisplayable);
		alert.ShowAlert();
	}

	public synchronized void setCurrent(Displayable nextDisplayable) {

		if (PREV_DISPLAYABLE != CURRENT_DISPLAYABLE) {
			PREV_DISPLAYABLE = CURRENT_DISPLAYABLE;
		}
		
		CURRENT_DISPLAYABLE = nextDisplayable;
		
		
		final Displayable finalnextdisplayable = nextDisplayable;

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				UIGlobal.APP_HANDLER.post(new Runnable() {
					// @Override
					public void run() {

						finalnextdisplayable.UpdateView();
						
						finalnextdisplayable.invalidate();
						
						//finalnextdisplayable.setBackgroundResource(R.drawable.calendar);
					
						//System.out.println("Background ID "+R.drawable.calendar);
						UIGlobal.APP_ACTIVITY.setContentView(
								finalnextdisplayable.getView());
						
						try {
							Thread.sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				});

			}
		};
		UIGlobal.runOnUiThread(new Thread(runnable));// .start();
		/*
		 * UIGlobal.runOnUiThread(new Thread() {
		 * 
		 * @Override public void run() { finalnextdisplayable.UpdateView();
		 * finalnextdisplayable.invalidate();
		 * UIGlobal.APP_ACTIVITY.setContentView(finalnextdisplayable .getView(),
		 * layaut);
		 * 
		 * try { Thread.sleep(10); } catch (InterruptedException e) {
		 * e.printStackTrace(); } } });
		 */
	}

	View v;

	public synchronized void setCurrentView(View vi) {
		this.v = vi;
		
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				UIGlobal.APP_HANDLER.post(new Runnable() {
					// @Override
					public void run() {
						
						UIGlobal.APP_ACTIVITY.setContentView(v);
						//v.setBackgroundResource(R.drawable.calendar);
						try {
							Thread.sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				});

			}
		};
		UIGlobal.runOnUiThread(new Thread(runnable));// .start();
		/*
		 * UIGlobal.runOnUiThread(new Thread() {
		 * 
		 * @Override public void run() { finalnextdisplayable.UpdateView();
		 * finalnextdisplayable.invalidate();
		 * UIGlobal.APP_ACTIVITY.setContentView(finalnextdisplayable .getView(),
		 * layaut);
		 * 
		 * try { Thread.sleep(10); } catch (InterruptedException e) {
		 * e.printStackTrace(); } } });
		 */
	}

	public synchronized void setBackScreen() {
		CURRENT_DISPLAYABLE = PREV_DISPLAYABLE;
		final Displayable finalnextdisplayable = PREV_DISPLAYABLE;

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				UIGlobal.APP_HANDLER.post(new Runnable() {
					// @Override
					public void run() {

						finalnextdisplayable.UpdateView();
						finalnextdisplayable.invalidate();
						//finalnextdisplayable.setBackgroundResource(R.drawable.calendar);
						UIGlobal.APP_ACTIVITY.setContentView(
								finalnextdisplayable.getView());

						try {
							Thread.sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				});

			}
		};
		UIGlobal.runOnUiThread(new Thread(runnable));// .start();
		/*
		 * UIGlobal.runOnUiThread(new Thread() {
		 * 
		 * @Override public void run() { finalnextdisplayable.UpdateView();
		 * finalnextdisplayable.invalidate();
		 * UIGlobal.APP_ACTIVITY.setContentView(finalnextdisplayable .getView(),
		 * layaut);
		 * 
		 * try { Thread.sleep(10); } catch (InterruptedException e) {
		 * e.printStackTrace(); } } });
		 */
	}

	public void refreshCurrentScreen() {

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				UIGlobal.APP_HANDLER.post(new Runnable() {
					// @Override
					public void run() {

						CURRENT_DISPLAYABLE.UpdateView();
						CURRENT_DISPLAYABLE.invalidate();
						//CURRENT_DISPLAYABLE.setBackgroundResource(R.drawable.calendar);
						UIGlobal.APP_ACTIVITY.setContentView(
								CURRENT_DISPLAYABLE.getView());

						try {
							Thread.sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				});

			}
		};
		UIGlobal.runOnUiThread(new Thread(runnable));// .start();
		/*
		 * 
		 * 
		 * UIGlobal.runOnUiThread(new Thread() {
		 * 
		 * @Override public void run() { CURRENT_DISPLAYABLE.UpdateView();
		 * CURRENT_DISPLAYABLE.invalidate();
		 * UIGlobal.APP_ACTIVITY.setContentView(CURRENT_DISPLAYABLE .getView(),
		 * layaut);
		 * 
		 * try { Thread.sleep(10); } catch (InterruptedException e) {
		 * e.printStackTrace(); } } });
		 */
	}
}
