Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports ESRI.ArcGIS.ADF.Web
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls
Imports ESRI.ArcGIS.ADF.Web.DataSources
Imports ESRI.ArcGIS.ADF.Web.Geometry
Imports ESRI.ArcGIS.ADF.Web.Display.Graphics
Imports System.Collections.Generic



'/ <summary>
'/ Summary description for Identify
'/ </summary>
Public Class MapIdentify
  Private m_page As Page
  Private m_map As Map
  Private m_dataset As DataSet
  Private m_idOption As IdentifyOption = IdentifyOption.VisibleLayers
  Private m_callbackInvocation As String = ""
  Private m_filePath As String = ""
  Private m_resultsDisplay As TaskResults = Nothing
  Private m_numberDecimals As Integer = 3
  Public m_IdentifyTolerance As Integer = 5 ' tolerance used in identify request... may need to be adjusted to a specific resource type
  Public Sub New() '
  End Sub 'New


  Public Sub New(ByVal map As Map)
    If Not (map Is Nothing) Then
      m_map = map
      SetupIdentify()
    End If
  End Sub 'New


  Public Sub New(ByVal map As Map, ByVal filePath As String)
    m_map = map
    m_filePath = filePath
    SetupIdentify()
    'SetupHISID()
  End Sub 'New

  'Public Sub SetupHISID() '
  '  m_page = m_map.Page
  '  Dim sb As New System.Text.StringBuilder()
  '  m_callbackInvocation = m_page.ClientScript.GetCallbackEventReference(m_page, "message", "processCallbackResult", "context", True)
  '  sb.Append((ControlChars.Lf + "<script language=""javascript"" type=""text/javascript"" src=""" + m_filePath + "JavaScript/cuahsi.js"" ></script>" + ControlChars.Lf))
  '  'sb.Append("<link rel=\"Stylesheet\" href=\"" + m_filePath + "styles/IdentifyWindowStyleSheet.css\" type=\"text/css\" />\n");
  '  sb.Append(("<script language=""javascript"" type=""text/javascript"">var HISIDCallbackFunctionString = """ + m_callbackInvocation + """;</script>" + ControlChars.Lf))
  '  If Not m_page.ClientScript.IsClientScriptBlockRegistered("HISIDScript") Then
  '    m_page.ClientScript.RegisterClientScriptBlock(m_page.GetType(), "HISIDScript", sb.ToString())
  '  End If
  'End Sub 'SetupIdentify

  Public Sub SetupIdentify() '
    m_page = m_map.Page
    Dim sb As New System.Text.StringBuilder()
    m_callbackInvocation = m_page.ClientScript.GetCallbackEventReference(m_page, "message", "processCallbackResult", "context", True)
    sb.Append((ControlChars.Lf + "<script language=""javascript"" type=""text/javascript"" src=""" + m_filePath + "JavaScript/display_mapidentify.js"" ></script>" + ControlChars.Lf))
    'sb.Append("<link rel=\"Stylesheet\" href=\"" + m_filePath + "styles/IdentifyWindowStyleSheet.css\" type=\"text/css\" />\n");
    sb.Append(("<script language=""javascript"" type=""text/javascript"">var identifyCallbackFunctionString = """ + m_callbackInvocation + """;</script>" + ControlChars.Lf))
    If Not m_page.ClientScript.IsClientScriptBlockRegistered("IdentifyScript") Then
      m_page.ClientScript.RegisterClientScriptBlock(m_page.GetType(), "IdentifyScript", sb.ToString())
    End If
  End Sub 'SetupIdentify

  Public Function Identify(ByVal queryString As NameValueCollection) As String
    Dim xString As String = queryString("minx")
    Dim yString As String = queryString("miny")
    Dim locXString As String = ""
    Dim locYString As String = ""
    Dim notSupported As Boolean = False
    Dim x As Integer = Convert.ToInt32(xString)
    Dim y As Integer = Convert.ToInt32(yString)
    Dim resource As IGISResource
    Dim query As IQueryFunctionality
    Dim mapPoint As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(x, y, m_map.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap))
    'Dim mapPoint As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(x, y, m_map.Extent, m_map.TilingScheme.ViewWidth, m_map.TilingScheme.ViewHeight)
    Dim gdsList As New List(Of DataSet)
    Dim mapFunc As IMapFunctionality
    For Each mapFunc In m_map.GetFunctionalities()
      If Not TypeOf mapFunc Is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality Then
        resource = mapFunc.Resource
        query = resource.CreateFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), "identify_") '

        Dim layerIds() As String = Nothing
        Dim layerNames() As String = Nothing
        query.GetQueryableLayers(Nothing, layerIds, layerNames)
        Dim roundFactor As Double = Math.Pow(10, m_numberDecimals)
        Dim pointXString As String = Convert.ToString((Math.Round((mapPoint.X * roundFactor)) / roundFactor))
        Dim pointYString As String = Convert.ToString((Math.Round((mapPoint.Y * roundFactor)) / roundFactor))
        locXString = pointXString
        locYString = pointYString
        Dim ds As DataTable() = Nothing
        Try
          ds = query.Identify(mapFunc.Name, mapPoint, m_IdentifyTolerance, m_idOption, Nothing)
        Catch e As Exception
          Dim table As New DataTable()
          table.TableName = "Identify Error: " & e.Message
          ds = New DataTable() {table}
          notSupported = True
        End Try
        If Not (ds Is Nothing) And ds.Length > 0 Then
          Dim gds As New DataSet()
          Dim table As DataTable
          Dim j As Integer = 0
          For j = ds.Length - 1 To 0 Step -1
            table = ds(j)
            ' Remove empty tables.
            If table.Rows.Count = 0 And table.TableName.IndexOf("Error") = -1 Then
              ' nothing
            Else
              'If table.TableName.IndexOf("Error") = -1 Then
              Dim layer As GraphicsLayer = ESRI.ArcGIS.ADF.Web.Converter.ToGraphicsLayer(table, System.Drawing.Color.Empty, System.Drawing.Color.Aqua)
              If Not (layer Is Nothing) Then
                gds.Tables.Add(layer)
              Else
                gds.Tables.Add(table)
              End If
            End If
          Next j
          If gds.Tables.Count > 0 Then
            gds.DataSetName = resource.Name & " (" & pointXString & ", " & pointYString & ")"
            gdsList.Add(gds)
          End If
        End If
      End If
    Next mapFunc

    Dim i As Integer
    For i = gdsList.Count - 1 To 0 Step -1
      m_resultsDisplay.DisplayResults(Nothing, Nothing, Nothing, gdsList(i))
    Next i
    If gdsList.Count = 0 Then
      Dim heading As String = "Location (" & locXString & ", " & locYString & ") No results found"
      Dim detail As String = "No results found"
      Dim str As New SimpleTaskResult(heading, detail)
      m_resultsDisplay.DisplayResults(Nothing, Nothing, Nothing, str)
    End If
    Return m_resultsDisplay.CallbackResults.ToString()
  End Function 'Identify 

  Public Property Map() As Map
    Get
      Return m_map
    End Get
    Set(ByVal value As Map)
      m_map = value
    End Set
  End Property

  Public Property Page() As Page
    Get
      Return m_page
    End Get
    Set(ByVal value As Page)
      m_page = value
    End Set
  End Property

  Public Property DataSet() As DataSet
    Get
      Return m_dataset
    End Get
    Set(ByVal value As DataSet)
      m_dataset = value
    End Set
  End Property

  Public Property IdentifyOption() As IdentifyOption
    Get
      Return m_idOption
    End Get
    Set(ByVal value As IdentifyOption)
      m_idOption = value
    End Set
  End Property

  Public Property ClientCallbackInvocation() As String
    Get
      Return m_callbackInvocation
    End Get
    Set(ByVal value As String)
      m_callbackInvocation = value
    End Set
  End Property

  Public Property FilePath() As String
    Get
      Return m_filePath
    End Get
    Set(ByVal value As String)
      m_filePath = value
    End Set
  End Property

  Public Property ResultsDisplay() As TaskResults
    Get
      Return m_resultsDisplay
    End Get
    Set(ByVal value As TaskResults)
      m_resultsDisplay = value
    End Set
  End Property

  Public Property NumberDecimals() As Integer
    Get
      Return m_numberDecimals
    End Get
    Set(ByVal value As Integer)
      m_numberDecimals = value
    End Set
  End Property
End Class 'MapIdentify '
