﻿/** @class Baseclass for line/polygon drawing tools
@param {Object} config configuration settings
@config {MapPanel} mapPanel a provider of mapclick, mapdoubleclick, and mapmousemove events
@config {VEMap} map the virtual earth map on which to draw a shape (used to convert pixel to LatLong)
@config {VEShapeLayer || VEMap} addShapeProvider function returning an object that implements AddShape (either a map or shapeLayer object)
*/
TNRIS.EditTool = function(config) {
    Ext.apply(this, config);
    TNRIS.EditTool.superclass.constructor.call(this);
};

Ext.extend(TNRIS.EditTool, TNRIS.ToolBase,
    /** @scope TNRIS.DrawingTool */
    {
    
    getKeyListener: function() {
        if (null == this.keyListener) {
            this.keyListener = this.mapPanel.getEl().addKeyListener(Ext.EventObject.DELETE, this.onDelete, this);
            this.keyListener.disable();
        }
        return this.keyListener;
    },
    
    activate: function() {
        TNRIS.EditTool.superclass.activate.call(this);
        this.mapPanel.on({
            'beforemapclick': {fn: this.onClick, scope: this},
            'beforemapdrag': {fn: this.onMapDragBegin, scope: this},
            'mapdragging': {fn: this.onMapDrag, scope: this},
            'mapdragdone': {fn: this.onMapDragDone, scope: this}
        });
        this.getKeyListener().enable();
        this.setInfoBubbleDelay(500); //info bubble pop up delay
    },
    
    deactivate: function() {
        TNRIS.EditTool.superclass.deactivate.call(this);
        this.mapPanel.removeListener('beforemapclick', this.onClick, this);
        this.mapPanel.removeListener('beforemapdrag', this.onMapDragBegin , this);
        this.mapPanel.removeListener('mapdragging', this.onMapDrag, this);
        this.mapPanel.removeListener('mapdragdone', this.onMapDragDone, this);
        this.getKeyListener().disable();
        this.clearInfoBubbleDelay();
    },
    
    onClick: function(mapEvent) {
        if (null != this.contextClickActivity){
            TNRIS.EditTool.superclass.doContextClickActivity.call(this,mapEvent);
            return false;
        }

        if (mapEvent.elementID == null) {
            if (!mapEvent.shiftKey) {
                this.mapPanel.clearSelections();
            }
            return true;
        }
       
        var shape = mapEvent.elementID;
        
        if (mapEvent.shiftKey) {
            this.mapPanel.toggleSelectShape(shape);
        } else {
            this.mapPanel.clearSelections();
            this.mapPanel.selectShape(shape);
        }
        return false;
    },
    
    onMapDragBegin: function(mapEvent) {
        if (mapEvent.elementID == null) {
            // TODO: only drag if one of the selected shapes is under the mouse
            return false;
        }
        this.draggedShapes = this.mapPanel.getSelectedShapes();
        return false;
    },
    
    onMapDrag: function(mapEvent, delta) {
        if (this.draggedShapes != null && this.draggedShapes.length > 0) {
            Ext.each(this.draggedShapes, function(shape) {
                var points = [];
                Ext.each(shape.GetPoints(), function(point) {
                    points.push(new VELatLong(point.Latitude + delta.latitude, point.Longitude + delta.longitude));
                }, this);
                shape.SetPoints(points);
            }, this);
            return false;
        } else {
            return true;
        }
    },
    
    onMapDragDone: function(mapEvent, delta) {
        this.onMapDrag(mapEvent, delta);
        this.mapPanel.modifySelectedShapes();
        this.draggedShapes = [];
    },
    
    onDelete: function(e) {
        this.mapPanel.deleteSelectedShapes();
    }
});

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }
