package com.myapp.client;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.IButton;
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.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.BlurbItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.validator.IsBooleanValidator;
import com.smartgwt.client.widgets.layout.HLayout;
import java.util.ArrayList;
import java.util.List;

public class FormWindow extends Window {
    private DynamicForm form = null;

    private DateItem dateItem;

    private boolean addValidatorAfterFormAddedToWindow;

    public FormWindow(boolean addValidatorAfterFormAddedToWindow) {
        super();
        this.addValidatorAfterFormAddedToWindow = addValidatorAfterFormAddedToWindow;
        configureFormWindow();
    }

    protected void configureFormWindow() {

        configureWindow();

        form = new DynamicForm();
        form.setFields(getFormItems());

        HLayout buttons = getFormButtons();

        // this seems to result in the built in isDate not being created
        if (addValidatorAfterFormAddedToWindow) {
            setTitle("Validation added *after* form added to window");
            addItem(form);
            dateItem.setValidators(new IsBooleanValidator());
        }
        else {
            setTitle("Validation added *before* form added to window");
            dateItem.setValidators(new IsBooleanValidator());
            addItem(form);
        }

        addItem(buttons);
    }

    protected void configureWindow() {
        setPadding(15);
        setWidth(400);
        setHeight(150);
    }

    protected FormItem[] getFormItems() {
        List<FormItem> items = new ArrayList<FormItem>();

        BlurbItem blurb = newBlurb();
        items.add(blurb);

        dateItem = newDateItem();
        items.add(dateItem);

        return items.toArray(new FormItem[items.size()]);
    }

    protected HLayout getFormButtons() {
        HLayout buttons = new HLayout();
        buttons.setMembersMargin(5);
        buttons.setAlign(Alignment.CENTER);
        buttons.setWidth("100%");
        buttons.setMargin(10);

        IButton save = new IButton("OK");
        save.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                form.validate();
            }
        });

        IButton cancel = new IButton("Cancel");
        cancel.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                destroy();
            }
        });

        buttons.addMember(save);
        buttons.addMember(cancel);
        return buttons;
    }

    private BlurbItem newBlurb() {
        BlurbItem blurb = new BlurbItem();
        if (addValidatorAfterFormAddedToWindow) {
            blurb.setValue("This form does not seem to execute the implicit isDate validator, continuing on to the IsBoolean() validator.");
        }
        else {
            blurb.setValue("This form executes the implicit isDate validator, and stops the next validator, IsBoolean() from running.");
        }
        blurb.setWrap(true);
        return blurb;
    }

    private DateItem newDateItem() {
        DateItem startDate = new DateItem("startDate", "Start");
        startDate.setUseTextField(true);
        startDate.setWrapTitle(false);
        return startDate;
    }

}