
Imports C1.Silverlight.Maps
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports VectorLayer.VectorLayer
Imports System.Reflection

Public Enum Location
    USA
    Japan
End Enum

''' <summary>
''' Interaction logic for MapShape.xaml
''' </summary>
Partial Public Class MapShape
    Inherits UserControl
    Private Locations As New Dictionary(Of Location, String)()
    Public Sub New()
        InitializeComponent()
        AddHandler Me.Loaded, AddressOf MapShape_Loaded
    End Sub

    Private Sub InitialLocations()
        If Locations.Count = 0 Then
            Locations.Add(Location.USA, "USA")
            Locations.Add(Location.Japan, "Japan")
        End If
    End Sub
    Private Sub MapShape_Loaded(sender As Object, e As RoutedEventArgs)
        InitialLocations()
        Me.countriesCombo.ItemsSource = Locations
        countriesCombo.SelectedIndex = 0
        maps.Source = Nothing
        InitialMapsByLocaltion(maps, Location.USA)
    End Sub

    Private Sub ProcessMap(vector As C1VectorItemBase, attribute As C1ShapeAttributes, lt As Location)
        InitialVectorItemBase(vector, attribute, lt)
    End Sub

    Private Sub InitialMapsByLocaltion(maps As C1Maps, lt As Location)
        Dim layer As New C1VectorLayer()
        Dim center As New Point()
        Dim zoom As Double = 0
        Select Case lt
            Case Location.USA
                Utils.LoadShapeFromResource(layer, "/" + New AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/states.shp", "/" + New AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/states.dbf", lt, True, AddressOf ProcessMap)
                center = New Point(-115, 50)
                zoom = 2
                Exit Select
            Case Location.Japan
                Utils.LoadShapeFromResource(layer, "/" + New AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/jp_toku_kuni_pgn.shp", "/" + New AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/jp_toku_kuni_pgn.dbf", lt, True, AddressOf ProcessMap)
                center = New Point(135, 37)
                zoom = 4
                Exit Select
        End Select

        If maps.Layers IsNot Nothing AndAlso maps.Layers.Count <> 0 Then
            maps.Layers.Clear()
        End If
        maps.Layers.Add(layer)
        maps.MinZoom = zoom
        maps.Zoom = zoom
        maps.Center = center
    End Sub

    Private Sub InitialVectorItemBase(vector As C1VectorItemBase, attribute As C1ShapeAttributes, lt As Location)
        Dim toolTip As String = String.Empty
        vector.Stroke = New SolidColorBrush(Colors.LightGray)
        Select Case lt
            Case Location.USA
                vector.Fill = New SolidColorBrush(Colors.Purple)
                toolTip = DirectCast(attribute("STATE_NAME"), String)
                Exit Select
            Case Location.Japan
                vector.Fill = New SolidColorBrush(Colors.Brown)
                toolTip = DirectCast(attribute("NAME_UTF"), String)
                Exit Select
        End Select
        ToolTipService.SetToolTip(vector, toolTip)
    End Sub

    Private Sub countriesCombo_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
        Dim comboBox = TryCast(sender, ComboBox)
        If comboBox IsNot Nothing Then
            Dim lt As Location = If(comboBox.SelectedItem Is Nothing, Location.USA, InlineAssignHelper(lt, CType(CType(countriesCombo.SelectedItem, KeyValuePair(Of Location, String)).Key, Location)))
            InitialMapsByLocaltion(maps, lt)
        End If
    End Sub
    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
        target = value
        Return value
    End Function
End Class
