/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.dhaval.web.vaadin.simple.forms;

import com.dhaval.web.vaadin.simple.EntryPoint.GenericFieldFactory;
import com.dhaval.web.vaadin.simple.GenericForm;
import com.dhaval.web.vaadin.simple.GenericFormEventHandler;
import com.dhaval.web.vaadin.simple.domain.Form;
import com.dhaval.web.vaadin.simple.domain.FormField;
import com.dhaval.web.vaadin.simple.domain.FormsContainer;
import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 *
 * @author dhaval
 */
public class FormDefinition extends VerticalLayout{

    private Application app;
    private Set<FormField> fieldSet = new LinkedHashSet<FormField>();
    private FormsContainer formContainer;

    public FormDefinition(final Application app, final FormsContainer formContainer) {
        this.app = app;
        this.formContainer = formContainer;

        setSpacing(true);
        setMargin(true);
        
        final TextField formName = new TextField("Name");
        formName.setRequired(true);

        final TextField formDesc = new TextField("Description");
        formDesc.setRequired(true);
        formDesc.setRows(5);
        formDesc.setWidth("100%");

        addComponent(formName);
        addComponent(formDesc);

        Button addFieldBtn = new Button("Add Field");

        Panel fieldsPanel = new Panel();
        fieldsPanel.setScrollable(true);
        fieldsPanel.setWidth("100%");
        fieldsPanel.setHeight("100px");
        final VerticalLayout fieldsLayout = new VerticalLayout();
        fieldsLayout.setSpacing(true);
        fieldsPanel.addComponent(fieldsLayout);

        addFieldBtn.setStyleName(Button.STYLE_LINK);
        addFieldBtn.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                final Window formWindow = getEmptyWindow("Enter Field Information");
                final GenericForm fieldForm = new GenericForm(new FormField(), "Form Field", new GenericFieldFactory(Arrays.asList(new String[]{"type"})), new GenericFormEventHandler() {

                    public void formCommitted(Object formBean) {
                        try{
                            FormField field = (FormField) formBean;
                            if(field.getName().length() <= 0 || field.getType().length() <= 0){
                                return;
                            }

                            if(!fieldSet.add(field)){
                                Window.Notification not = new Window.Notification("Invalid Field Name", "Given field " + field.getName() + " is already available.",
                                        Window.Notification.TYPE_ERROR_MESSAGE);
                                getWindow().showNotification(not);
                                return;
                            }
                            app.getMainWindow().removeWindow(formWindow);

                            HorizontalLayout fieldP = new HorizontalLayout();
                            fieldP.setSpacing(true);
                            fieldP.addComponent(new Label("<div style='font-family: verdana; font-size: 1.5em;'>" + field.getName() + "</div>", Label.CONTENT_XHTML));
                            fieldP.addComponent(new Label(field.getType(), Label.CONTENT_XHTML));
                            fieldsLayout.addComponent(fieldP);
                            fieldsLayout.requestRepaint();
                        } catch (Exception e) {
                            Window.Notification not = new Window.Notification("Error", e.getMessage(), Window.Notification.TYPE_ERROR_MESSAGE);
                            getWindow().showNotification(not);
                        }
                    }
                });
                formWindow.addComponent(fieldForm);
                app.getMainWindow().addWindow(formWindow);
            }
        });

        addComponent(addFieldBtn);
        addComponent(fieldsPanel);
        
        addComponent(new Label("<hr>", Label.CONTENT_XHTML));
        addComponent(new Button("Save", new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                try{
                    Form form = new Form(formName.getValue().toString(), formDesc.getValue().toString());
                    form.setFields(fieldSet);

                    if(form.getName().length() <= 0 || form.getDescription().length() <= 0 || form.getFields().isEmpty()){
                        return;
                    }

                    formContainer.addForm(form);

                    formName.setValue("");
                    formDesc.setValue("");
                    fieldSet= new HashSet<FormField>();
                    fieldsLayout.removeAllComponents();

                    Window.Notification not = new Window.Notification("Form saved successfully", Window.Notification.TYPE_HUMANIZED_MESSAGE);
                    getWindow().showNotification(not);
                }catch(Exception e){

                }
            }
        }));
    }

    public Window getEmptyWindow(String header){
        Window formWindow = new Window(header);
        formWindow.setModal(true);
        formWindow.setClosable(true);
        formWindow.setWidth("30%");
        return formWindow;
    }
}
