Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls
Imports ESRI.ArcGIS.ADF.Web.DataSources
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports ESRI.ArcGIS.ADF.ArcGISServer
Imports ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer
Imports System.Collections

Namespace CuahsiTools

  Public Class SelectStationsTool
    Inherits System.Web.UI.Page
    Implements IMapServerToolAction
    Public Sub ServerAction(ByVal args As ToolEventArgs) Implements IMapServerToolAction.ServerAction
      Dim pMapCtrl As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map
      Dim oPage As System.Web.UI.Page
      Dim oNetwork As HISNetwork
      Dim sName As String
      Dim ServerObjectName As String
      Dim pMF As MapFunctionality
      Dim LayerName As String
      Dim sMsg As String = ""
      Dim s As String
      Try
        pMapCtrl = args.Control
        oPage = args.Control.Page
        oNetwork = oPage.Session(ApSConfigAHS.KS_ACTIVENETWORK)
        sName = oNetwork.Name
        LayerName = oNetwork.LayerName
        ServerObjectName = ApSConfigAHS.MapServiceName
        pMF = pMapCtrl.GetFunctionality(ServerObjectName)
        Dim pGisRes As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = pMF.Resource
        Dim pGisResLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = pGisRes
        Dim pRectArgs As RectangleEventArgs = args
        Dim pRect As System.Drawing.Rectangle = pRectArgs.ScreenExtent


        Dim minPnt As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(pRect.Left, pRect.Bottom, pMapCtrl.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap))
        Dim maxPnt As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(pRect.Right, pRect.Top, pMapCtrl.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap))

        'Dim minPnt As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(pRect.Left, pRect.Bottom, pMapCtrl.Extent, pMF.DisplaySettings.ImageDescriptor.Width, pMF.DisplaySettings.ImageDescriptor.Height)
        'Dim maxPnt As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(pRect.Right, pRect.Top, pMapCtrl.Extent, pMF.DisplaySettings.ImageDescriptor.Width, pMF.DisplaySettings.ImageDescriptor.Height)
        Dim pMapRect As ESRI.ArcGIS.ADF.Web.Geometry.Envelope = New ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minPnt, maxPnt)
        Dim env As EnvelopeN = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfEnvelope(pMapRect)
        pMapRect = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfEnvelope(env)
        Dim SelectAction = 0
        Dim ShpName As String = ApSConfigAHS.GetShapeFieldName(pMF, sName)

        'Dim orgIds() As Integer
        Dim n As Integer
        ApSConfigAHS.SaveSelectedOIDs(oPage, pMF, LayerName, True)
        '..Getting the selected via map, not from the saved hashtable.
        'n = ApSConfigAHS.GetSelectedFeatureIDs(pMF, LayerName, ServerObjectName, orgIds)
        'ApSConfigAHS.HashtableAddValue(htSelectedFeatures, LayerName & ApSConfigAHS.KS_L, orgIds, True)
        Dim htSelectedFeatures As Hashtable
        htSelectedFeatures = ApSConfigAHS.GetSelected_Hashtable(oPage)
        Dim pSelColor As RgbColor
        pSelColor = ApSConfigAHS.GetSelectionColor()
        Dim ids() As Integer = ApSConfigAHS.ApplySelection(pMF, LayerName, env, esriSpatialRelEnum.esriSpatialRelIntersects, ShpName, pSelColor, ApSConfigAHS.SelectActions.NewSet)
        ApSConfigAHS.HashtableAddValue(htSelectedFeatures, LayerName & ApSConfigAHS.KS_C, ids, True)
        oPage.Session(ApSConfigAHS.KS_Selected) = htSelectedFeatures
        Try
          s = ApSConfigAHS.SaveSelectedSiteCodes(oPage, LayerName, pMF, ids, sMsg, oNetwork.SiteCodeField)
          If s = "" Then
            If ((ApSConfigAHS.WebDebug And ApSConfigAHS.DebugType.LogError) = ApSConfigAHS.DebugType.LogError) Then
              ApSConfigAHS.Write2Log("No sitecode found for " & ids.ToString & " " & oNetwork.Name & ", " & oNetwork.SiteCodeField)
            End If
          End If
          n = ids.Length()
          Dim cbParams(1) As Object
          cbParams(0) = "SetStatusMsg('" & CStr(n) & " features selected from layer " & LayerName & "');"
          Dim cbr As CallbackResult = New CallbackResult(Nothing, Nothing, "javascript", cbParams)
          pMapCtrl.CallbackResults.Add(cbr)
        Catch ex As Exception
          If ((ApSConfigAHS.WebDebug And ApSConfigAHS.DebugType.LogError) = ApSConfigAHS.DebugType.LogError) Then
            ApSConfigAHS.Write2Log("SelectStationsTool.ServerAction:" & ex.Message & "," & ex.StackTrace, True, -1, False)
          End If
        End Try
        pMapCtrl.Refresh()
      Catch ex As Exception

      End Try
    End Sub

    'Public Sub ServerAction(ByVal args As ToolEventArgs) Implements IMapServerToolAction.ServerAction
    '  Dim resource_index As Integer = 0
    '  Dim mapctrl As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(args.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)

    '  ClearSelectedFeatures(mapctrl)
    '  Dim oNetwork As HISNetwork = mapctrl.Page.Session(ApSConfigAHS.KS_ACTIVENETWORK)

    '  Dim sourcelayername As String = oNetwork.LayerName    'CStr(mapctrl.Page.Session(ApSConfigAHS.KS_ACTIVENETWORK))
    '  Dim mf As ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality = CType(mapctrl.GetFunctionality(resource_index), ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)
    '  Dim gisresource As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = mf.Resource

    '  Dim rectargs As RectangleEventArgs = CType(args, RectangleEventArgs)
    '  Dim myrect As System.Drawing.Rectangle = rectargs.ScreenExtent


    '  Dim minpnt As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(myrect.Left, myrect.Bottom, mapctrl.Extent, mf.DisplaySettings.ImageDescriptor.Width, mf.DisplaySettings.ImageDescriptor.Height)
    '  Dim maxpnt As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(myrect.Right, myrect.Top, mapctrl.Extent, mf.DisplaySettings.ImageDescriptor.Width, mf.DisplaySettings.ImageDescriptor.Height)
    '  Dim mappoly As ESRI.ArcGIS.ADF.Web.Geometry.Envelope = New ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minpnt, maxpnt)

    '  mapctrl.Page.Session(ApSConfigAHS.KS_SELECTED_CURRENTEXT) = mappoly
    '  '     Me.Page.Session(ApSConfigAHS.KS_SELECTED_CURRENTEXT_SAVED) = Nothing

    '  Dim supported As Boolean = gisresource.SupportsFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality))
    '  If supported Then
    '    Dim qfunc As ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality = CType(gisresource.CreateFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), Nothing), ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)
    '    Dim lids As String()
    '    Dim lnames As String()
    '    qfunc.GetQueryableLayers(Nothing, lids, lnames)
    '    Dim layer_index As Integer = 1
    '    Dim i As Integer = 0
    '    Do While i < lnames.Length
    '      If lnames(i) = sourcelayername Then
    '        If TypeOf lids(i) Is String Then
    '          If (Not Integer.TryParse(CStr(lids(i)), layer_index)) Then
    '            layer_index = i
    '          End If
    '        Else
    '          layer_index = i
    '        End If
    '        Exit Do
    '      Else
    '        layer_index = 0  ' default selected layer
    '      End If
    '      i += 1
    '    Loop

    '    If TypeOf qfunc.Resource Is ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal Then
    '      Dim ags_mf As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality = CType(mf, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)
    '      Dim ags_mr As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceBase = CType(qfunc.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceBase)
    '      Dim mapdescription As ESRI.ArcGIS.ADF.ArcGISServer.MapDescription
    '      mapdescription = ags_mf.MapDescription
    '      Dim layerdescs As LayerDescription() = mapdescription.LayerDescriptions

    '      Dim mli As MapLayerInfo() = ags_mr.MapServerInfo.MapLayerInfos
    '      Dim featureLayer As LayerDescription = layerdescs(layer_index)


    '      If featureLayer Is Nothing Then
    '        ' throw exception because feature layer is not valid
    '        Throw New Exception("featurelayer null")
    '      Else

    '        Dim envelopen As EnvelopeN = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfEnvelope(mappoly)

    '        Dim sf As SpatialFilter = New SpatialFilter()
    '        sf.FilterGeometry = envelopen
    '        sf.GeometryFieldName = "SHAPE"
    '        sf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects

    '        Dim msdp As MapServerProxy = ags_mr.MapServerProxy

    '        Dim irgbc As RgbColor = New RgbColor()
    '        irgbc.Red = 255
    '        irgbc.Green = 255
    '        irgbc.Blue = 0
    '        irgbc.AlphaValue = 255

    '        featureLayer.SelectionColor = irgbc
    '        Dim ifid As FIDSet = msdp.QueryFeatureIDs(mapdescription.Name, featureLayer.LayerID, sf)
    '        featureLayer.SelectionFeatures = ifid.FIDArray

    '        Dim fIdx As Integer = ApSConfigAHS.NWIS_FEATURECLASS_STATION_IDX
    '        Dim pFSiteColumnName As String = ""

    '        If sourcelayername.ToLower.Contains(ApSConfigAHS.NETWORK_NAME_EPA) Then
    '          pFSiteColumnName = ApSConfigAHS.EPA_FEATURECLASS_SITE_COLUMN_NAME

    '        ElseIf sourcelayername.ToLower.Contains(ApSConfigAHS.NETWORK_NAME_NWIS) Then
    '          pFSiteColumnName = ApSConfigAHS.NWIS_FEATURECLASS_SITE_COLUMN_NAME

    '        End If

    '        Dim recSet As ESRI.ArcGIS.ADF.ArcGISServer.RecordSet = msdp.QueryFeatureData(mapdescription.Name, featureLayer.LayerID, sf)
    '        Dim j As Integer
    '        Dim recd As ESRI.ArcGIS.ADF.ArcGISServer.Record
    '        Dim strSelectedStationList As String = ""
    '        Dim selSiteCount As Integer = 0

    '        fIdx = GetFieldIndex(recSet, pFSiteColumnName)
    '        For j = 0 To recSet.Records.Length - 1
    '          recd = recSet.Records(j)
    '          strSelectedStationList += CStr(recd.Values(fIdx)) + "|"
    '          selSiteCount += 1

    '        Next
    '        'SiteCode1|SiteCode2|SiteCode3|
    '        Session(ApSConfigAHS.SELECTED_STATION_LIST) = strSelectedStationList

    '        mapctrl.Refresh()
    '      End If
    '    End If
    '  End If
    'End Sub


    'Same code appears in multiple places
    'Sub ClearSelectedFeatures(ByVal pMapCtrl As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)
    '  Dim resource_index As Integer = 0
    '  Dim mf As MapFunctionality = CType(pMapCtrl.GetFunctionality(resource_index), MapFunctionality)
    '  Dim pGisRecs As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = mf.Resource
    '  Dim pGisRecsLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = CType(pGisRecs, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
    '  Dim pMapDesc As ESRI.ArcGIS.ADF.ArcGISServer.MapDescription = mf.MapDescription
    '  Dim pLayerDescs() As ESRI.ArcGIS.ADF.ArcGISServer.LayerDescription = pMapDesc.LayerDescriptions
    '  Dim pLayerDesc As ESRI.ArcGIS.ADF.ArcGISServer.LayerDescription

    '  ' Get the current extent and save it before drawing the new extent
    '  Dim mappoly As ESRI.ArcGIS.ADF.Web.Geometry.Envelope = CType(pMapCtrl.Page.Session(ApSConfigAHS.KS_SELECTED_CURRENTEXT), ESRI.ArcGIS.ADF.Web.Geometry.Envelope)
    '  If Not mappoly Is Nothing Then
    '    pMapCtrl.Page.Session(ApSConfigAHS.KS_SELECTED_CURRENTEXT_SAVED) = mappoly
    '    pMapCtrl.Page.Session(ApSConfigAHS.KS_SELECTED_CURRENTEXT) = Nothing
    '  End If


    '  For Each pLayerDesc In pLayerDescs
    '    pLayerDesc.SelectionFeatures = Nothing
    '  Next
    '  Me.Page.Session(ApSConfigAHS.SELECTED_STATION_LIST) = Nothing
    '  pMapCtrl.Refresh()
    '    End Sub

    Public Function GetFieldIndex(ByVal rsSet As ESRI.ArcGIS.ADF.ArcGISServer.RecordSet, ByVal sFieldName As String) As Integer

      Dim i As Integer

      Dim icnt As Integer

      Dim sName As String

      Dim iIndex As Integer = -1

      Try

        sName = sFieldName.ToLower.Trim

        icnt = rsSet.Fields.FieldArray.Length

        For i = 0 To icnt - 1

          If (rsSet.Fields.FieldArray(i).Name.ToLower = sName) Then

            iIndex = i

            Exit For

          End If

        Next

      Catch ex As Exception



      End Try

      Return iIndex

    End Function

  End Class

  Public Class SelectPolygonTool
    Inherits System.Web.UI.Page
    Implements IMapServerToolAction

    Public Sub ServerAction(ByVal args As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs) Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction
      Dim resource_index As Integer = 0

      Dim mapctrl As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(args.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)
      'ClearSelectedFeatures(mapctrl)
      Dim sourcelayername As String = CStr(mapctrl.Page.Session(ApSConfigAHS.KS_ACTIVENETWORK))
      Dim mf As ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality = CType(mapctrl.GetFunctionality(resource_index), ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)
      Dim gisresource As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = mf.Resource

      Dim polyargs As PolygonEventArgs = CType(args, PolygonEventArgs)
      Dim myPoly As ESRI.ArcGIS.ADF.ArcGISServer.PolygonN = GetPolygonN(mapctrl, polyargs)

    End Sub


    Public Function GetPolygonN(ByVal pMapCtrl As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map, ByVal pPolyEventArgs As PolygonEventArgs) As PolygonN

      Dim pPolygon As PolygonN = Nothing
      Try

        Dim mf As MapFunctionality = CType(pMapCtrl.GetFunctionality(0), MapFunctionality)
        Dim screenpoly() As System.Drawing.Point = pPolyEventArgs.Vectors
        Dim pc As ESRI.ArcGIS.ADF.Web.Geometry.PointCollection = New ESRI.ArcGIS.ADF.Web.Geometry.PointCollection()
        Dim dpnt As System.Drawing.Point
        Dim pPnt As ESRI.ArcGIS.ADF.Web.Geometry.Point
        For Each dpnt In screenpoly
          pPnt = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(dpnt.X, dpnt.Y, pMapCtrl.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap))
          pc.Add(pPnt)
        Next
        Dim ring As ESRI.ArcGIS.ADF.Web.Geometry.Ring = New ESRI.ArcGIS.ADF.Web.Geometry.Ring()
        ring.Points = pc

        Dim rings As ESRI.ArcGIS.ADF.Web.Geometry.RingCollection = New ESRI.ArcGIS.ADF.Web.Geometry.RingCollection()
        rings.Add(ring)

        Dim mappoly As ESRI.ArcGIS.ADF.Web.Geometry.Polygon = New ESRI.ArcGIS.ADF.Web.Geometry.Polygon()
        mappoly.Rings = rings

        pPolygon = New ESRI.ArcGIS.ADF.ArcGISServer.PolygonN()
        pPolygon = CType(ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPolygon(mappoly), PolygonN)

      Catch ex As Exception

      End Try

      Return pPolygon
    End Function


    'Sub ClearSelectedFeatures(ByVal pMapCtrl As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)
    '  Dim resource_index As Integer = 0
    '  Dim mf As MapFunctionality = CType(pMapCtrl.GetFunctionality(resource_index), MapFunctionality)
    '  Dim pGisRecs As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = mf.Resource
    '  Dim pGisRecsLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = CType(pGisRecs, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
    '  Dim pMapDesc As ESRI.ArcGIS.ADF.ArcGISServer.MapDescription = mf.MapDescription
    '  Dim pLayerDescs() As ESRI.ArcGIS.ADF.ArcGISServer.LayerDescription = pMapDesc.LayerDescriptions
    '  Dim pLayerDesc As ESRI.ArcGIS.ADF.ArcGISServer.LayerDescription

    '  ' Get the current extent and save it before drawing the new extent
    '  Dim mappoly As ESRI.ArcGIS.ADF.Web.Geometry.Envelope = CType(pMapCtrl.Page.Session(ApSConfigAHS.KS_SELECTED_CURRENTEXT), ESRI.ArcGIS.ADF.Web.Geometry.Envelope)
    '  If Not mappoly Is Nothing Then
    '    pMapCtrl.Page.Session(ApSConfigAHS.KS_SELECTED_CURRENTEXT_SAVED) = mappoly
    '    pMapCtrl.Page.Session(ApSConfigAHS.KS_SELECTED_CURRENTEXT) = Nothing
    '  End If


    '  For Each pLayerDesc In pLayerDescs
    '    pLayerDesc.SelectionFeatures = Nothing
    '  Next
    '  Me.Page.Session(ApSConfigAHS.SELECTED_STATION_LIST) = Nothing
    '  pMapCtrl.Refresh()
    'End Sub
  End Class
End Namespace

