package com.smartgwt.sample.showcase.client.forms.controls;

import java.util.Date;
import java.util.LinkedHashMap;

import com.smartgwt.client.data.Criterion;
import com.smartgwt.client.data.DateRange;
import com.smartgwt.client.data.RelativeDate;
import com.smartgwt.client.types.MultipleAppearance;
import com.smartgwt.client.util.DateDisplayFormatter;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.ColorPickerItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.DateRangeItem;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.MiniDateRangeItem;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.RelativeDateItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.SliderItem;
import com.smartgwt.client.widgets.form.fields.SpinnerItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.TimeItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.validator.DateRangeValidator;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;
import com.smartgwt.sample.showcase.client.mobile.SpinnerControlSample;

public class FormVariousControlsSample extends ShowcasePanel {
    private static final String DESCRIPTION = "Demonstration of several form controls.";
    private HTMLFlow logs;

    public Canvas getViewPanel() {
        VLayout layout = new VLayout(10);

        logs = new HTMLFlow();
        logs.setWidth(450);
        logs.setHeight(300);

        DynamicForm dateForm = new DynamicForm();
        dateForm.setWidth(450);
        dateForm.setColWidths(120, "*");
        dateForm.setIsGroup(true);
        dateForm.setGroupTitle("Date Controls");

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

        DateItem dateItem2 = new DateItem();
        dateItem2.setTitle("Date");
        dateItem2.setUseTextField(true);
        dateItem2.setHint("<nobr>Direct date input</nobr>");

        TimeItem timeItem1 = new TimeItem("timeItem", "Time");
        TimeItem timeItem2 = new TimeItem("timeItem", "Time");
        timeItem2.setHint("Picklist based time input");
        timeItem2.setUseTextField(false);

        DateRangeItem dateRangeItem = new DateRangeItem("dri", "Date Range");
        dateRangeItem.setAllowRelativeDates(true);
        DateRange dateRange = new DateRange();
        dateRange.setRelativeStartDate(RelativeDate.TODAY);
        dateRange.setRelativeEndDate(new RelativeDate("-1m"));
        dateRangeItem.setValue(dateRange);

        MiniDateRangeItem miniDateRangeItem = new MiniDateRangeItem("mdri", "Mini Date Range");
        final RelativeDateItem relativeDateItem = new RelativeDateItem("rdi", "Relative Date");

        // Trying to limit start date to 01.01.2016
        final Date startDate = new Date();
        //                startDate.setTime(Date.UTC(2016, 1, 1, 0, 0, 0));
        startDate.setYear(116);
        startDate.setMonth(1);
        startDate.setDate(1);
        log("Setting start date to " + startDate);
        //        relativeDateItem.setStartDate(startDate);

        SpinnerItem quantityFieldProperties = new SpinnerItem();
        quantityFieldProperties.setKeyPressFilter("[0-9]*");
        relativeDateItem.setAutoChildProperties("quantityField", quantityFieldProperties);

        relativeDateItem.setDateFormatter(new DateDisplayFormatter() {
            @Override public String format(Date date) {
                log("Formatting date " + date);
                return date.toString();
            }
        });
        final DateRangeValidator dateRangeValidator = new DateRangeValidator();
        dateRangeValidator.setMin(startDate);
        relativeDateItem.setValidators(dateRangeValidator);

        Button getValueButton = new Button("Log value");
        getValueButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
            @Override public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
                log("Value is " + relativeDateItem.getValue().toString());
                final Boolean isValid = relativeDateItem.validate();
                log("Validation result is " + isValid);
            }
        });

        Button getCriteriaButton = new Button("Log criteria");
        getCriteriaButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
            @Override public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
                final Criterion criterion = relativeDateItem.getCriterion();
                log("Criterion is " + criterion.getValues());
            }
        });

        dateForm.setItems(dateItem, dateItem2, timeItem1, timeItem2, dateRangeItem, miniDateRangeItem, relativeDateItem);

        layout.addMember(dateForm);
        layout.addMember(getValueButton);
        //        layout.addMember(getCriteriaButton);
        layout.addMember(logs);

        return layout;
    }

    private void log(String log) {
        String contents = logs.getContents();
        contents += "<br>" + log;
        logs.setContents(contents);
    }

    public String getIntro() {
        return DESCRIPTION;
    }

    public static class Factory implements PanelFactory {
        private String id;

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

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

}
