package com.test.maps.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.maps.client.LoadApi;
import com.google.gwt.maps.client.MapOptions;
import com.google.gwt.maps.client.MapTypeId;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.base.LatLng;
import com.google.gwt.maps.client.base.LatLngBounds;
import com.google.gwt.maps.client.events.bounds.BoundsChangeMapEvent;
import com.google.gwt.maps.client.events.bounds.BoundsChangeMapHandler;
import com.google.gwt.maps.client.events.click.ClickMapEvent;
import com.google.gwt.maps.client.events.click.ClickMapHandler;
import com.google.gwt.maps.client.events.place.PlaceChangeMapEvent;
import com.google.gwt.maps.client.events.place.PlaceChangeMapHandler;
import com.google.gwt.maps.client.placeslib.*;
import com.google.gwt.user.client.ui.*;

import java.util.ArrayList;

public class Test implements EntryPoint {
    

    public void onModuleLoad() {

        Runnable onLoad = new Runnable() {
            @Override
            public void run() {
                RootPanel.get().add(new AutocompletePlacesWidget());

            }
        };
        loadMapApi(onLoad, "en");
    }

    public static void loadMapApi(Runnable runnable, String lang) {

        // load all the libs for use in the maps
        ArrayList<LoadApi.LoadLibrary> loadLibraries = new ArrayList<>();
        loadLibraries.add(LoadApi.LoadLibrary.DRAWING);
        loadLibraries.add(LoadApi.LoadLibrary.GEOMETRY);
        loadLibraries.add(LoadApi.LoadLibrary.PLACES);


        LoadApi.Language language = getLanguage(lang);
        LoadApi.go(runnable, loadLibraries, true, language);
    }

    public static LoadApi.Language getLanguage(String localeValue) {
        if (localeValue == null) {
            return LoadApi.Language.ENGLISH;
        }
        for (LoadApi.Language language : LoadApi.Language.values()) {
            if (language.getValue().equals(localeValue)) {
                return language;
            }
        }
        return LoadApi.Language.ENGLISH;
    }

    public class AutocompletePlacesWidget extends Composite {

        private VerticalPanel pWidget;

        private MapWidget mapWidget;

        private TextBox tbPlaces;

        public AutocompletePlacesWidget() {
            pWidget = new VerticalPanel();
            initWidget(pWidget);

            draw();
        }

        private void draw() {

            pWidget.clear();
            tbPlaces = new TextBox();
            tbPlaces.setWidth("350px");

            HorizontalPanel hp = new HorizontalPanel();
            hp.add(tbPlaces);

            pWidget.add(hp);

            hp.setCellVerticalAlignment(tbPlaces, HorizontalPanel.ALIGN_BOTTOM);

            drawMap();

            drawAutoComplete();
        }

        private void drawMap() {
            LatLng center = LatLng.newInstance(49.496675, -102.65625);
            MapOptions opts = MapOptions.newInstance();
            opts.setZoom(8);
            opts.setCenter(center);
            opts.setMapTypeId(MapTypeId.TERRAIN);

            mapWidget = new MapWidget(opts);
            pWidget.add(mapWidget);
            mapWidget.setSize("750px", "500px");

            mapWidget.addClickHandler(new ClickMapHandler() {
                public void onEvent(ClickMapEvent event) {
                    // TODO fix the event getting, getting ....
                    GWT.log("clicked on latlng=" + event.getMouseEvent().getLatLng());
                }
            });
        }

        private void drawAutoComplete() {

            Element element = tbPlaces.getElement();

            AutocompleteType[] types = new AutocompleteType[2];
            types[0] = AutocompleteType.ESTABLISHMENT;
            types[1] = AutocompleteType.GEOCODE;

            AutocompleteOptions options = AutocompleteOptions.newInstance();
            options.setTypes(types);
            options.setBounds(mapWidget.getBounds());

            final Autocomplete autoComplete = Autocomplete.newInstance(element, options);

            autoComplete.addPlaceChangeHandler(new PlaceChangeMapHandler() {
                public void onEvent(PlaceChangeMapEvent event) {

                    PlaceResult result = autoComplete.getPlace();

                    PlaceGeometry geomtry = result.getGeometry();
                    LatLng center = geomtry.getLocation();

                    mapWidget.panTo(center);
                    // mapWidget.setZoom(8);

                    GWT.log("place changed center=" + center);
                }
            });

            mapWidget.addBoundsChangeHandler(new BoundsChangeMapHandler() {
                public void onEvent(BoundsChangeMapEvent event) {
                    LatLngBounds bounds = mapWidget.getBounds();
                    autoComplete.setBounds(bounds);
                }
            });
        }
    }
}

