package org.example.client;

import com.smartgwt.client.types.AnimationAcceleration;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.types.SelectionType;
import com.smartgwt.client.widgets.HeaderControl;
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.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;

public class SmartWindow extends Window {
	
	public SmartWindow() {
		super();
//		setAnimateHideTime(300);
//		setAnimateHideAcceleration(AnimationAcceleration.SMOOTH_END);
		setAnimateAcceleration(AnimationAcceleration.SMOOTH_START);
		setAnimateShowEffect(AnimationEffect.FADE);
//		setDragAppearance(DragAppearance.OUTLINE);
		setShadowDepth(4);
		setShowShadow(true);
		setResizeFrom("T","B","L","R","TL","TR","BL","BR"); //allow resizing from all sides
		
		addCloseClickHandler(new CloseClickHandler() {
			@Override
			public void onCloseClick(CloseClientEvent event) {
				animateHide(AnimationEffect.FADE);
			}
		});
	}
	
	private int nonMaximizedWidth;
	private int nonMaximizedHeight;
	private int nonMaximizedLeft;
	private int nonMaximizedTop;
	private boolean maximized = false;
	
	@Override
	public void setHeaderControls(Object... headerControls) {
		Object[] augmentedHeaderControls = new Object[headerControls.length+4];
		augmentedHeaderControls[0] = HeaderControls.HEADER_LABEL;
		for (int i = 0; i < headerControls.length; i++) {
			augmentedHeaderControls[i+1] = headerControls[i];
		}
//		augmentedHeaderControls = Arrays.copyOf(headerControls, headerControls.length+2);
		augmentedHeaderControls[augmentedHeaderControls.length-3] =  HeaderControls.MINIMIZE_BUTTON;
		augmentedHeaderControls[augmentedHeaderControls.length-2] =  new MaximizeHeaderControl(this);
		augmentedHeaderControls[augmentedHeaderControls.length-1] =  HeaderControls.CLOSE_BUTTON;
		super.setHeaderControls(augmentedHeaderControls);
	}
	/**
	 * Building our own maximization logic because of this issue http://forums.smartclient.com/showthread.php?t=10704&highlight=maximize
	 */
	class MaximizeHeaderControl extends HeaderControl{
		
		MaximizeHeaderControl(final Window parent){
			super(HeaderControl.MAXIMIZE);
			setActionType(SelectionType.CHECKBOX);
			setSelected(false);
			setShowSelectedIcon(true);
			
			ClickHandler maximizeWindowClickHandler = new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					if (!maximized){
						maximized = true;
						nonMaximizedWidth = parent.getWidth();
						nonMaximizedHeight = parent.getHeight();
						parent.setHeight100();
						parent.setWidth100();
						nonMaximizedLeft = parent.getLeft();
						nonMaximizedTop = parent.getTop();
						parent.setLeft(0);
						parent.setTop(0);

					} else {
						maximized = false;
						parent.setWidth(nonMaximizedWidth);
						parent.setHeight(nonMaximizedHeight);
						parent.setLeft(nonMaximizedLeft);
						parent.setTop(nonMaximizedTop);
					}
				}
			};
			addClickHandler(maximizeWindowClickHandler);
		}
	}
}
