Imports DevExpress.XtraMap

Public Class frmGeodataSelect
    Public SelectedPoint As GeoPoint = Nothing
    Dim _SelectActive As Boolean = False
    Private BING_KEY As String = "hQUTlqLLK70bETnonpfi~0jx1pIAq1yQ7gXqbIyzKrg~Au-Tewbty8afAxdbNilSv4JlU7qwU-fQKu0ouH9e1uJmpIyVdA3jugVEWMdy1Rbt"

    Private ReadOnly Property ImageLayer() As ImageTilesLayer
        Get
            Return CType(MapControl1.Layers("ImageLayer"), ImageTilesLayer)
        End Get
    End Property

    Private ReadOnly Property VectorLayer() As VectorItemsLayer
        Get
            Return CType(MapControl1.Layers("VectorLayer"), VectorItemsLayer)
        End Get
    End Property

    Private ReadOnly Property ItemStorage() As MapItemStorage
        Get
            Return CType(VectorLayer.Data, MapItemStorage)
        End Get
    End Property

    Public Sub New(currentPoint As GeoPoint)
        SelectedPoint = currentPoint

        Me.InitializeComponent()
    End Sub

    Private Sub frmGeodataSelect_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim dataProvider As New BingMapDataProvider()
        dataProvider.BingKey = BING_KEY
        ImageLayer.DataProvider = dataProvider

        Dim XMLPath = Get_Settings_Filename()
        Dim layout As New ClassLayout(XMLPath)

        Dim storage As New MapItemStorage()
        VectorLayer.Data = storage

        If SelectedPoint IsNot Nothing Then
            Dim pin = New MapPushpin()
            pin.Location = SelectedPoint
            ItemStorage.Items.Add(pin)

            txtLat.Text = SelectedPoint.Latitude.ToString()
            txtLon.Text = SelectedPoint.Longitude.ToString()

            Dim zoomLevel = layout.GetValue("MapControlSelect_ZoomLevel")

            If IsNothing(zoomLevel) Then
                MapControl1.ZoomLevel = 10.0
            Else
                MapControl1.ZoomLevel = Double.Parse(zoomLevel)
            End If

            MapControl1.ZoomToFit(New List(Of MapPushpin) From {pin})
        End If


    End Sub

    Private Sub MapControl1_Click(sender As Object, e As MouseEventArgs) Handles MapControl1.Click
        If e.Button <> System.Windows.Forms.MouseButtons.Left Then
            Exit Sub
        End If
        If _SelectActive = False Then
            Exit Sub
        End If
        Dim point As GeoPoint = MapControl1.ScreenPointToCoordPoint(e.Location)

        Dim items As New List(Of MapPushpin)
        Dim storage As New MapItemStorage()

        Dim pin = New MapPushpin()
        pin.Location = point

        ItemStorage.Items.Clear()
        ItemStorage.Items.Add(pin)

        txtLat.Text = point.Latitude.ToString()
        txtLon.Text = point.Longitude.ToString()

        SelectedPoint = point
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim XMLPath = Get_Settings_Filename()
        Dim layout As New ClassLayout(XMLPath)
        Dim settings As New System.Collections.Generic.List(Of ClassSetting)
        settings = layout.Load()

        layout.PutValue("MapControlSelect_ZoomLevel", MapControl1.ZoomLevel)

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Function Get_Settings_Filename()
        Dim Filename As String = String.Format("{0}-Geodata-Layout.xml", CURRENT_CONSTRUCTOR_ID)
        Return System.IO.Path.Combine(Application.UserAppDataPath(), Filename)
    End Function

    Private Sub tsbtnactivate_Click(sender As Object, e As EventArgs) Handles tsbtnactivate.Click
        If _SelectActive = False Then
            _SelectActive = True
            tsbtnactivate.Checked = True
        Else
            tsbtnactivate.Checked = False
            _SelectActive = False
        End If
    End Sub
End Class