package com.example.datecalc;

import java.util.Iterator;

import javax.microedition.lcdui.*;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
	public static MainActivity ma = null;
	public Display display;
	Menu appmenu;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		ma = this;
		// setContentView(R.layout.activity_main);

		UIGlobal.APP_CONTEXT = this;
		UIGlobal.APP_ACTIVITY = this;
		UIGlobal.APP_HANDLER = new Handler();
		
		UIGlobal.TEXT_NORMAL = Color.BLACK;
		UIGlobal.APP_TYPEFACE = Font.getDefaultFont().typeface;
		
        //UIGlobal.bg= R.drawable.cal2;
        
		 display = new Display();
		 Instances.setDisplay(display);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		// UIGlobal.setActivityOrientation(this,
		// ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		// UIGlobal.setActivityOrientation(this,
		// Configuration.ORIENTATION_PORTRAIT);
		Window win = getWindow();
		win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		android.view.Display displays = ((WindowManager) getSystemService(Activity.WINDOW_SERVICE))
				.getDefaultDisplay();

		UIGlobal.DEVICE_WIDTH = displays.getWidth();
		UIGlobal.DEVICE_HEIGHT = displays.getHeight();

		new dateCalc().startApp();

	}

/*
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

		} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, "sensor", Toast.LENGTH_SHORT).show();
		}
	}
*/
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		try {
			super.onCreateOptionsMenu(menu);
			UIGlobal.APP_MENU = menu;
			this.appmenu = menu;
			updateMenuItems();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean onMenuOpened(int featureId, android.view.Menu menu) {
		this.appmenu = menu;
		UIGlobal.APP_MENU = menu;
		updateMenuItems();
		return true;
	}

	@Override
	public void finish() {

		try {

			Displayable currentdisplayable = Instances.getUIInstance()
				.getCurrent();
			currentdisplayable.arrangeCommands();
			Iterator<Command> itr = currentdisplayable.aListCommands.iterator();


			while (itr.hasNext()) {
				Command cmditem = (Command) itr.next();
				if (cmditem.getCommandType() == Command.BACK) {
					currentdisplayable.commandListener.commandAction(cmditem, currentdisplayable);
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
//    Hashtable com = new Hashtable();

	public void updateMenuItems() {
//        com.clear();
		try {
			int base = Menu.FIRST;
			Displayable currentdisplayable = Instances.getUIInstance()
				.getCurrent();
				//if( currentdisplayable== null) System.out.println(" currentdisplayable is NULL");
			currentdisplayable.arrangeCommands();
			Iterator itr = currentdisplayable.aListCommands.iterator();
			int i = 0;
			if (this.appmenu != null) {

				this.appmenu.clear();
				while (itr.hasNext()) {
					Command cmditem = (Command) itr.next();
					//System.out.println("Command to add " + cmditem.getLabel()
					 //        + " base " + base + " i " + i);
					if (cmditem.getCommandType() != Command.BACK) {
						this.appmenu.add(base, base + i, base + i,
							cmditem.getLabel());
					}
                   //else this.com.put(cmditem.getLabel(), ""+(base+i));
				   
					i++;
				}
			}
		} catch (Exception e) {
		System.out.println(""+e.toString());
			e.printStackTrace();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		try {
			int intMenuItemId = item.getItemId();
			Displayable currentdisplayable = Instances.getUIInstance()
				.getCurrent();
			Command selectedcmditem = null;
			Iterator itr = currentdisplayable.aListCommands.iterator();
			int base = Menu.FIRST;
			int i = 0;
			while (itr.hasNext()) {
				selectedcmditem = (Command) itr.next();
				if (intMenuItemId == (base + i)) {
					break;
				}
				i++;
			}
			if (currentdisplayable.getCommandListener() != null) {
				currentdisplayable.getCommandListener().commandAction(
					selectedcmditem, currentdisplayable);
			} else {
				///System.out.println("Not Handled");
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public void closeApplication() {
		super.onDestroy();
		super.finish();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
