package com.sujay.accessibility.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Img;
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.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class SgwtAccessibilityApp implements EntryPoint {

	// roles: application, banner, complementary, contentinfo, form, main, navigation, region, search
	public void onModuleLoad() {
		enableADA();
		final VLayout appLayout = new VLayout();
		//		appLayout.setAriaRole("article");

		// landmark - banner
		final HLayout banner = new HLayout();
		banner.setAriaRole("banner");

		// Banner Image
		final Img img = new Img("http://www.google.com/intl/en_com/images/srpr/logo3w.png");
		img.setAltText("Google Logo");
		img.setHeight(50);
		banner.addMember(img);
		appLayout.addMember(banner);

		final CustomHeader header1 = new CustomHeader(CustomHeader.Header.H1, "Header1");
		header1.setWidth("300px");
		header1.setHeight("20px");
		header1.setAlign(Alignment.LEFT);
		appLayout.addMember(header1);

		// landmark - label
		final Label label1 = new Label("Welcome to the application!");
		label1.setAccessKey("L");
		label1.setAriaRole("contentinfo");
		label1.setWidth("400px");
		appLayout.addMember(label1);

		final CustomHeader header2 = new CustomHeader(CustomHeader.Header.H2, "Header2");
		header2.setWidth("300px");
		header2.setHeight("20px");
		appLayout.addMember(header2);

		// landmark - label
		final Label label2 = new Label("Descriptive text for the form.");
		label2.setAccessKey("L");
		label2.setAriaRole("contentinfo");
		label2.setWidth("400px");
		appLayout.addMember(label2);

		// landmark - form 
		final DynamicForm form = new DynamicForm();
		form.setAriaRole("form");

		final TextItem textItem1 = new TextItem("myTextItem", "Text Item1");
		textItem1.setWrapTitle(false);
		textItem1.setTabIndex(10);

		final TextItem textItem2 = new TextItem("myTextItem2", "Text Item2");
		textItem2.setWrapTitle(false);
		textItem2.setTabIndex(20);

		final LinkItem linkItem = new LinkItem("link");
		linkItem.setTitle("LinkItem");
		linkItem.setLinkTitle("Click Me");
		linkItem.setTabIndex(30);

		final CheckboxItem checkboxItem = new CheckboxItem();
		checkboxItem.setTitle("Checkbox");
		checkboxItem.setTabIndex(40);

		final RadioGroupItem radioGroupItem = new RadioGroupItem();
		radioGroupItem.setTitle("Radio Group");
		radioGroupItem.setValueMap("Option 1", "Option 2");
		radioGroupItem.setTabIndex(50);
		radioGroupItem.setWidth("300px");
		radioGroupItem.setWrapTitle(false);

		final ComboBoxItem cbItem = new ComboBoxItem();
		cbItem.setTitle("Select");
		cbItem.setHint("<nobr>A simple combobox</nobr>");
		cbItem.setType("comboBox");
		cbItem.setValueMap("Cat", "Dog", "Giraffe", "Goat", "Marmoset", "Mouse");
		cbItem.setTabIndex(60);

		final DateItem dateItem = new DateItem();
		dateItem.setTitle("Date");
		dateItem.setUseTextField(true);
		dateItem.setHint("<nobr>Picklist based date input</nobr>");
		dateItem.setTabIndex(70);

		form.setItems(textItem1, textItem2, linkItem, checkboxItem, radioGroupItem, cbItem, dateItem);
		appLayout.addMember(form);

		//ibutton
		final IButton button = getIButton("iButton1", "IButton");
		button.setAccessKey("I");
		button.setTabIndex(80);
		appLayout.addMember(button);

		// css button
		final Button cssButton = getCssButton("cssButton1", "CSS Button - Invoke Modal Dialog");
		cssButton.setAccessKey("C");
		cssButton.setTabIndex(90);
		appLayout.addMember(cssButton);

		RootPanel.get("appDiv").add(appLayout);

		invokeModalDialog();

		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				SC.say("Hello world!");
			}
		});
		cssButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				invokeModalDialog();
			}
		});
	}

	public static native void enableADA() /*-{
		$wnd.isc.screenReader = true;
		//		$wnd.isc.setScreenReaderMode(true);
	}-*/;

	public IButton getIButton(final String widgetId, final String buttonText) {
		final IButton iButton = new IButton(buttonText);
		iButton.setAriaRole("button");
		iButton.setID(widgetId);
		return iButton;
	}

	public Button getCssButton(final String widgetId, final String buttonText) {
		final Button cssButton = new Button(buttonText);
		cssButton.setAriaRole("button");
		cssButton.setID(widgetId);
		return cssButton;
	}

	public Window getModalWindow(final int height, final int width, final String title) {
		final Window winModal = new Window();
		winModal.setAriaRole("dialog");
		winModal.setShowModalMask(true);
		winModal.setZIndex(999999);
		winModal.setTabIndex(0); //gets the focus to modal window in the screen reader
		winModal.setShowMinimizeButton(false);
		winModal.setIsModal(true);
		winModal.setWidth(width);
		winModal.setHeight(height);
		winModal.setTitle(title);
		winModal.centerInPage();
		//		winModal.setCanDragReposition(false);

		winModal.addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(final CloseClientEvent event) {
				winModal.destroy();
			}
		});
		//winModal.draw();
		winModal.show();

		return winModal;
	}

	public void invokeModalDialog() {
		final Window w = getModalWindow(400, 400, "Test Modal Window");

		final VLayout v = new VLayout();
		v.setTabIndex(1);
		v.setPadding(10);

		final CustomHeader header3 = new CustomHeader(CustomHeader.Header.H3, "Header3");
		header3.setWidth("300px");
		v.addMember(header3);

		final Label l = new Label("Descriptive text for the popup window.");
		l.setAriaRole("contentinfo");
		l.setWidth("300px");
		v.addMember(l);

		final DynamicForm f = new DynamicForm();
		f.setAriaRole("form");

		final TextItem t = new TextItem("myTextItem3", "Text Item 3");
		//		t.focusInItem(); //doesn't work, throws error
		t.setWrapTitle(false);
		t.setTabIndex(2);

		f.setFields(t);

		// none of these work to get the field in focus, how the heck do we get the focus on TextItem3?
		f.focusInItem(t);
		//		f.focus();
		//		f.focusInItem(0);
		//		f.selectRecord(0);
		v.addMember(f);

		final Button b = getCssButton("cssButton2", "Go");
		b.setAccessKey("G");
		b.setTabIndex(3);
		//		b.setShowFocused(true);
		//		b.focus();
		v.addMember(b);

		b.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				w.destroy();
			}
		});

		w.addItem(v);
	}

}
