/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2011 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;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import org.appcelerator.titanium.TiActivityWindow;
import org.appcelerator.titanium.TiActivityWindows;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.proxy.TiWindowProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiUIView;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.os.Messenger;


@Kroll.proxy(creatableInModule=UIModule.class)
public class ActivityWindowProxy extends TiWindowProxy
{
	private static final String LCAT = "ActivityWindowProxy";
	private static final boolean DBG = TiConfig.LOGD;
	private static final int MSG_FIRST_ID = TiWindowProxy.MSG_LAST_ID + 1;
	private static final int MSG_FINISH_OPEN = MSG_FIRST_ID + 100;

	protected static final int MSG_LAST_ID = MSG_FIRST_ID + 999;

	protected String windowId;
	protected boolean useCurrentActivity;


	@Override
	public void handleCreationDict(KrollDict options)
	{
		super.handleCreationDict(options);

		Object useCurrentActivity = options.get("useCurrentActivity");
		if (useCurrentActivity != null) {
			this.useCurrentActivity = TiConvert.toBoolean(useCurrentActivity);
		}
	}

	@Override
	protected KrollDict getLangConversionTable()
	{
		KrollDict table = new KrollDict();
		table.put(TiC.PROPERTY_TITLE, TiC.PROPERTY_TITLEID);
		table.put(TiC.PROPERTY_TITLE_PROMPT, TiC.PROPERTY_TITLE_PROMPTID);

		return table;
	}
	

	@Override
	public TiUIView getOrCreateView()
	{
		throw new IllegalStateException("call to getView on an ActivityWindow");
	}

	protected TiUIActivityWindow getWindow()
	{
		return (TiUIActivityWindow) view;
	}

	@Override
	public boolean handleMessage(Message msg)
	{
		switch(msg.what) {
			case MSG_FINISH_OPEN: {
				realizeViews(view);

				if (tab == null) {
					//TODO attach window
				}

				opened = true;
				handlePostOpen();

				return true;
			}
			default : {
				return super.handleMessage(msg);
			}
		}
	}

	@Override
	protected void handleOpen(KrollDict options)
	{
		if (DBG) {
			Log.d(LCAT, "handleOpen");
		}

		Messenger messenger = new Messenger(getMainHandler());

		if (useCurrentActivity) {
			Activity activity = TiApplication.getInstance().getCurrentActivity();
			if (activity instanceof TiBaseActivity) {
				view = new TiUIActivityWindow(this, (TiBaseActivity) activity, messenger, MSG_FINISH_OPEN);
			}

		} else {
			view = new TiUIActivityWindow(this, options, messenger, MSG_FINISH_OPEN);
		}
	}

	public void fillIntentForTab(Intent intent, TabProxy tab)
	{
		intent.putExtra(TiC.INTENT_PROPERTY_USE_ACTIVITY_WINDOW, true);

		int windowId = TiActivityWindows.addWindow(new TiActivityWindow() {
			@Override
			public void windowCreated(TiBaseActivity activity)
			{
				// This is the callback when a window associated with a tab is created.
				// Since TiUIActivityWindow.bindProxies isn't called here, 
				// we call setWindowProxy directly to make sure the activity->window
				// association is correctly initialized.
				activity.setWindowProxy(ActivityWindowProxy.this);
				view = new TiUIActivityWindow(ActivityWindowProxy.this, activity);

				realizeViews(view);
				opened = true;
				fireEvent(TiC.EVENT_OPEN, null, false);
			}
		});

		tab.setWindowId(windowId);
		intent.putExtra(TiC.INTENT_PROPERTY_WINDOW_ID, windowId);
		intent.putExtra(TiC.INTENT_PROPERTY_IS_TAB, true);
	}

	@Override
	protected void handleClose(KrollDict options)
	{
		if (DBG) {
			Log.d(LCAT, "handleClose");
		}

		TiUIActivityWindow window = getWindow();

		if (window != null) {
			window.close(options);
		}
	}

	@Kroll.getProperty @Kroll.method
	public TiViewProxy getTab()
	{
		return tab;
	}

	@Kroll.getProperty @Kroll.method
	public TiViewProxy getTabGroup()
	{
		return tabGroup;
	}

	/*
	@Kroll.setProperty @Kroll.method
	public void setLeftNavButton(ButtonProxy button)
	{
		Log.w(LCAT, "setLeftNavButton not supported in Android");
	}

	@Kroll.method @Kroll.getProperty
	public int getOrientation()
	{
		Activity activity = getActivity();

		if (activity != null)
		{
			return TiOrientationHelper.convertConfigToTiOrientationMode(activity.getResources().getConfiguration().orientation);
		}

		Log.e(LCAT, "unable to get orientation, activity not found for window");
		return TiOrientationHelper.ORIENTATION_UNKNOWN;
	}

	@Kroll.method @Kroll.getProperty
	public int getWindowPixelFormat() 
	{
		int pixelFormat = PixelFormat.UNKNOWN;
		
		if (hasProperty(TiC.PROPERTY_WINDOW_PIXEL_FORMAT)) {
			pixelFormat = TiConvert.toInt(getProperty(TiC.PROPERTY_WINDOW_PIXEL_FORMAT));
		}

		return pixelFormat;
	}

	@Kroll.method @Kroll.setProperty(retain=false)
	public void setWindowPixelFormat(int pixelFormat)
	{
		setProperty(TiC.PROPERTY_WINDOW_PIXEL_FORMAT, pixelFormat, true);
	}
	*/

	@Override
	protected Activity getWindowActivity() 
	{
		TiUIActivityWindow window = getWindow();
		if (window != null) {
			return window.getActivity();
		}

		return null;
	}
}

@@ -447,111 +447,8 @@
 ig;%0A
-import org.appcelerator.titanium.TiActivityWindow;%0Aimport org.appcelerator.titanium.TiActivityWindows;%0A
 impo
@@ -815,39 +815,8 @@
 ty;%0A
-import android.content.Intent;%0A
 impo
@@ -2920,909 +2920,8 @@
 %09%7D%0A%0A
-%09public void fillIntentForTab(Intent intent, TabProxy tab)%0A%09%7B%0A%09%09intent.putExtra(TiC.INTENT_PROPERTY_USE_ACTIVITY_WINDOW, true);%0A%0A%09%09int windowId = TiActivityWindows.addWindow(new TiActivityWindow() %7B%0A%09%09%09@Override%0A%09%09%09public void windowCreated(TiBaseActivity activity)%0A%09%09%09%7B%0A%09%09%09%09// This is the callback when a window associated with a tab is created.%0A%09%09%09%09// Since TiUIActivityWindow.bindProxies isn't called here, %0A%09%09%09%09// we call setWindowProxy directly to make sure the activity-%3Ewindow%0A%09%09%09%09// association is correctly initialized.%0A%09%09%09%09activity.setWindowProxy(ActivityWindowProxy.this);%0A%09%09%09%09view = new TiUIActivityWindow(ActivityWindowProxy.this, activity);%0A%0A%09%09%09%09realizeViews(view);%0A%09%09%09%09opened = true;%0A%09%09%09%09fireEvent(TiC.EVENT_OPEN, null, false);%0A%09%09%09%7D%0A%09%09%7D);%0A%0A%09%09tab.setWindowId(windowId);%0A%09%09intent.putExtra(TiC.INTENT_PROPERTY_WINDOW_ID, windowId);%0A%09%09intent.putExtra(TiC.INTENT_PROPERTY_IS_TAB, true);%0A%09%7D%0A%0A
 %09@Ov

Remove tab specific intent setup code.