﻿///
/// Code for handling the identification of data at a given point 
///


/// Locate a point on the map for the given lat/long and display using identify.js.showLocation
/// @param {number} inputLong The longitude for point
/// @param {number} inputLat The lattitude for point
function findByCoordinates(inputLong, inputLat){
  // Spatial reference for lat/long
  var spatialReference = new esri.SpatialReference({wkid: 4326});  var candidatePoint = new esri.geometry.Point(inputLong, inputLat, spatialReference);
  
  // Set the marker title
  markerSearchText = 'Lat/Long: ' + inputLat + ", " + inputLong;
  queryForCountyByPoint(esri.geometry.geographicToWebMercator(candidatePoint));
  // Convert the candidate point from wkid:4326 to WebMercator (for Bing)
  //showCountyInfoMarker(esri.geometry.geographicToWebMercator(candidatePoint));  
}

///
/// Process the returned candidates from Bing geocoder service
/// @param JSON inputCandidates The found candidates in the following structure
///
//  [
//      {
//          "address": {
//              "addressLine": "1835 Shackleford Ct",
//              "adminDistrict": "GA",
//              "countryRegion": "United States",
//              "formattedAddress": "1835 Shackleford Ct, Norcross, GA 30093-2949",
//              "locality": "Norcross",
//              "postalCode": "30093-2949"
//          },
//          "bestView": {
//              "xmin": -84.1609465833273,
//              "ymin": 33.9344112824293,
//              "xmax": -84.1485314166727,
//              "ymax": 33.9421367175707,
//              "spatialReference": {
//                  "wkid": 4326
//              }
//          },
//          "confidence": "high",
//          "displayName": "1835 Shackleford Ct, Norcross, GA 30093-2949",
//          "entityType": "Address",
//          "matchCodes": [
//              "Good"
//          ],
//          "locationArray": [
//              {
//                  "x": -84.154739,
//                  "y": 33.938274,
//                  "calculationMethod": "Parcel"
//              },
//              {
//                  "x": -84.154164,
//                  "y": 33.938558,
//                  "calculationMethod": "Interpolation"
//              }
//          ]
//      }
//  ]
function showBingCandidates(geocodeResults){
  
  // if only one result and it's good, this is our address
  // if multiple results, draw the first one and populate/switch to the results window
  if(geocodeResults.length == 1 && 
    geocodeResults[0].confidence == 'high' && 
    geocodeResults[0].matchCodes.length == 1 &&
    geocodeResults[0].matchCodes[0] == 'Good'){
    
    showAddressLocation(geocodeResults[0]);
    addressSearchResultsStore.removeAll();
  } else {
    addressSearchResultsStore.loadData(geocodeResults);

    //showAddressLocation(geocodeResults[0]);
    //locationSearchTabs.setActiveTab(2);
    Ext.MessageBox.alert(
      'Search', 
      'No exact match found. <br/>Review the results tab for possible matches or search again. <br/>'
    );
    locationSearchTabs.setActiveTab(4);
  }  
}

/// Creates the esri.geometry.Point for displaying the given address using identify.js.showLocation
/// @param {esri.virtualearth.VEGeocodeResult} candidate The address to show on the map
function showAddressLocation(candidate){
    
  // First we create the point defined in the return with its spatial reference
  var locationFound = false;
  var candidateSpatialReference = new esri.SpatialReference({wkid: 4326});
  var locX, locY;
  
  // Not all candidates contain an array, but typically they do
  if(candidate.locationArray != null){
    locX = candidate.locationArray[0].x;
    locY = candidate.locationArray[0].y;
  } else {
    locX = candidate.x;
    locY = candidate.y;
  }  
  
  var candidatePoint = new esri.geometry.Point(locX, locY, candidateSpatialReference);
  //var candidateGraphic = new esri.Graphic(candidatePoint, new esri.symbol.SimpleMarkerSymbol());
  //var attributes = { address: candidate.address.formattedAddress, score:candidate.confidence};
  
  ///
  /// In order to wrap long addresses, we need to split the formatted address
  /// Not all results have a formatted address, so we must check and test for one first
  ///
  if(candidate.address.formattedAddress != null){
    var splitAddress = candidate.address.formattedAddress.split(',');
    if ( splitAddress.length > 2 ) {
      Mapping.Ext.Esri.Marker.reset().setTitle(splitAddress[0] + '<br>' + splitAddress[1] + splitAddress[2]);
    } else { 
      Mapping.Ext.Esri.Marker.reset().setTitle(candidate.address.formattedAddress);
    }  
  } else {
    Mapping.Ext.Esri.Marker.reset().setTitle(candidate.address);
  }
  
  // Convert the candidate point from wkid:4326 to WebMercator (for Bing)
  //showLocation(esri.geometry.geographicToWebMercator(candidatePoint));
  queryForCountyByPoint(esri.geometry.geographicToWebMercator(candidatePoint))
}

