Imports System.IO
Imports System.Net
Imports System.Reflection
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 Utils
        Public Shared Function CreateBaloon() As Geometry
            Dim pg As New PathGeometry()
            pg.Transform = New TranslateTransform() With {
                .X = -10,
                .Y = -24.14
            }
            Dim pf As New PathFigure() With {
                .StartPoint = New Point(10, 24.14),
                .IsFilled = True,
                .IsClosed = True
            }
            pf.Segments.Add(New ArcSegment() With {
                .SweepDirection = SweepDirection.Counterclockwise,
                .Point = New Point(5, 19.14),
                .RotationAngle = 45,
                .Size = New Size(10, 10)
            })
            pf.Segments.Add(New ArcSegment() With {
                .SweepDirection = SweepDirection.Clockwise,
                .Point = New Point(15, 19.14),
                .RotationAngle = 270,
                .Size = New Size(10, 10),
                .IsLargeArc = True
            })
            pf.Segments.Add(New ArcSegment() With {
                .SweepDirection = SweepDirection.Counterclockwise,
                .Point = New Point(10, 24.14),
                .RotationAngle = 45,
                .Size = New Size(10, 10)
            })
            pg.Figures.Add(pf)
            Return pg
        End Function

        Public Shared Sub LoadShapeFromResource(vl As C1VectorLayer, resname As String, dbfname As String, lt As Location, clear As Boolean, pv As ProcessShapeItem)
            Dim shapeResource = Application.GetResourceStream(New Uri(resname, UriKind.Relative))
            Dim dbResource = Application.GetResourceStream(New Uri(dbfname, UriKind.Relative))
            Using shapeStream As Stream = shapeResource.Stream
                Using dbfStream As Stream = dbResource.Stream
                    If shapeStream IsNot Nothing Then
                        If clear Then
                            vl.Children.Clear()
                        End If

                        vl.LoadShape(shapeStream, dbfStream, lt, True, pv)
                    End If
                End Using
            End Using
        End Sub

        Public Shared Sub LoadKMZFromResources(vl As C1VectorLayer, resname As String, clear As Boolean, pv As ProcessVectorItem)
            Dim resouce = Application.GetResourceStream(New Uri(resname, UriKind.Relative))
            Using zipStream As Stream = resouce.Stream
                If zipStream IsNot Nothing Then
                    If clear Then
                        vl.Children.Clear()
                    End If

                    vl.LoadKMZ(zipStream, True, pv)
                End If
            End Using
        End Sub

        Public Shared Sub LoadKMLFromResources(vl As C1VectorLayer, resname As String, clear As Boolean, pv As ProcessVectorItem)
            Dim resouce = Application.GetResourceStream(New Uri(resname, UriKind.Relative))
            If clear Then
                vl.Children.Clear()
            End If

            vl.LoadKML(resouce.Stream, False, pv)
        End Sub

        Shared rnd As New Random()

        Public Shared Function GetRandomColor(a As Byte) As Color
            Return Color.FromArgb(a, CByte(rnd.[Next](255)), CByte(rnd.[Next](255)), CByte(rnd.[Next](255)))
        End Function

        Public Shared Function GetRandomColor(min As Byte, a As Byte) As Color
            Return Color.FromArgb(a, CByte(min + rnd.[Next](255 - min)), CByte(min + rnd.[Next](255 - min)), CByte(min + rnd.[Next](255 - min)))
        End Function
    End Class
End Namespace
