﻿/*
footprintcount.js handles the logic for executing footprint counts and processing the results
*/

var _displayResults = false; //Bool - should footprint count be displayed?


/// <summary>
/// Initiates the counting of footprints within the current viewport
/// <param name="displayResults">If true, then result count is displayed in Map Alert window</param>
/// </summary>
/// <returns>null</returns>
function GetFootprintCountByEnvelope(displayResults)
{
    try {
        //Display Loading GIF
        //ShowLoadingImage();
        
        //Formulate Rest Request Parameters based on Search Preferences.
        _displayResults = displayResults; //Set whether or not we should display footprint count.
        var bounds = _map.getBounds();
        var returnGeometry = false;
        var whereClause = "";
        var outFields = ["FILENAME"];
        ExecuteFootprintQuery(bounds, whereClause, returnGeometry, outFields);
     }
     catch (e)
     {
        ErrorAlert(ProcessingSearchResultsError);
     }
}


/// <summary>
/// Executes the REST query to count footprints in current viewport
/// <param name="geometry">The Google geometry (point.getLatLng(), GPolygon, GPolyline, GLatLonBounds) </param>
/// <param name="whereClause">SQL Where clause to filter results</param>
/// <param name="returnGeometry">Boolean, do you want geometry back? </param>
/// <param name="outFields">Comma separated list of fields to return from query</param>
/// </summary>
/// <returns>null</returns>
function ExecuteFootprintQuery(geometry, whereClause, returnGeometry, outFields) 
{
    try {
        // clear gOverlays overlays and event listeners
        //_mapExtension.removeFromMap(_gOverlays);

        // set query parameters
        _query.queryGeometry = geometry;
        _query.returnGeometry = returnGeometry;
        _query.outFields = outFields; //["FILENAME"]

        // execute query task
        _qtask.execute(_query, false, ProcessFootprintCount);
     }
     catch (e)
     {
        ErrorAlert(ProcessingSearchResultsError);
     }
}


/// <summary>
/// Handles the callback containing Featureset from REST Request of Footprint count
/// <param name="featureSet">JSON passed back from ArcGIS Server</param>
/// </summary>
/// <returns>null</returns>
function ProcessFootprintCount(featureSet)
{
    //Set the global feature countd
    _footprintCount = featureSet.features.length;
    HandleFootPrintCountResults(featureSet.features.length);
    if(_displayResults)
    {
        DisplayFootprintCount(_footprintCount);
    }
    
}


/// <summary>
/// Handles the Footprint count results.  If there are none, say so.  If there are too many, disable map tools.
/// If within threshold, enable map search tools
/// <param name="footprintCount">The number of footprints in current extent</param>
/// </summary>
/// <returns>null</returns>
function HandleFootPrintCountResults(footprintCount)
{
        if(footprintCount == 0)
        {
            //Report to user that there are 0 results in this area.
            MapAlert("There are no results in this area.");
            return;
        }
        else if(footprintCount >= _footprintCountThreshold)
        {
            //Report to user that there are too many results to display - zoom in
            MapAlert("There are too many results in your current Extent.  Please zoom in to use the <b>Extent</b> tool.");
            return;
        }
        else
        {
            //OK to Search - Enable Search Tools.
            MapAlert("Use buttons above to search catalog.");
            //TODO:  Enable Search Buttons.
            
        }
}


/// <summary>
/// Displays Footprint count in Map Alert Window
/// <param name="count">The number of footprints in the current viewport</param>
/// </summary>
/// <returns>null</returns>
function DisplayFootprintCount(count)
{
    MapAlert("There are " + count + " footprints in the current view.");
}