﻿TNRIS.HISCentral = function(config) {
    Ext.applyIf(this, {
        shapeLineColor: new VEColor(0,150,150,0.6),
        shapeFillColor: new VEColor(0,150,150,0.6),
        shapeIcon: '<div class="hurrtraxPushpin"><img src="images/pushPins/greenGlobePin.png" /></div>'
    });
    
    TNRIS.HISCentral.superclass.constructor.call(this, config );
};

Ext.extend(TNRIS.HISCentral, TNRIS.ShapeLayer, {

    getUSPName: function (context) {
        return 'findHisCentralSpRecords';
    },

    //TODO: The grid is not functional
    queryable: function (value) {
        return 0;
    },

    getBaseParams: function () {
        return { layerId: this.layerId() };
    },

    refreshLayerData: function () {
        this.load(this.map());
    },

    load: function (map) {
        this.clearMapPoint();

        var viewRect = map.GetMapView();
        var filterOptions = this.getFilterOptions();
        this.getShapeStore().load({
            params: {
                envelope: [viewRect.TopLeftLatLong.Latitude, viewRect.TopLeftLatLong.Longitude, viewRect.BottomRightLatLong.Latitude, viewRect.BottomRightLatLong.Longitude],
                params: filterOptions.parameters,
                startDt: filterOptions.startDt,
                endDt: filterOptions.endDt
            }
        });
    },

    clearMapPoint: function () {
        var layer = this.getShapeLayer();
        var currentShapeIDs = [];
        for (i = 0; i < layer.GetShapeCount(); i++) {
            currentShapeIDs.push(layer.GetShapeByIndex(i).GetID());
        }

        Ext.each(currentShapeIDs, function (shapeId) {
            this.uncacheShape(shapeId);
            layer.DeleteShape(layer.GetShapeByID(shapeId));
        }, this);

    },

    smartable: function (value) {
        return false;
    },

    applyShapeStyling: function (shape, record) {
        var iconText = record.get('Record Count');
        if (iconText == 1) {
            shape.SetCustomIcon(this.getShapeIcon());
        } else {
            shape.SetCustomIcon('<div class="hurrtraxPushpin"><img src="images/pushPins/Chris_icon.gif" /></div>' + '<div class="iconText">' + iconText + '</div>');
        }
    },


    getShapeDescription: function (record) {
        var description = "";
        var recordCount = record.get('Record Count');
        if (recordCount == 1) {
            if (this.columnConfigs()) {
                this.sortColumnConfig();
                Ext.each(this.columnConfigs(), function (config) {
                    if (!config.isHidden) {
                        description = description + String.format('<div class="nameValuePair">{0}</div>', this.getShapeDescriptionField(config, record));
                    }
                }, this);
            } else {
                $H(record.data).each(function (entry) {
                    if (entry.key != "id" && entry.key != "shapeType" && entry.key != "layerId" && entry.key != "shapePoints" && entry.key != "name" && entry.key != "shapeId" && entry.value != null) {
                        description = description + '<div class="nameValuePair">' + '<span class="nameColumn">' + entry.key + ':</span><span class="valueColumn">' + entry.value + '</span></div>';
                    }
                    if (entry.key == "name") {
                        //description = description + '<div class="nameValuePair">' + '<span class="nameTitleColumn">' + 'Name:</span><span class="valueTitleColumn">' + entry.value + '</span></div>';
                    }
                } .bind(this));
            }
        } else {
            description = description = description + '<div class="nameValuePair">' + '<span class="nameColumn">' + "Record Count" + ':</span><span class="valueColumn">' + recordCount + '</span></div>';
        }

        description = description + this.getLinkButtonDescription(record);
        return description;
    },

    getLinkButtonDescription: function (record) {
        return "";
    },

    getOdmNetworkName: function () {
        return this.config.odmNetworkName;
    },

    getFilterOptions: function () {
        var filterOptions = Ext.getCmp('txHisCentralFilterWindow').getFilterOptions();
        return filterOptions;
    }
});
