package fi.nls.oskari.control;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.util.ResponseHelper;
import org.codehaus.jackson.map.ObjectMapper;
import org.postgis.LineString;
import org.postgis.MultiLineString;
import org.postgis.PGgeometry;
import org.postgis.Point;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@OskariActionRoute("CalculateRoute")
public class CalculateRouteHandler extends ActionHandler {
    private Map<String, Object> toGeoJSON(MultiLineString multiLineString) {
        Map<String, Object> ret = new HashMap<String, Object>();
        List<List<List<Double>>> coordinates = new ArrayList<List<List<Double>>>();
        for(int i = 0; i < multiLineString.getLines().length; ++i) {
            LineString lineString = multiLineString.getLine(i);
            List<List<Double>> lineStringCoordinates = new ArrayList<List<Double>>();
            for(int j = 0; j < lineString.numPoints(); ++j) {
                Point point = lineString.getPoint(j);
                List<Double> pointCoordinates = new ArrayList<Double>();
                pointCoordinates.add(point.x);
                pointCoordinates.add(point.y);
                lineStringCoordinates.add(pointCoordinates);
            }
            coordinates.add(lineStringCoordinates);
        }
        ret.put("type", "MultiLineString");
        ret.put("coordinates", coordinates);
        return ret;
    }

    @Override
    public void handleAction(ActionParameters params) throws ActionException {
        try {
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://localhost:5432/oskaridb";
            java.sql.Connection conn = DriverManager.getConnection(url, "oskari", "W3jept2MqqZRX4J");
            ObjectMapper mapper = new ObjectMapper();

    /*
    * Add the geometry types to the connection. Note that you
    * must cast the connection to the pgsql-specific connection
    * implementation before calling the addDataType() method.
    */
            ((org.postgresql.PGConnection) conn).addDataType("geometry", Class.forName("org.postgis.PGgeometry"));
            ((org.postgresql.PGConnection) conn).addDataType("box3d", Class.forName("org.postgis.PGbox3d"));
    /*
    * Create a statement and execute a select query.
    */
            Statement s = conn.createStatement();
            ResultSet r = s.executeQuery("select geom from tieviiva where gid = 10");
            PGgeometry geometry = null;
            while (r.next()) {
      /*
      * Retrieve the geometry as an object then cast it to the geometry type.
      * Print things out.
      */
                geometry = (PGgeometry) r.getObject(1);
                System.out.println(geometry.toString());
            }
            s.close();

            Statement routeStatement = conn.createStatement();
            ResultSet routeResult = routeStatement.executeQuery("" +
                    "select geom2d from hkiroads where gid in " +
                    "(select id2 from pgr_astar('select gid as id, cast(source as int4), cast(target as int4), cost, x1, y1, x2, y2 from hkiroads', 1736, 437, false, false))" +
                    "");
            List<LineString> routeLines = new ArrayList<LineString>();
            while (routeResult.next()) {
                PGgeometry linkGeometry = (PGgeometry) routeResult.getObject(1);
                routeLines.add((LineString)linkGeometry.getGeometry());
            }
            routeStatement.close();
            conn.close();

            LineString[] routeLineArray = new LineString[routeLines.size()];
            routeLines.toArray(routeLineArray);
            MultiLineString routeMultiLineString = new MultiLineString(routeLineArray);
            ResponseHelper.writeResponse(params, mapper.writeValueAsString(toGeoJSON(routeMultiLineString)));
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

@@ -1941,210 +1941,8 @@
 );%0A%0A
-    /*%0A    * Add the geometry types to the connection. Note that you%0A    * must cast the connection to the pgsql-specific connection%0A    * implementation before calling the addDataType() method.%0A    */%0A
     
@@ -2164,554 +2164,8 @@
 %22));
-%0A    /*%0A    * Create a statement and execute a select query.%0A    */%0A            Statement s = conn.createStatement();%0A            ResultSet r = s.executeQuery(%22select geom from tieviiva where gid = 10%22);%0A            PGgeometry geometry = null;%0A            while (r.next()) %7B%0A      /*%0A      * Retrieve the geometry as an object then cast it to the geometry type.%0A      * Print things out.%0A      */%0A                geometry = (PGgeometry) r.getObject(1);%0A                System.out.println(geometry.toString());%0A            %7D%0A            s.close();
 %0A%0A  
@@ -2512,17 +2512,16 @@
 s', 
-1736, 437
+265, 854
 , fa
@@ -2797,16 +2797,17 @@
 eString)
+ 
 linkGeom
@@ -3240,22 +3240,15 @@
    %7D
-%0A       
  catch
+ 
 (Exc

Remove obsolete code