package org.opentripplanner.analyst;

import com.google.common.collect.Maps;
import org.opentripplanner.analyst.request.SampleFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class PointSetCache {

    private static final Logger LOG = LoggerFactory.getLogger(PointSetCache.class);

    public static final String PATH = "/var/otp/pointsets";

    public final Map<String, PointSet> pointSets = Maps.newHashMap();
    public final Map<String, SampleSet> sampleSets = Maps.newHashMap();

    private final SampleFactory sfac;

    public PointSetCache (SampleFactory sfac) {
        this.sfac = sfac;
        reload();
    }

    public PointSet get(String id) {
        return pointSets.get(id);
    }

    public void reload() {
        LOG.info("Loading all PointSets in directory '{}'", PATH);
        File dir = new File(PATH);
        if ( ! (dir.isDirectory() && dir.canRead())) {
            LOG.error("'{}' is not a readable directory.", dir);
            return;
        }
        pointSets.clear();
        for (File file : dir.listFiles()) {
            String name = file.getName();
            if (name.endsWith(".csv")) {
                String baseName = name.substring(0, name.length() - 4);
                LOG.info("loading '{}' with ID '{}'", file, baseName);
                try {
                    PointSet pset = PointSet.fromCsv(file.getAbsolutePath());
                    if (pset == null) {
                        LOG.warn("skipping this pointset.");
                    }
                    pset.samples = new SampleSet(pset, sfac);
                    pointSets.put(baseName, pset);
                } catch (IOException ioex) {
                    LOG.warn("Exception while loading pointset: {}", ioex);
                }
            }
        }
    }

}

@@ -1543,16 +1543,25 @@
 G.warn(%22
+Failure, 
 skipping
@@ -1858,32 +1858,528 @@
  %7D%0A            %7D
+ else if (name.endsWith(%22.json%22)) %7B%0A                String baseName = name.substring(0, name.length() - 5);%0A                LOG.info(%22loading '%7B%7D' with ID '%7B%7D'%22, file, baseName);%0A                PointSet pset = PointSet.fromGeoJson(file.getAbsolutePath());%0A                if (pset == null) %7B%0A                    LOG.warn(%22Failure, skipping this pointset.%22);%0A                %7D%0A                pset.samples = new SampleSet(pset, sfac);%0A                pointSets.put(baseName, pset);%0A            %7D%0A
 %0A        %7D%0A    %7D

load geojson pointsets on server startup