Imports System.Collections.Generic
Imports System.Globalization
Imports System.IO
Imports System.Net
Imports System.Windows.Data
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Ink
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes

Imports C1.Silverlight.Maps

Namespace VectorLayer
    Public Class City
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(value As String)
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
        Public Property GeoPoint() As Point
            Get
                Return m_GeoPoint
            End Get
            Set(value As Point)
                m_GeoPoint = Value
            End Set
        End Property
        Private m_GeoPoint As Point
        Public Property Country() As String
            Get
                Return m_Country
            End Get
            Set(value As String)
                m_Country = Value
            End Set
        End Property
        Private m_Country As String
        Public Property Population() As Integer
            Get
                Return m_Population
            End Get
            Set(value As Integer)
                m_Population = Value
            End Set
        End Property
        Private m_Population As Integer

        Public Overrides Function ToString() As String
            Return String.Format("{0}" & vbTab & "{1}" & vbTab & "{2}" & vbTab & "{3}", Name, GeoPoint, Population, Country)
        End Function

        Public Shared Function FromString(s As String) As City
            Dim city As City = Nothing
            If Not String.IsNullOrEmpty(s) Then
                Dim ss As String() = s.Split(ControlChars.Tab)
                If ss.Length = 4 Then
                    city = New City() With {
                        .Name = ss(0),
                        .GeoPoint = PointFromString(ss(1)),
                        .Population = Integer.Parse(ss(2)),
                        .Country = ss(3)
                    }
                End If
            End If

            Return city
        End Function

        Private Shared Function PointFromString(s As String) As Point
            Dim ss As String() = s.Split(","c)
            Return New Point(Double.Parse(ss(1), CultureInfo.InvariantCulture), Double.Parse(ss(0), CultureInfo.InvariantCulture))
        End Function

        Public Shared Function Read(stream As Stream) As List(Of City)
            Dim cities As New List(Of City)()

            Using sr As New StreamReader(stream)
                While Not sr.EndOfStream
                    Dim city As City = FromString(sr.ReadLine())
                    If city IsNot Nothing Then
                        cities.Add(city)
                    End If
                End While
            End Using

            Return cities
        End Function
    End Class

    Public Class GeometryConverter
        Implements IValueConverter
        Shared stroke As Brush = New SolidColorBrush(Colors.Black)
        Shared fill As Brush = New SolidColorBrush(Colors.White)

        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Dim population As Integer = CInt(value)

            Dim ell As New EllipseGeometry()

            If population >= 2000000 Then
                ell.RadiusX = InlineAssignHelper(ell.RadiusY, 6)
            ElseIf population >= 1000000 Then
                ell.RadiusX = InlineAssignHelper(ell.RadiusY, 5)
            ElseIf population >= 7500000 Then
                ell.RadiusX = InlineAssignHelper(ell.RadiusY, 4)
            ElseIf population >= 500000 Then
                ell.RadiusX = InlineAssignHelper(ell.RadiusY, 3)
            ElseIf population >= 250000 Then
                ell.RadiusX = InlineAssignHelper(ell.RadiusY, 2)
            Else
                ell.RadiusX = InlineAssignHelper(ell.RadiusY, 1)
            End If

            'ToolTipService.SetToolTip(ell, population);

            Return ell
        End Function

        Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
        Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
    End Class

    Public Class LODConverter
        Implements IValueConverter
        Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
            Dim population As Integer = CInt(value)

            If population >= 2000000 Then
                Return New LOD(0, 0, 0, 20)
            End If

            If population >= 1000000 Then
                Return New LOD(0, 0, 1, 20)
            End If

            If population >= 7500000 Then
                Return New LOD(0, 0, 2, 20)
            End If

            If population >= 500000 Then
                Return New LOD(0, 0, 3, 20)
            End If

            If population >= 250000 Then
                Return New LOD(0, 0, 4, 20)
            End If

            Return New LOD(0, 0, 5, 20)
        End Function

        Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    End Class
End Namespace
