package com.gurkensalat.osm.mosques;

import com.gurkensalat.osm.entity.DitibPlace;
import com.gurkensalat.osm.repository.DitibPlaceRepository;
import org.apache.commons.lang3.text.ExtendedMessageFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Locale;

@Controller
@EnableAutoConfiguration
public class MapController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(MapController.class);

    private static final String MEDIATYPE_JAVASCRIPT = "application/javascript";

    @Autowired
    private DitibPlaceRepository ditibPlaceRepository;


    @RequestMapping("/map")
    String map(Model model)
    {
        StringBuffer result = new StringBuffer();
        result.append("\n");

        Iterable<DitibPlace> places = ditibPlaceRepository.findAll();
        model.addAttribute("places", places);

        ExtendedMessageFormat mf = new ExtendedMessageFormat("[{0}, {1}, \"{2}\"]", Locale.ENGLISH);

        result.append("var addressPoints = [");
        result.append("\n");

        for (DitibPlace place : places)
        {
            // String popupHtml = "'<b>" + place.getName() + "<br/>" + place.getAddress().getCity() + "</b>'";
            String popupHtml = place.getAddress().getCity() + " / " + place.getName();

            StringBuffer currentLine = new StringBuffer();
            result.append(mf.format(new Object[]{place.getLat(), place.getLon(), popupHtml}));
            result.append(",\n");
        }

        result.append(mf.format(new Object[]{48.1364, 11.3872928, "Germering / OSM"}));
        result.append("\n");
        result.append("]\n");

        model.addAttribute("placemarkers_as_js", result.toString());
        return "map/index";
    }
}

@@ -108,24 +108,126 @@
 osm.
-repository.Ditib
+entity.OsmPlace;%0Aimport com.gurkensalat.osm.repository.DitibPlaceRepository;%0Aimport com.gurkensalat.osm.repository.Osm
 Plac
@@ -974,16 +974,82 @@
 itory;%0A%0A
+    @Autowired%0A    private OsmPlaceRepository osmPlaceRepository;%0A
 %0A    @Re
@@ -1187,125 +1187,8 @@
 );%0A%0A
-        Iterable%3CDitibPlace%3E places = ditibPlaceRepository.findAll();%0A        model.addAttribute(%22places%22, places);%0A%0A
     
@@ -1399,14 +1399,38 @@
 e : 
-places
+ditibPlaceRepository.findAll()
 )%0A  
@@ -1452,11 +1452,8 @@
     
- //
  Str
@@ -1472,84 +1472,273 @@
 l = 
-%22'%3Cb%3E%22 + place.getName() + %22%3Cbr/%3E%22 + place.getAddress().getCity() + %22%3C/b%3E'%22;
+place.getAddress().getCity() + %22 / %22 + place.getName();%0A            result.append(mf.format(new Object%5B%5D%7Bplace.getLat(), place.getLon(), popupHtml%7D));%0A            result.append(%22,%5Cn%22);%0A        %7D%0A%0A        for (OsmPlace place : osmPlaceRepository.findAll())%0A        %7B
 %0A   
@@ -1756,32 +1756,43 @@
 ring popupHtml =
+ %22OSM / %22 +
  place.getAddres
@@ -1835,68 +1835,8 @@
 e();
-%0A%0A            StringBuffer currentLine = new StringBuffer();
 %0A   

Put OSM places on the same map