/*
 *  GeoSDI ERA - The new era of webGIS
 *  http://code.google.com/p/geosdiera/
 * ====================================================================
 *
 * Copyright (C) 2008-2009 GeoSDI Group (CNR IMAA).
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. 
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by developers
 * of GeoSDI Group.  For more information on GeoSDI, please see
 * &lt;http://www.geosdi.org/&gt;.
 *
 */

package it.geosdi.era.client.form;

import it.geosdi.era.client.AppEvents;
import it.geosdi.era.client.Costanti;
import it.geosdi.era.client.ERAMessages;
import it.geosdi.era.client.controllo.ControllerServer;
import it.geosdi.era.client.model.Server;
import it.geosdi.era.client.nodistringa.INodo;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Position;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.event.KeyListener;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.AnchorLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.tree.TreeNode;

/**
 * @author Giuseppe La Scaleia
 * @author Francesco Izzi
 */

public class FormAddServer extends Window implements IForm {

	/** Internationalization **/
	private static ERAMessages messages = GWT.create(ERAMessages.class);

	protected Controller controller;
	private TextField testoURLServer;
	private FormPanel formPanel;
	private Button bottoneSalva;
	protected Server server;

	public FormAddServer(Controller controller) {
		super.setTitle(messages.addServer());
		this.inizializzaWindow();
		this.inizializzaForm();
		this.add(formPanel);
		this.setModal(true);
		this.controller = controller;
	}

	private void inizializzaWindow() {
		setWidth(320);
		setHeight(150);
		setResizable(false);
		setLayout(new FitLayout());
		setButtonAlign(Position.CENTER);
		setCloseAction(Window.HIDE);
		setPlain(true);
	}

	private void inizializzaForm() {
		formPanel = new FormPanel();
		// strips all Ext styling for the component
		formPanel.setFrame(true);
		formPanel.setWidth(310);
		formPanel.setHeight(150);
		formPanel.setPaddings(5);

		testoURLServer = new TextField(messages.serverURL(), "urlServer");
		testoURLServer.addKeyListener(KeyboardListener.KEY_ENTER,
				new KeyListener() {

					public void onKey(int key, EventObject e) {
						if ((key == KeyboardListener.KEY_ENTER)
								&& (formPanel.getForm().isValid())) {
							execute();
							testoURLServer.setAllowBlank(true);
						}
					}

				});

		FieldSet fieldSet = new FieldSet(messages.serverURL());
		fieldSet.add(testoURLServer, new AnchorLayoutData("90%"));

		bottoneSalva = new Button(messages.save());
		bottoneSalva.addListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				if (formPanel.getForm().isValid()) {
					execute();
				}
			}
		});
		formPanel.add(fieldSet);
		Button bottoneCancella = new Button(messages.cancel());
		bottoneCancella.addListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				hide();
			}
		});
		formPanel.addButton(bottoneSalva);
		formPanel.addButton(bottoneCancella);
	}

	private Server costruisciServer(TreeNode nodoPadre) {
		server.setUrlServer(testoURLServer.getText());
		if (((INodo) nodoPadre.getUserObject()).toString().equals(
				Costanti.COMUNITY_SERVER)) {
			server.setTipoServer(Costanti.COMUNITY_SERVER);
		} else if (((INodo) nodoPadre.getUserObject()).toString().equals(
				Costanti.FEDERATED_SERVERS)) {
			server.setTipoServer(Costanti.FEDERATED_SERVER);
		}
		server.setTipoServizio(Costanti.WMS);
		return server;
	}

	public void resettaValori() {
		this.formPanel.getForm().reset();
	}

	public void initProgressBar() {
		Dispatcher.forwardEvent(AppEvents.InitProgressBar);
	}

	public void execute() {
		initProgressBar();
		TreeNode nodoSelezionato = (TreeNode) Registry
				.get(Costanti.SERVIZIO_SELEZIONATO);
		TreeNode nodoPadre = (TreeNode) nodoSelezionato.getParentNode();
		INodo nodoStringaServizio = (INodo) nodoSelezionato.getUserObject();
		server = new Server();
		if (nodoStringaServizio.toString().equals(Costanti.WMS)) {
			((ControllerServer) controller)
					.salvaServer(costruisciServer(nodoPadre));
		}
	}

	public TextField getTestoURLServer() {
		return testoURLServer;
	}
}
