package com.itmill.toolkit.demo;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.service.ApplicationContext.TransactionListener;
import com.itmill.toolkit.terminal.gwt.server.WebApplicationContext;
import com.itmill.toolkit.ui.FormLayout;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.Window;

public class DemoPortletApplication extends Application {

	private Label portletMode;
	private Label windowState;
	private Label userPrincipal;
	private Label userInfo;

	public void init() {
		// init main window, add labels
		Window main = new Window();
		setMainWindow(main);
		FormLayout layout = new FormLayout();
		main.addComponent(layout);

		portletMode = new Label();
		portletMode.setCaption("PortletMode: ");
		layout.addComponent(portletMode);

		windowState = new Label();
		windowState.setCaption("WindowState: ");
		layout.addComponent(windowState);

		userPrincipal = new Label();
		userPrincipal.setCaption("UserPrincipal: ");
		layout.addComponent(userPrincipal);

		userInfo = new Label();
		userInfo.setCaption("User info: ");
		layout.addComponent(userInfo);
		userInfo.setContentMode(Label.CONTENT_PREFORMATTED);

		Label id = new Label("" + this);
		id.setCaption("Application instance: ");
		layout.addComponent(id);

		// Add a transaction listener, so we can update the portlet stuff
		initTransactionListener();
	}

	private void initTransactionListener() {
		getContext().addTransactionListener(new TransactionListener() {

			public void transactionEnd(Application application,
					Object transactionData) {
				// not used
			}

			public void transactionStart(Application application,
					Object transactionData) {
				if (application == DemoPortletApplication.this) {
					// get the session
					HttpSession ses = ((WebApplicationContext) getContext())
							.getHttpSession();
					// the portlet has stored it's info there:
					DemoPortlet.PortletInfo p = (DemoPortlet.PortletInfo) ses.getAttribute(ses
							.getServletContext().getServletContextName()
							+ "/DemoPortletApplication");
					if (p == null) {
						// not found, try w/o application name
						p = (DemoPortlet.PortletInfo) ses.getAttribute(ses
								.getServletContext().getServletContextName()+"/");
					}

					if (p != null) {
						// update labels
						portletMode.setValue(p.getPortletMode());
						windowState.setValue(p.getWindowState());
						userPrincipal.setValue(p.getUserPrincipal());
						Map uinfo = p.getUserInfo();
						if (uinfo != null) {
							String s = "";
							for (Iterator it = uinfo.keySet().iterator(); it
									.hasNext();) {
								Object key = it.next();
								Object val = uinfo.get(key);
								s += key + ": " + val + "\n";
							}
							userInfo.setValue(s);
						} else {
							userInfo.setValue("-");
						}
					} else {
						// portlet not started
					}
				}
			}

		});

	}
}
