package com.smartgwt.sample.showcase.client.windows;

import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;

public class WindowModalitySample extends ShowcasePanel {
	private static final String DESCRIPTION = "<p>Click on \"Show Window\" to show a modal window.</p><p>Note that the \"Touch This\" button no "
			+ "longer shows rollovers or an interactive cursor, nothing outside the window can be clicked, clicks outside the window cause "
			+ "the window to flash, and tabbing remains in a closed loop cycling through only the contents of the window.</p>";
	 public static final int DEFAULT_WIDTH = 350;
	 public static final int MAX_HEIGHT = 500;
	public static class Factory implements PanelFactory {
		private String id;

		public ShowcasePanel create() {
			WindowModalitySample panel = new WindowModalitySample();
			id = panel.getID();
			return panel;
		}

		public String getID() {
			return id;
		}

		public String getDescription() {
			return DESCRIPTION;
		}
	}

	public Canvas getViewPanel() {
		IButton buttonShowWindow = new IButton("Show Resized Window");
		buttonShowWindow.setShowRollOver(true);
		buttonShowWindow.setShowDown(true);
		buttonShowWindow.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String contentMoreThen400 = DESCRIPTION.concat(DESCRIPTION).concat(DESCRIPTION).concat(DESCRIPTION).concat(DESCRIPTION).concat(DESCRIPTION).concat(DESCRIPTION);
				final Window winModal = createWindow(contentMoreThen400);				
				winModal.show();
			}
		});
		
		IButton buttonShowWindow2 = new IButton("Show Window");
		buttonShowWindow2.setShowRollOver(true);
		buttonShowWindow2.setShowDown(true);
		buttonShowWindow2.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String contentLessThen400 = DESCRIPTION.concat(DESCRIPTION).concat(DESCRIPTION);
				final Window winModal = createWindow(contentLessThen400);
				winModal.show();
			}
		});

		VLayout layoutMain = new VLayout();
		layoutMain.addMember(buttonShowWindow);
		layoutMain.addMember(buttonShowWindow2);

		return layoutMain;
	}

	public String getIntro() {
		return DESCRIPTION;
	}

	private Label createLabel(String contents) {
        Label label = new Label(contents);
        label.setPadding(5);
        label.setWidth(DEFAULT_WIDTH);
        label.setAutoHeight();
        label.setIconAlign("left");
        label.setIconSize(17);
        return label;
    }

    protected void setWindowProperties(Window winModal) {
    	winModal.setAutoSize(true);
    	winModal.setWidth(DEFAULT_WIDTH);
    	winModal.setDismissOnEscape(true);
    	winModal.setDismissOnOutsideClick(true);
    	winModal.setUseOpacityFilter(true);
    	winModal.setIsModal(true);
    	winModal.setShowModalMask(false);
    	winModal.setOverflow(Overflow.AUTO);
    }


	private VLayout createTopLayout(String content) {
		VLayout topInnerLayout = new VLayout();
		topInnerLayout.setLayoutMargin(10);
		topInnerLayout.setLayoutLeftMargin(20);
		topInnerLayout.setShadowDepth(1);
		topInnerLayout.setShadowOffset(0);
		topInnerLayout.setShowShadow(true);
		topInnerLayout.setBackgroundColor("white");
		topInnerLayout.setAutoHeight();
		topInnerLayout.setWidth100();
		topInnerLayout.addMember(createLabel(content));

		// no-need-to-locate
		VLayout topLayout = new VLayout();
		topLayout.setWidth100();
		topLayout.setAutoHeight();
		topLayout.setLayoutMargin(22);
		topLayout.addMember(topInnerLayout);
		return topLayout;
	}

	private VLayout createMainLayout(String content) {
		VLayout layout = new VLayout();
		layout.setWidth100();
		layout.addMember(createTopLayout(content));
		layout.addMember(buildContentLayout());
		layout.setAutoDraw(false);
		layout.setOverflow(Overflow.VISIBLE);
		return layout;
	}

	private VLayout buildContentLayout() {
		// no-need-to-locate
		VLayout contentLayout = new VLayout();
		contentLayout.setBackgroundColor("transparent");

		contentLayout.setLayoutTopMargin(20);

		contentLayout.setOverflow(Overflow.AUTO);
		contentLayout.setWidth100();
		contentLayout.setAlign(VerticalAlignment.TOP);
		return contentLayout;

	}

	private Window createWindow(String content) {
		final Window winModal = new Window();
		winModal.setTitle("Modal Window");
		winModal.setShowMinimizeButton(false);
		winModal.setShowModalMask(true);
		winModal.setBodyColor("white");
		winModal.setShowEdges(false);
		winModal.setShowMinimizeButton(false);
		winModal.setIsModal(true);
		winModal.setShowShadow(false);
		winModal.setShadowSoftness(10);
		winModal.setShadowOffset(0);
		setWindowProperties(winModal);

		
		final VLayout mainLayout = createMainLayout(content);
		mainLayout.addDrawHandler(new DrawHandler() {
			
			@Override
			public void onDraw(DrawEvent event) {
				if (mainLayout.getVisibleHeight() > 400) {
					//uncomment here to see the exception
					//mainLayout.setOverflow(Overflow.AUTO);
					mainLayout.resizeTo(mainLayout.getWidth(), MAX_HEIGHT);
				}
			}
		});
		winModal.addItem(mainLayout);
		winModal.addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(CloseClickEvent event) {
				winModal.destroy();
			}
		});
		return winModal;
	}
}