﻿
var DrawingControl = {
    drawBoundingBox: function(t_bbox, t_drawingGroupId) {
        var t_map = g_kartController.getMap();
        var t_canvas = t_map.getDrawCanvas();
        if (t_canvas == null) {
            t_canvas = new WAPICanvas();
            t_map.addDrawCanvas(t_canvas);
        }
        t_canvas.clear(t_drawingGroupId);
        var t_line = new PolyLine([], { 'rgba': "rgba(0,0,0,1)", 'lineWidth': 2 });
        t_line.addPoint(new Coordinate(t_bbox.left, t_bbox.bottom));
        t_line.addPoint(new Coordinate(t_bbox.left, t_bbox.top));
        t_line.addPoint(new Coordinate(t_bbox.right, t_bbox.top));
        t_line.addPoint(new Coordinate(t_bbox.right, t_bbox.bottom));
        t_line.addPoint(new Coordinate(t_bbox.left, t_bbox.bottom));

        t_canvas.addPolyLine(t_line, t_drawingGroupId);
    },

    drawBoundingBoxFromValues: function(t_top, t_bottom, t_left, t_right, t_drawingGroupId) {
        var t_map = g_kartController.getMap();
        var t_canvas = t_map.getDrawCanvas();
        if (t_canvas == null) {
            t_canvas = new WAPICanvas();
            t_map.addDrawCanvas(t_canvas);
        }
        
        t_canvas.clear(t_drawingGroupId);
        var t_line = new PolyLine([], { 'rgba': "rgba(0,0,0,1)", 'lineWidth': 2 });
        t_line.addPoint(new Coordinate(t_left, t_bottom));
        t_line.addPoint(new Coordinate(t_left, t_top));
        t_line.addPoint(new Coordinate(t_right, t_top));
        t_line.addPoint(new Coordinate(t_right, t_bottom));
        t_line.addPoint(new Coordinate(t_left, t_bottom));

        t_canvas.addPolyLine(t_line, t_drawingGroupId);
    },

    drawPolygon: function(t_coordinates, t_drawingGroupId) {
        var t_map = g_kartController.getMap();
        var t_canvas = t_map.getDrawCanvas();
        if (t_canvas == null) {
            t_canvas = new WAPICanvas();
            t_map.addDrawCanvas(t_canvas);
        }
        t_canvas.clear(t_drawingGroupId);
        t_line = new PolyLine([], { 'rgba': "rgba(200,0,0,1)", 'lineWidth': 2 });

        var strArrPair = t_coordinates.toString().split(',');

        var i = 0, t_length = strArrPair.length;
        while (i < t_length - 1) {
            var north = strArrPair[i];
            i++;
            var east = strArrPair[i];
            i++;

            t_line.addPoint(new Coordinate(east, north));
        }

        t_canvas.addPolyLine(t_line, t_drawingGroupId);
    },

    drawCoordinatePolygon: function(t_coordinates, t_options) {
        var t_width = $ifDefined(t_options.width, 3);    
        var t_close = $ifDefined(t_options.closePolygon, false);
        var t_drawingGroupId = $ifDefined(t_options.groupName, "polygon");
        
        var t_red = 0, t_green = 0, t_blue = 0, t_alpha = 128;
        if($defined(t_options.color))
        {
            var t_color = t_options.color;
            t_red = $ifDefined(t_color.r, 0);
            t_green = $ifDefined(t_color.g, 0);
            t_blue = $ifDefined(t_color.b, 0);
            t_alpha = $ifDefined(t_color.a, 128);
        }
        
        var t_map = g_kartController.getMap();
        var t_canvas = t_map.getDrawCanvas();
        if (t_canvas == null) {
            t_canvas = new WAPICanvas();
            t_map.addDrawCanvas(t_canvas);
        }

        t_line = new PolyLine([], { 'rgba': "rgba(" + t_red + "," + t_green + "," + t_blue + "," + t_alpha + ")", 'lineWidth': t_width });

        var i = 0, t_length = t_coordinates.length;
        while (i < t_length) {
            t_line.addPoint(t_coordinates[i]);
            i++;
        }

        if (t_close) {
            t_line.addPoint(t_coordinates[0]);
        }
        t_canvas.addPolyLine(t_line, t_drawingGroupId);
    },

    drawSemiClosedPolygon: function(t_coordinates, t_drawingGroupId) {
        //Checks if the polygon is valid
        var t_mouseCoordinate = t_coordinates[t_coordinates.length - 1];
        t_coordinates.splice(t_coordinates.length - 1, 1);
        if (!this.isPolygonValid(t_coordinates, t_mouseCoordinate)) {
            alert("Det er ikke lov med kryssende linjer i polygonet.\r\nKlikk et annet sted i kartet.");
            return;
        }
        t_coordinates.push(t_mouseCoordinate);

        //Draws the polygon
        var t_map = g_kartController.getMap();
        var t_canvas = t_map.getDrawCanvas();
        if (t_canvas == null) {
            t_canvas = new WAPICanvas();
            t_map.addDrawCanvas(t_canvas);
        }
        t_canvas.clear(t_drawingGroupId);

        var t_polyLine = new PolyLine([], { 'rgba': "rgba(0,0,0,1)", 'lineWidth': 2 });

        var i, t_length = t_coordinates.length;
        for (i = 0; i < t_length; i++) {
            t_polyLine.addPoint(t_coordinates[i]);
        }

        t_canvas.addPolyLine(t_polyLine,
            {
                group: t_drawingGroupId,
                supressRedraw: false
            });

        //"Closes" the polygon by drawing a line from the start coordinate to the end coordinate
        if (t_coordinates.length > 2) {
            var t_closingLine = new PolyLine([], { 'rgba': "rgba(0,0,0,1)", 'lineWidth': 0.5 });
            t_closingLine.addPoint(t_coordinates[0]);
            t_closingLine.addPoint(t_coordinates[t_coordinates.length - 1]);
            t_canvas.addPolyLine(t_closingLine,
                {
                    group: t_drawingGroupId,
                    supressRedraw: false
                });
        }
    },

    drawMouseMovedLine: function(t_coordinates, t_drawingGroupId, t_mouseCoord) {
        var t_map = g_kartController.getMap();
        var t_canvas = t_map.getDrawCanvas();
        if (t_canvas == null) {
            t_canvas = new WAPICanvas();
            t_map.addDrawCanvas(t_canvas);
        }
        t_canvas.clear(t_drawingGroupId);

        var t_polyLine = new PolyLine([], { 'rgba': "rgba(0,0,0,1)", 'lineWidth': 2 });

        var i, t_length = t_coordinates.length;
        for (i = 0; i < t_length; i++) {
            t_polyLine.addPoint(t_coordinates[i]);
        }

        t_canvas.addPolyLine(t_polyLine,
            {
                group: t_drawingGroupId,
                supressRedraw: false
            });

        var isPolygonValid = this.isPolygonValid(t_coordinates, t_mouseCoord);

        //Find the right colour of the pen used to draw the new lines
        var penColour = "rgba(0,0,0,1)";
        if (!isPolygonValid) {
            penColour = "rgba(255,0,0,1)";
        }

        //Line to mousepointer
        if (t_coordinates.length > 0) {
            var t_mouseMovedLine = new PolyLine([], { 'rgba': penColour, 'lineWidth': 0.5 });
            t_mouseMovedLine.addPoint(t_coordinates[t_coordinates.length - 1]);
            t_mouseMovedLine.addPoint(t_mouseCoord);
            t_canvas.addPolyLine(t_mouseMovedLine,
                {
                    group: t_drawingGroupId,
                    supressRedraw: false
                });
        }

        //New line to start 
        if (t_coordinates.length > 1) {
            var t_closingLine = new PolyLine([], { 'rgba': penColour, 'lineWidth': 0.5 });
            t_closingLine.addPoint(t_coordinates[0]);
            t_closingLine.addPoint(t_mouseCoord);
            t_canvas.addPolyLine(t_closingLine,
        {
            group: t_drawingGroupId,
            supressRedraw: false
        });
        }

        //Old line to start 
        if (t_coordinates.length > 1) {
            var t_closingLine = new PolyLine([], { 'rgba': "rgba(0,0,0,1)", 'lineWidth': 1 });
            t_closingLine.addPoint(t_coordinates[0]);
            t_closingLine.addPoint(t_coordinates[t_coordinates.length - 1]);
            t_canvas.addPolyLine(t_closingLine,
            {
                group: t_drawingGroupId,
                supressRedraw: false
            });
        }

    },

    //Check if linear lines, Y = aX + b, do cross each other. If so, return true. Else return false.
    //If the lines start or stop in the same coordinate the function return false. 
    checkIfLinesCrossEachOther: function(t_line1Coord1, t_line1Coord2, t_line2Coord1, t_line2Coord2) {
        //Check if some of the coorinates in the start or end of the lines are similar. That case will not
        //give real crossing lines.
        if (
               this.coordinatesAreEqual(t_line1Coord1, t_line2Coord1)
            || this.coordinatesAreEqual(t_line1Coord1, t_line2Coord2)
            || this.coordinatesAreEqual(t_line1Coord2, t_line2Coord1)
            || this.coordinatesAreEqual(t_line1Coord2, t_line2Coord2)
        )
            return false;

        //Find how steep the line is (a)
        var t_line1a = (t_line1Coord2.y - t_line1Coord1.y) / (t_line1Coord2.x - t_line1Coord1.x);
        var t_line2a = (t_line2Coord2.y - t_line2Coord1.y) / (t_line2Coord2.x - t_line2Coord1.x);

        //Find the y-value where the line is crossing y (b)
        var t_line1b = t_line1Coord1.y - (t_line1a * t_line1Coord1.x);
        var t_line2b = t_line2Coord1.y - (t_line2a * t_line2Coord1.x);

        //Find the x-value of where the lines are crossing
        var t_x = (t_line2b - t_line1b) / (t_line1a - t_line2a);

        //Find the y-value of where the lines are crossing
        var t_y = t_line1a * t_x + t_line1b;

        //Check if the cross is between the two coordinates of each line
        var isCrossingLine1 = false;
        var isCrossingLine2 = false;

        if (t_line1Coord2.x > t_line1Coord1.x) {
            if (t_x < t_line1Coord2.x && t_x > t_line1Coord1.x)
                isCrossingLine1 = true;
        }
        else {
            if (t_x < t_line1Coord1.x && t_x > t_line1Coord2.x)
                isCrossingLine1 = true;
        }

        if (t_line2Coord2.x > t_line2Coord1.x) {
            if (t_x < t_line2Coord2.x && t_x > t_line2Coord1.x)
                isCrossingLine2 = true;
        }
        else {
            if (t_x < t_line2Coord1.x && t_x > t_line2Coord2.x)
                isCrossingLine2 = true;
        }

        return isCrossingLine1 && isCrossingLine2;
    },

    coordinatesAreEqual: function(t_coordinate1, t_coordinate2) {
        if (t_coordinate1.x == t_coordinate2.x && t_coordinate1.y == t_coordinate2.y)
            return true;
        return false;
    },

    isPolygonValid: function(t_coordinates, t_mouseCoord) {
        //Check if line to mousepointer and new line to start will cross the other lines.
        //If that is the case, the polygon is not valid
        var isCrossingLineToPointer = false;
        for (i = 1; i < t_coordinates.length; i++) {
            if (this.checkIfLinesCrossEachOther(t_coordinates[i - 1], t_coordinates[i], t_coordinates[t_coordinates.length - 1], t_mouseCoord)) {
                isCrossingLineToPointer = true;
                break;
            }
        }

        //Check if the new line to start crosses the other lines. 
        //If that is the case, the polygon is not valid
        var isCrossingLineToStart = false;
        for (i = 1; i < t_coordinates.length; i++) {
            if (this.checkIfLinesCrossEachOther(t_coordinates[i - 1], t_coordinates[i], t_coordinates[0], t_mouseCoord)) {
                isCrossingLineToStart = true;
                break;
            }
        }

        if (isCrossingLineToPointer == true || isCrossingLineToStart == true) return false;
        return true;
    },

    closePolygon: function(t_coordinates, t_drawingGroupId) {
        if (t_coordinates.length > 2) {
            var t_map = g_kartController.getMap();
            var t_canvas = t_map.getDrawCanvas();
            if (t_canvas == null) {
                t_canvas = new WAPICanvas();
                t_map.addDrawCanvas(t_canvas);
            }

            var t_closingLine = new PolyLine([], { 'rgba': "rgba(0,0,0,1)", 'lineWidth': 2 });
            t_closingLine.addPoint(t_coordinates[0]);
            t_closingLine.addPoint(t_coordinates[t_coordinates.length - 1]);
            t_canvas.addPolyLine(t_closingLine, t_drawingGroupId);
        }
    },

    removeDrawingGroup: function(t_drawingGroupId) {
        var t_map = g_kartController.getMap();
        var t_canvas = t_map.getDrawCanvas();
        if (t_canvas != null) {
            t_canvas.clear(t_drawingGroupId);
        }
    }
}