﻿/** @class Base Class for tools (LineTool,PolygonTool,EditTool,LineTool,PanMapTool)
@constructor 
@param {Configuration} config the constructor is empty for the time being
*/
TNRIS.ToolBase = function(config) {
    Ext.apply(this, config);
    TNRIS.ToolBase.superclass.constructor.call(this);
    this.infoBubbleDelayTimer = new Ext.util.DelayedTask();
};

Ext.extend(TNRIS.ToolBase,Ext.Component, 
    /** @scope TNRIS.ToolBase */
    {
    /** Start to implement the tool behavior - connect listeners, etc */
    activate: function() {
        this.mapPanel.on({
            'mapmouseover': {fn: this.onMouseOver, scope: this},
            'mapmouseout' : {fn: this.onMouseOut, scope:this},
            'mapcontextclick': {fn: this.onContextClick, scope: this}
        });
        this.infoBoxPopUpDelay = 0;
    },
    
    /** No longer implement the tool action - disconnect listeners, etc */
    deactivate: function() {
        this.mapPanel.removeListener('mapmouseover', this.onMouseOver, this);
        this.mapPanel.removeListener('mapmouseout', this.onMouseOver, this);
        this.mapPanel.removeListener('mapcontextclick', this.onContextClick, this);
        this.mapPanel.clearCustomCursor();
    },
    
    /** show description for polygon/polyline shapes without requiring an icon */
    onMouseOver: function(mapEvent) {
        if (mapEvent.elementID != null) {        
            var map = this.mapPanel.getMap();
            var shape = map.GetShapeByID(mapEvent.elementID);
            map.HideInfoBox();
            if (null != shape && shape.GetType() != "Point") {
                this.infoBubbleDelayTimer.delay (this.infoBubbleDelay, function () {            
                    map.ShowInfoBox(shape, new VEPixel(mapEvent.mapX, mapEvent.mapY));
                },this);
            }
        }
        return true;
    },
    
    onMouseOut: function(mapEvent) {
        /* below code commented as shape can not be hidden on mouse out as the user can not edit the shape information
        if (mapEvent.elementID != null) {
            var map = this.mapPanel.getMap();
            var shape = map.GetShapeByID(mapEvent.elementID);
            if (null != shape && shape.GetType() != "Point") {
                map.HideInfoBox(shape);
            }
        } */
        this.infoBubbleDelayTimer.cancel();
        return true;
    },
    
    onContextClick: function (e) {
        this.menu = Ext.getCmp('map-context-menu');
        if (! this.menu) {
            this.menu = new Ext.menu.Menu({
                id:"map-context-menu",
                cls: "map-context-menu",
                items:[
                    {
                        id:"Menu-findNearestAddress", 
                        icon: '../images/buttons/find_address.png',
                        text:"Find Nearest Address",
                        cls: 'mapPanelContextMenu',
                        scope: this, 
                        handler:function (btn, e) {
                            this.contextClickActivity = "FindAddress";
                            this.oldCursor = this.mapPanel.getCustomCursor();
                            this.mapPanel.setCustomCursor('crosshair');
                            btn.parentMenu.hide();
                        }
                    },
                    {
                        id:"Menu-copyLatLongToClipBoard",
                        icon: '../images/buttons/copy_to_clipboard.png',
                        text:"Copy Coordinates To Clipboard", 
                        cls: 'mapPanelContextMenu',
                        scope: this, 
                        handler:function (btn, e) {
                            this.contextClickActivity = "ClipBoardCopy";
                            this.oldCursor = this.mapPanel.getCustomCursor();
                            this.mapPanel.setCustomCursor('crosshair');
                            btn.parentMenu.hide();
                        }
                    }
                ]
            });
            this.menu.on('hide', this.onContextHide, this);
        }
        this.ctxLatLong = this.eventToLatLong(e);
        this.menu.showAt([e.mapX, e.mapY]);
        return true;
    },
    
    doContextClickActivity: function(mapEvent){
        var point = this.eventToLatLong(mapEvent);

        switch(this.contextClickActivity) {
            case "FindAddress":
                this.findAddress(point);
                break;
            case "ClipBoardCopy":
                this.copyLatLongToClipboard(point);
                break;
            default:
                break;
        }
        this.contextClickActivity = null;
        this.setOldCursor();
    },
    
    setOldCursor: function(){
        if (null != this.oldCursor) {
            this.mapPanel.setCustomCursor(this.oldCursor);
            this.oldCursor = null;
        } else {
            this.mapPanel.clearCustomCursor();
        }
    },
    
    findAddress: function(point) {
        this.mapPanel.getMap().FindLocations(point, function(places) {
            if (null != places && places.length > 0) {
                this.mapPanel.addLocation(places[0]);          
            } else {
                Ext.Msg.alert('', 'There were no addresses found at that location.  Please try again.');
            }
        }.createDelegate(this));
    },
    
    copyLatLongToClipboard: function(point) {
        var textValue = point.Latitude + " , " + point.Longitude;
        try{
            window.clipboardData.setData('text',textValue);
            gemss.app.msg('','Coordinates copied to clipboard',textValue);
        }catch(err){
            Ext.Msg.alert('Error',
            'Sorry! This feature is not supported in this broswer.<br/>Firefox users please refer http://www.mozilla.org/editor/midasdemo/securityprefs.html for details');
        }
    },
    
    onContextHide: function() {
        this.ctxLatLong = null;
    },

    eventToLatLong: function(mapEvent) {
        var map = this.mapPanel.getMap();
        return map.PixelToLatLong(new VEPixel(mapEvent.mapX, mapEvent.mapY));
    },

    /** sets delay interval after which the info bubble pops up when the user move the mouse over a shape */
    setInfoBubbleDelay: function(value) {
        if (null != value) {
            this.infoBubbleDelay = value;    
        }
    },
    
    clearInfoBubbleDelay: function() {
        this.infoBubbleDelayTimer.cancel();
        this.infoBubbleDelay = 0;
    },
    
    setCursor: function(value,shiftKeyDragging) {
        this.mapPanel.setCustomCursor(value,shiftKeyDragging);
    }
});

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }
