package solysombra.client.views;
import java.util.HashMap;
import solysombra.client.MashupService;
import solysombra.client.MashupServiceAsync;
import solysombra.client.SolYSombra;
import solysombra.client.maps.CustomizedMap1;
import solysombra.shared.domain.wwo.Forecast;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.maps.gwt.client.Geocoder;
import com.google.maps.gwt.client.GeocoderRequest;
import com.google.maps.gwt.client.GeocoderResult;
import com.google.maps.gwt.client.GeocoderStatus;
import com.google.maps.gwt.client.LatLng;
public class ViewBuscadorSitio extends Composite {
private final HorizontalPanel mainPanel;
public static ListBox ciudades = new ListBox();
private static Geocoder geocoder = Geocoder.create();
private static LatLng coord = LatLng.create(0.0, 0.0);
private static TextBox searchBox = new TextBox();
private final static MashupServiceAsync mashupService = GWT
.create(MashupService.class);
private static Double porcentajeSolar;
private static boolean haysol = true;
public ViewBuscadorSitio(HashMap<String, Integer> params) {
final HashMap<String, Integer> params2 = params;
mainPanel = new HorizontalPanel();
initWidget(mainPanel);
VerticalPanel buscaPanel = new VerticalPanel();
HorizontalPanel searchBar = new HorizontalPanel();
HorizontalPanel linksBar = new HorizontalPanel();
buscaPanel.getElement().addClassName("buscaPanel");
searchBar.getElement().addClassName("searchBar");
linksBar.getElement().addClassName("linksBar");
mainPanel.getElement().addClassName("buscadorSitio");
// Creamos los elementos de la searchBar
ciudades.clear();
ciudades.addItem("----------");
ciudades.addItem("A-CORU\u00D1A");
ciudades.addItem("ALMERIA");
ciudades.addItem("BARCELONA");
ciudades.addItem("GRANADA");
ciudades.addItem("MADRID");
ciudades.addItem("MALAGA");
ciudades.addItem("MURCIA");
ciudades.addItem("OVIEDO");
ciudades.addItem("PALMASdeGC");
ciudades.addItem("SEVILLA");
ciudades.addItem("VALENCIA");
ciudades.addItem("VALLADOLID");
ciudades.addItem("ZARAGOZA");
Button searchButton = new Button("Buscar");
// searchBox.addValueChangeHandler(new ValueChangeHandler<String>() {
//
// @Override
// public void onValueChange(ValueChangeEvent<String> event) {
//
// }
// });
searchButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
buscaSitio(searchBox.getText());
// Document.get().getElementById("submitHS").
}
});
if (params.containsKey("ciudadSeleccionada")) {
ciudades.setSelectedIndex(params.get("ciudadSeleccionada"));
}
// Creamos los elementos de la linksBar
Hyperlink huellaSolar = new Hyperlink("Huella Solar",
"http://www.huellasolar.com/");
huellaSolar.getElement().addClassName("huellaSolar");
Hyperlink google = new Hyperlink("GoogleMaps", "http://google.com");
google.getElement().addClassName("google");
Hyperlink weather = new Hyperlink("WWO",
"http://www.worldweatheronline.com/");
weather.getElement().addClassName("weatherCannel");
// A�adimos a la searchBar
searchBar.add(searchBox);
searchBar.add(searchButton);
searchBar.add(ciudades);
// A�adimos a la linksBar
linksBar.add(huellaSolar);
linksBar.add(google);
linksBar.add(weather);
Image image = new Image("images/icono-rutas.png");
image.getElement().addClassName("imageRuta");
PushButton ruta = new PushButton(image);
ruta.getElement().addClassName("rutaButton");
ruta.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
SolYSombra.go("rutaSolar", params2);
}
});
mainPanel.add(ruta);
buscaPanel.add(searchBar);
buscaPanel.add(linksBar);
mainPanel.add(buscaPanel);
}
private void buscaSitio(final String address) {
haysol = true;
GeocoderRequest GReq = GeocoderRequest.create();
String aux = address + ", "
+ ciudades.getValue(ciudades.getSelectedIndex());
GReq.setAddress(aux.toUpperCase().trim());
geocoder.geocode(GReq, new Geocoder.Callback() {
@Override
public void handle(JsArray<GeocoderResult> a, GeocoderStatus b) {
// while (!(esCoordEspa�ola(coord))) {
if (datosBusquedaInic()) {
if (b == GeocoderStatus.OK) {
GeocoderResult result = a.shift();
coord = result.getGeometry().getLocation();
System.out.println("Ahora cambio las coord: " + coord);
if (ViewSelectorCiudad.ciudadActual != null
&& ViewSelectorCiudad.ciudadActual
.equals(ciudades.getValue(ciudades
.getSelectedIndex()))) {
Document.get().getElementById("lon")
.setPropertyDouble("value", coord.lng());
Document.get().getElementById("lat")
.setPropertyDouble("value", coord.lat());
} else {
RootPanel.get("wwo").clear();
Document.get().getElementById("lon")
.setPropertyDouble("value", coord.lng());
Document.get().getElementById("lat")
.setPropertyDouble("value", coord.lat());
System.out.println(Document.get()
.getElementsByTagName("y").toString());
mashupService.getWWOForecast(coord.lat(),
coord.lng(), new AsyncCallback<Forecast>() {
@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(Forecast result) {
ViewSelectorCiudad.showForecast(
result,
ciudades.getValue(ciudades
.getSelectedIndex()));
if (!(result.getData()
.getCurrent_condition()
.get(0).getWeatherDesc()
.get(0).getValue()
.equals("Sunny"))) {
haysol = false;
}
}
});
}
// AQU� HABR�A QUE INSERTAR ALG�N M�TODO QUE LLAMARA
// A HUELLA SOLAR, COGIERA LOS DATOS Y LOS UTILIZARA
// EN UN MARCADOR O ALGO AS�
RootPanel.get("hs").clear();
mashupService.getFormResponse(coord.lng(), coord.lat(),
Document.get().getElementById("mes")
.getPropertyInt("value"), Document
.get().getElementById("dia")
.getPropertyInt("value"), Document
.get().getElementById("hora")
.getPropertyInt("value"),
new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(String result) {
resultadosHS(result);
CustomizedMap1.showMapSitioSol(coord,
address, porcentajeSolar);
}
});
CustomizedMap1.showMapSitio(coord, address);
} else {
Window.alert("Algo sali� mal, comprobar la direcci�n");
}
// }
}
}
});
}
// private static boolean esCoordEspa�ola(LatLng coord) {
// boolean res = true;
// if (coord.lat() > 43.4738 || coord.lat() < 27.3816
// || coord.lng() < -18.0938 || coord.lng() > 4.1946) {
// res = false;
// }
// return res;
// }
// private void rellenaDatosHS() {
// System.out.println(Document.get().getElementById("lat")
// .getPropertyDouble("y"));
//
// }
public void resultadosHS(String entrada) {
String[] trozos = entrada.split(",");
if (trozos.length != 4) {
if (trozos.length == 2 && trozos[1].equals("Es de noche")) {
String output = "<br/><br/><fieldset style=\"border-style: dotted;border-color: black; font-size: 1em; font-family: \"Lucida Sans Unicode\"\">";
output += "<legend >" + "<h2>Soleamiento</h2>" + "</legend>";
output += "<span>Es de noche</span>";
output += "</fieldset>";
HTML photos = new HTML(output);
RootPanel.get("hs").add(photos);
porcentajeSolar = 0.0;
} else {
throw new IllegalArgumentException(
"Numero de argumentos invalido");
}
} else {
Double soleamientoActual = new Double(trozos[0]);
porcentajeSolar = soleamientoActual;
Double soleamientoActualCubierta = new Double(trozos[1]);
Double soleamientoFuturo = new Double(trozos[2]);
Double soleamientoFuturoCubierta = new Double(trozos[3]);
String output = "<br/><br/><fieldset style=\"border-style: dotted;border-color: black; font-size: 1em; font-family: \"Lucida Sans Unicode\"\">";
output += "<legend >" + "<h2>Soleamiento</h2>" + "</legend>";
if (entrada != null) {
output += "<span>Soleamiento esa hora: " + soleamientoActual
+ " %"
+ "</span> <br/> <span>Soleamiento una hora despues: "
+ soleamientoFuturo + " %" + "</span>";
} else {
output = "<span> No results </span>";
}
output += "</fieldset>";
if (!haysol) {
output += "<p>Al no estar soleado, los datos de soleamiento expuestos no son fiables</p>";
}
HTML photos = new HTML(output);
RootPanel.get("hs").add(photos);
}
}
public boolean datosBusquedaInic() {
boolean res = true;
TextArea errorBox = new TextArea();
String errorMsg = "";
// if (Document.get().getElementById("lon").getPropertyString("value")
// .equals("Longitud")
// || Document.get().getElementById("lat")
// .getPropertyString("value").equals("Latitud")) {
if (searchBox.getText().equals("")) {
errorMsg += "falta sitio ";
res = false;
}
if (Document.get().getElementById("mes").getPropertyString("value")
.equals("Mes")
|| Document.get().getElementById("dia")
.getPropertyString("value").equals("Dia")) {
errorMsg += "falta fecha ";
res = false;
}
if (Document.get().getElementById("hora").getPropertyString("value")
.equals("Hora")) {
errorMsg += "falta hora ";
res = false;
}
errorBox.setText(errorMsg);
if (!errorMsg.isEmpty()) {
mainPanel.add(errorBox);
}
return res;
}
}