package kz.cafe.admin.panel_extensions;

import kz.cafe.admin.panels.*;
import com.vaadin.terminal.*;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;

public class ExtraPanelGoogleMap extends AbstractExtendedPanel implements java.io.Serializable{
   private static final long serialVersionUID = 711754256190346567L;

   private Table table;
   private String sColumn;
   private Button button = new Button("Карта");
   
   
   public ExtraPanelGoogleMap(DBListPanel<?> panel, String sColumn){
      this.table = panel.table;
      this.sColumn = sColumn;

      AbstractLayout panelLayout = (AbstractLayout) getContent();
      panelLayout.setMargin(false);      
      
      button.setEnabled(false);
      button.addListener(new ClickMap());
      
      this.addComponent(button);      
   }
   
   

   private class ClickMap implements Button.ClickListener{
      private static final long serialVersionUID = 7787296644371759307L;

      public void buttonClick(ClickEvent event){
         
         try{
            Object rowSelected = table.getValue();
            String sCoordinates = (String) table.getContainerProperty(rowSelected, sColumn).getValue();
            
            String[] arrstrCoordinates = sCoordinates.split(","); 
            
            double latitude = Double.parseDouble(arrstrCoordinates[0].trim());
            double longitude = Double.parseDouble(arrstrCoordinates[1].trim());
            
            Label label = new Label("<iframe width=\"900\" height=\"600\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"http://maps.google.com/?t=h&amp;ie=UTF8&amp;ll=" + latitude + "," + longitude + "&amp;spn=0.240329,0.583649&amp;z=10&amp;output=embed\"></iframe><br /><small><a href=\"http://maps.google.com/?t=h&amp;ie=UTF8&amp;ll=60,30&amp;spn=" + latitude + "," + longitude + "&amp;z=10&amp;source=embed\" style=\"color:#0000FF;text-align:left\">Enlaged Map</a></small>", Label.CONTENT_RAW);
            Label lbCross = new Label("<div style=\"position:absolute;left:450px;top:300px;font-size:50px;\"><font color=\"#00FF00\">+</font></div>", Label.CONTENT_RAW);
            
            
            Window windowMap= new Window("MAP");
            windowMap.setWidth("940px");
            windowMap.setHeight("700px");
            windowMap.setClosable(true);
            windowMap.setResizable(false);
  
            windowMap.addComponent(label);
            windowMap.addComponent(lbCross);
            
            table.getApplication().getMainWindow().addWindow(windowMap);
         }
         catch (Exception e){
            button.setComponentError(new UserError("Координаты должны быть в формате: долгота,широта"));
         }
      
//         application.getMainWindow().executeJavaScript(
//            "window.open('http://maps.google.com/?ll=" + tfLatitude.getValue() + "," + tfLongitude.getValue() + "&t=h&&z=10','Map','width=1000,height=1000,toolbar=1,resizable=yes')"
//         );      
      }
   }



   @Override
   public void changeFocus(){
      button.setComponentError(null);
      
      String sValue;
      try{
         sValue = "" + table.getContainerProperty(table.getValue(), sColumn).getValue();
         
         if("".equals(sValue)){
         }
         else{
            button.setEnabled(true);
            return;
         }
      }
      catch (Exception e){
      }
      
      button.setEnabled(false);
   }
}
