package com.gurkensalat.osm.mosques;

import com.gurkensalat.osm.entity.DitibParsedPlace;
import com.gurkensalat.osm.entity.DitibPlace;
import com.gurkensalat.osm.entity.Place;
import com.gurkensalat.osm.repository.DitibParserRepository;
import com.gurkensalat.osm.repository.DitibPlaceRepository;
import org.apache.commons.lang3.CharEncoding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

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

    private final static String REQUEST_ROOT = "/ditib";

    private final static String REQUEST_IMPORT = REQUEST_ROOT + "/import";

    @Autowired
    private DitibParserRepository ditibParserRepository;

    @Autowired
    private DitibPlaceRepository ditibPlaceRepository;

    @Value("${ditib.data.location}")
    private String dataLocation;

    @RequestMapping(value = REQUEST_ROOT, produces = "application/hal+json")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<String> allMethods()
    {
        return new ResponseEntity<String>("{ '_links': {} }", null, HttpStatus.OK);
    }

    @RequestMapping(value = REQUEST_IMPORT, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<String> importData()
    {
        return importData(null);
    }

    @RequestMapping(value = REQUEST_IMPORT + "/{path}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<String> importData(@PathVariable("path") String path)
    {
        LOGGER.info("About to import OSM data from {} / {}", dataLocation, path);

        File dataDirectory = new File(dataLocation);
        if (path != null)
        {
            try
            {
                path = URLDecoder.decode(path, CharEncoding.UTF_8);
            }
            catch (UnsupportedEncodingException e)
            {
                LOGGER.error("While decoding optional path", e);
            }

            // TODO sanitize data directory first...
            dataDirectory = new File(dataDirectory, path);
        }

        LOGGER.info("DITIB Parser Repository is: {}", ditibParserRepository);

        List<DitibParsedPlace> parsedPlaces = new ArrayList<DitibParsedPlace>();

        for (int i = 1; i < 9; i++)
        {
            String dataFileName = "ditib-germany-page-" + i + ".html";
            File dataFile = new File(dataDirectory, dataFileName);

            parsedPlaces.addAll(ditibParserRepository.parse(dataFile));
        }

        LOGGER.info("DITIB Place Repository is: {}", ditibPlaceRepository);

        // Empty the storage first
        ditibPlaceRepository.deleteAll();

        int parsedPlaceNumber = 10000;
        for (DitibParsedPlace parsedPlace : parsedPlaces)
        {
            parsedPlaceNumber++;
            String key = "D-" + Integer.toString(parsedPlaceNumber).substring(1);

            // TODO create a key class, base it off the PLZ - DitibKey.valueOf(String)

            DitibPlace tempPlace = new DitibPlace(key);

            // Now, insert-or-update the place
            try
            {
                DitibPlace place = null;
                List<Place> places = null; // ditibPlaceRepository.findByNameAndType(tempPlace.getName(), tempPlace.getType());
                if ((places == null) || (places.size() == 0))
                {
                    // Place could not be found, insert it...
                    place = ditibPlaceRepository.save(tempPlace);
                }
                else
                {
                    // take the one from the database and update it
                    // place = places.get(0);
                    // place = ditibPlaceRepository.findOne(place.getId());
                }

                place.setName(parsedPlace.getName());
                place.setPostcode(parsedPlace.getPostcode());
                place.setCity(parsedPlace.getCity());
                place.setDitibCode(parsedPlace.getDitibCode());
                place.setPhone(parsedPlace.getPhone());
                place.setFax(parsedPlace.getFax());

                place = ditibPlaceRepository.save(place);

                LOGGER.info("Saved Place {}", place);
            }
            catch (Exception e)
            {
                LOGGER.info("While persisting place", e);
                LOGGER.info("Place:");
                LOGGER.info("    name: '{}'", tempPlace.getName());
            }
        }

        return new ResponseEntity<String>("Done Massa", null, HttpStatus.OK);
    }
}

@@ -3219,24 +3219,219 @@
 FileName);%0A%0A
+            File splitDirectory = new File(dataDirectory, %22ditib-germany-split-%22 + i);%0A            splitDirectory.mkdirs();%0A            ditibParserRepository.prettify(splitDirectory, dataFile);%0A%0A
             
@@ -3966,17 +3966,16 @@
 String)%0A
-%0A
         

Split up the full input data too