package au.gov.amsa.gt;

import static java.lang.Double.parseDouble;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.junit.Test;

import au.gov.amsa.streams.Strings;

import com.google.common.collect.Lists;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

public class ShapefileTest {

    @Test
    public void testLoadShapefileFromFileSystem() {
        Shapefile shape = Shapefile.from(new File(
                "src/test/resources/shapefile-srr-polygon/srr.shp"));
        assertTrue(shape.contains(-20, 135));
        assertFalse(shape.contains(0, 0));
        assertEquals(4, shape.geometries().size());
        shape.close();

    }

    @Test
    public void testLoadShapefileFromZippedInputStream() throws Exception {
        Shapefile shape = Shapefile.fromZip(Shapefile.class
                .getResourceAsStream("/shapefile-srr-polygon.zip"));
        assertFalse(shape.contains(0, 0));
        assertEquals(4, shape.geometries().size());
        shape.close();
    }

    @Test
    public void testContainsInSrr() {
        // northern border is -12, 113
        Shapefile shape = Shapefile.fromZip(Shapefile.class
                .getResourceAsStream("/shapefile-srr-polygon.zip"));

        assertTrue(shape.contains(-13, 113));
        assertFalse(shape.contains(-10, 113));
    }

    @Test
    public void testShowDistanceBetweenPoints() {
        GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
        Point a = gf.createPoint(new Coordinate(-10, 114.0));
        Point b = gf.createPoint(new Coordinate(-11, 113.0));
        System.out.println("distance=" + a.distance(b));
    }

    @Test
    public void testContainsInSrrWithBuffer() {
        // northern border is -12, 113
        Shapefile shape = Shapefile.fromZip(
                Shapefile.class.getResourceAsStream("/shapefile-srr-polygon.zip"), 3);

        assertTrue(shape.contains(-13, 113));
        assertTrue(shape.contains(-10, 113));
        assertFalse(shape.contains(-8, 113));
    }

    @Test
    public void testCreatePolygonShapefile() throws IOException {
        try (InputStreamReader r = new InputStreamReader(
                Shapefile.class.getResourceAsStream("/southwest.txt"))) {
            // read coordinates from classpath
            List<Coordinate> coords = Strings.lines(r).map(line -> line.trim())
                    .filter(line -> line.length() > 0).filter(line -> line.charAt(0) != '#')
                    .doOnNext(System.out::println).map(line -> line.split(" "))
                    .map(items -> new Coordinate(parseDouble(items[0]), parseDouble(items[1])))
                    .toList().map(list -> {
                        List<Coordinate> list2 = Lists.newArrayList(list);
                        list2.add(new Coordinate(list.get(0).x, list.get(0).y));
                        return list2;
                    }).toBlocking().single();
            ShapefileCreator.createPolygon(coords, new File("target/southwest.shp"));
        }
    }

    @Test
    public void testToGeoJson() throws IOException {
        Shapefile shape = Shapefile.fromZip(
                Shapefile.class.getResourceAsStream("/shapefile-srr-polygon.zip"), 3);
        FileWriter writer = new FileWriter("target/srr.geojson");
        shape.writeGeoJson(writer, "EPSG:4326");
        writer.close();
    }

}

@@ -3688,10 +3688,355 @@
 %0A    %7D%0A%0A
+    public static void main(String%5B%5D args) throws IOException %7B%0A        Shapefile shape = Shapefile.from(new File(System.getProperty(%22user.home%22)%0A                + %22/temp/amb06_map_eez_pl.shp%22));%0A        FileWriter writer = new FileWriter(%22target/geojson-eez.txt%22);%0A        shape.writeGeoJson(writer, %22EPSG:3857%22);%0A        writer.close();%0A    %7D%0A
 %7D%0A

add main