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

import com.smartgwt.client.data.Record;
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.FormItemHoverFormatter;
import com.smartgwt.client.widgets.form.FormItemValueFormatter;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.PickerIcon;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.FormItemClickHandler;
import com.smartgwt.client.widgets.form.fields.events.FormItemIconClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ItemHoverEvent;
import com.smartgwt.client.widgets.form.fields.events.ItemHoverHandler;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;

public class PickerControlsSample extends ShowcasePanel {
    private static final String DESCRIPTION = "<p>The CustomPicker example demonstrates various standard picker icons that provided with the library. " +
            "You can combine as many pickers as you like as illustrated by the sample below.</p>";

    public static class Factory implements PanelFactory {
        private String id;

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

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

    public Canvas getViewPanel() {

        final DynamicForm form = new DynamicForm();
        form.setMargin(10);
        form.setAutoFocus(true);
        form.setWidth(400);
        form.setTitleWidth(100);

        PickerIcon clearPicker = new PickerIcon(PickerIcon.CLEAR, new FormItemClickHandler() {
            public void onFormItemClick(FormItemIconClickEvent event) {
                SC.say("Clear Picker clicked");
            }
        });
        clearPicker.setNeverDisable(true);

        PickerIcon searchPicker = new PickerIcon(PickerIcon.SEARCH, new FormItemClickHandler() {
            public void onFormItemClick(FormItemIconClickEvent event) {
                SC.say("Search Picker clicked");
            }
        });
        searchPicker.setNeverDisable(true);

        PickerIcon datePicker = new PickerIcon(PickerIcon.DATE, new FormItemClickHandler() {
            public void onFormItemClick(FormItemIconClickEvent event) {
                SC.say("Date Picker clicked");
            }
        });
        datePicker.setNeverDisable(true);

        PickerIcon refreshPicker = new PickerIcon(PickerIcon.REFRESH, new FormItemClickHandler() {
            public void onFormItemClick(FormItemIconClickEvent event) {
                SC.say("Refresh Picker clicked");
            }
        });
        refreshPicker.setNeverDisable(true);

        TextItem pickerControls = new TextItem("pickerControls", "Picker Controls");
        pickerControls.setIcons(clearPicker, searchPicker, refreshPicker);   
        pickerControls.setDisabled(false);

        TextItem datePickerControl = new TextItem("datePicker", "Date Picker");
        datePickerControl.setIcons(datePicker);
        datePickerControl.setDisabled(true);

        TextItem refreshOnlyControl = new TextItem("refreshPicker", "Refresh Picker");
        refreshOnlyControl.setIcons(refreshPicker);
        refreshOnlyControl.setCanEdit(false);

        addDefaultValue(pickerControls, datePickerControl, refreshOnlyControl);
        addFormaters(pickerControls, datePickerControl, refreshOnlyControl);
        
        form.setFields(pickerControls, datePickerControl, refreshOnlyControl);

        return form;
    }
    
    private void addDefaultValue(TextItem ...items){
    	for(TextItem item:items){
    		item.setValue("Internal Value");
    	}
    }
    
    private void addFormaters(TextItem ...items ){
    	for(TextItem item: items){
    	
	    	item.setEditorValueFormatter(new FormItemValueFormatter() {
				
				@Override
				public String formatValue(Object value, Record record, DynamicForm form,
						FormItem item) {
					return "Display Value";
				}
			});
	    	
	    	item.setItemHoverFormatter(new FormItemHoverFormatter() {
				
				@Override
				public String getHoverHTML(FormItem item, DynamicForm form) {
					return "value="+item.getValue().toString();
				}
			});
    	}
    }
}
