package org.appcelerator.titanium.proxy;

import java.lang.ref.WeakReference;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.view.TiUIView;

import android.app.Activity;

/**
 * This class exists to allow JS wrapping of the abstract methods
 */
@Kroll.proxy
public class TiBaseWindowProxy extends TiWindowProxy
{
	private static final String TAG = "TiBaseWindow";
	
	private WeakReference<TiBaseActivity> hostActivity;

	/**
	 * Called to associate a view with a JS window wrapper 
	 * 
	 * @param viewProxy			real view that the JS wrapper represents
	 */
	@Kroll.method
	public void setWindowView(TiViewProxy viewProxy) {
		TiUIView view = viewProxy.peekView();
		setView(view);
		setModelListener(view);
	}
	
	@Kroll.method
	public void addSelfToStack() 
	{
		// adding window to stack
		Activity topActivity = TiApplication.getAppCurrentActivity();
		if (topActivity instanceof TiBaseActivity) {
			hostActivity = new WeakReference<TiBaseActivity>((TiBaseActivity) topActivity);
			((TiBaseActivity)topActivity).addWindowToStack(this);
		}
	}
	
	@Kroll.method
	public void removeSelfFromStack() 
	{
		// removing window from stack
		TiBaseActivity activity = (hostActivity != null) ? hostActivity.get() : null;
		if (activity != null) {
			activity.removeWindowFromStack(this);
		}
	}

	/**
	 * Returns the view that is wrapped by this object.  The caller is
	 * expected to check the return value for null
	 * 
	 * @return		view proxy that is wrapped by this object
	 * 
	 */
	public TiViewProxy getWrappedView() {
		return view != null ? view.getProxy() : null;
	}

	@Override
	public TiUIView getOrCreateView() {
		throw new IllegalStateException("Cannot create view on TiBaseWindowProxy");
	}

	@Override
	protected void handleOpen(KrollDict options)
	{
	}

	@Override
	protected void handleClose(KrollDict options)
	{
	}

	@Override
	protected Activity getWindowActivity()
	{
		return null;
	}

}

@@ -1108,16 +1108,78 @@
 vity) %7B%0A
+%09%09%09TiBaseActivity baseActivity = (TiBaseActivity)topActivity;%0A
 %09%09%09hostA
@@ -1222,20 +1222,17 @@
 tivity%3E(
-(TiB
+b
 aseActiv
@@ -1239,55 +1239,25 @@
 ity)
- topActivity);%0A%09%09%09((TiBaseActivity)topActivity)
+;%0A%09%09%09baseActivity
 .add

Address comments