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

import java.util.HashMap;
import java.util.Map;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;

public class FormDataboundDependentSelectsSample extends ShowcasePanel {
    private static final String TEST_FIELD = "testField";
    private static final String TEST_FIELD2 = "testField2";

    private enum Languages {
        EN, DE
    };

    private static final String DESCRIPTION = "<p>This example demonstrates two select items, both of which load data on the fly from a DataSource, "
            + "where the \"Category\" drop-down controls the list of available items in the \"Item\" drop-down.</p>"
            + "<p>Try selecting a value in the \"Category\" drop-down list to change the set of options available in the \"Item\" drop-down.</p>";

    public static class Factory implements PanelFactory {
        private String id;

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

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

    public Canvas getViewPanel() {

        final DynamicForm form = new DynamicForm();
        form.setWidth(500);
        form.setNumCols(4);
        DataSource testDS = new TestDS();
        localize(testDS, Languages.DE);

        form.setDataSource(testDS);
        SelectItem testSelectItem = new SelectItem(TEST_FIELD);
        SelectItemWithPickList testSelectItemWithPickList = new SelectItemWithPickList(TEST_FIELD2, true, false);
        form.setItems(testSelectItem, testSelectItemWithPickList);

        return form;
    }

    private void localize(DataSource testDS, Languages lang) {
        for (DataSourceField dsField : testDS.getFields()) {
            if (TEST_FIELD.equals(dsField.getName()) || TEST_FIELD2.equals(dsField.getName())) {
                Map<String, String> localizedValueMap = dsField.getValueMap();
                for (String id : localizedValueMap.keySet()) {
                    if ("1".equals(id)) {
                        localizedValueMap.put(id, lang == Languages.EN ? "one" : "ein");
                    } else if ("2".equals(id)) {
                        localizedValueMap.put(id, lang == Languages.EN ? "two" : "zwei");
                    }
                }
                dsField.setValueMap(localizedValueMap);
            }
        }
    }

    public String getIntro() {
        return DESCRIPTION;
    }

    public class TestDS extends DataSource {
        public TestDS() {
            setID("testDS");
            DataSourceTextField testField = new DataSourceTextField(TEST_FIELD, "Test");
            DataSourceTextField testField2 = new DataSourceTextField(TEST_FIELD2, "Test");

            Map<String, String> valueMap = new HashMap<String, String>();
            valueMap.put("1", null);
            valueMap.put("2", null);

            testField.setValueMap(valueMap);
            testField2.setValueMap(valueMap);

            setFields(testField, testField2);
            setClientOnly(true);
        }
    }

    public class SelectItemWithPickList extends SelectItem {
        public SelectItemWithPickList(String name, final boolean multiple, boolean allowEmptyValue) {
            super(name);
            setMultiple(multiple);
            setAllowEmptyValue(allowEmptyValue);

            setPickListFields(getPickListFields());
            setPickListProperties(getPickListGrid());
        }

        public ListGridField[] getPickListFields() {
            ListGridField field = new ListGridField(getName(), 230);
            field.setShowHover(true);
            return new ListGridField[] { field };
        }

        protected ListGrid getPickListGrid() {
            ListGrid pickListGrid = new ListGrid();

            pickListGrid.setShowFilterEditor(getShowFilterEditor());
            pickListGrid.setBackgroundColor("#fff");
            pickListGrid.setCanReorderFields(false);
            pickListGrid.setCanFreezeFields(false);
            pickListGrid.setCanPickFields(false);
            pickListGrid.setCanGroupBy(false);

            return pickListGrid;
        }

        protected boolean getShowFilterEditor() {
            return true;
        }
    }
}