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

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.smartgwt.client.data.DateRange;
import com.smartgwt.client.data.RelativeDate;
import com.smartgwt.client.types.MultipleAppearance;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
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.layout.VLayout;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;

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

    public static class Factory implements PanelFactory {
        private String id;

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

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

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

        final DynamicForm form = new DynamicForm();

        RadioGroupItem radioGroupItem = new RadioGroupItem();
        radioGroupItem.setTitle("Radio Group");
        
        LinkedHashMap<String, String> unsorted = new LinkedHashMap<String, String>();
        unsorted.put(Options.CCC.getIdAsString(), Options.CCC.getValue());
        unsorted.put(Options.DDD.getIdAsString(), Options.DDD.getValue());
        unsorted.put(Options.BBB.getIdAsString(), Options.BBB.getValue());
        unsorted.put(Options.EEE.getIdAsString(), Options.EEE.getValue());
        unsorted.put(Options.AAA.getIdAsString(), Options.AAA.getValue());
        unsorted.put(Options.FFF.getIdAsString(), Options.FFF.getValue());
        printMap("Unsorted map", unsorted);
        
        LinkedHashMap<String, String> sorted = getSortedMap(unsorted);
        printMap("Sorted map", sorted);
        radioGroupItem.setValueMap(sorted);
        
        form.setFields(radioGroupItem);
        layout.addMember(form);

        return layout;
    }

    public String getIntro() {
        return DESCRIPTION;
    }

    
    private LinkedHashMap<String, String> getSortedMap(HashMap<String, String> sections) {
        List<String> sortedList = new ArrayList<String>();
        sortedList.addAll(sections.values());
        Collections.sort(sortedList);

        LinkedHashMap<String, String> sortedMap = new LinkedHashMap<String, String>();
        for (String listValue : sortedList) {
            for (Map.Entry<String, String> entry : sections.entrySet()) {
                if (listValue.equals(entry.getValue())) {
                    sortedMap.put(entry.getKey(), entry.getValue());
                }
            }
        }

        return sortedMap;
    }
    
    private void printMap(String message, LinkedHashMap<String, String> map) {
        System.out.println(message);
        for(String key: map.keySet()) {
            System.out.println(key + ":" + map.get(key));
        }
    }
}

enum Options {

    AAA(5, "AAA 5"),
    BBB(4, "BBB 4"),
    CCC(3, "CCC 3"),
    DDD(2, "DDD 2"),
    EEE(1, "EEE 1"),
    FFF(0, "FFF 0");
        
    private final int id;
    private String value;

    private Options(int id,String value) {

        this.id = id;
        this.value = value;
    }
    
    public String getValue() {
        return value;
    }
    
    public String getIdAsString() {
        return String.valueOf(id);
    }
}