Imports System.IO
Imports System.Net
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.Imaging
Imports System.Windows.Data
Imports System.Reflection
#If SILVERLIGHT Then
Imports C1.Silverlight.FlexGrid
#Else
Imports C1.WPF.FlexGrid
#End If

Namespace MainTestApplication
	''' <summary>
	''' Represents a grid cell that has an image and some text.
	''' </summary>
	Public MustInherit Class ImageCell
		Inherits StackPanel
		Private _imgSrc As ImageSource

		Public Sub New(ByVal row_Renamed As Row)
			If _imgSrc Is Nothing Then
				_imgSrc = GetImageSource(GetImageResourceName())
			End If

			Orientation = Orientation.Horizontal

			Dim img = New Image()
			img.Source = _imgSrc
			img.Width = 25
			img.Height = 15
			img.VerticalAlignment = VerticalAlignment.Center
			img.Stretch = Stretch.None
			Children.Add(img)

			Dim tb = New TextBlock()
			tb.VerticalAlignment = VerticalAlignment.Center
			Children.Add(tb)
			BindCell(row_Renamed.DataItem)
		End Sub
		Public ReadOnly Property TextBlock() As TextBlock
			Get
				Return TryCast(Children(Children.Count - 1), TextBlock)
			End Get
		End Property
		Public Overridable WriteOnly Property Row() As Row
			Set(ByVal value As Row)
				BindCell(value.DataItem)
			End Set
		End Property
		Private Sub BindCell(ByVal dataItem As Object)
			Dim binding_Renamed = New Binding("Name")
			binding_Renamed.Source = dataItem
			TextBlock.SetBinding(TextBlock.TextProperty, binding_Renamed)
		End Sub
		Public MustOverride Function GetImageResourceName() As String

		Public Shared Function GetImageSource(ByVal imageName As String) As ImageSource
#If SILVERLIGHT Then
			Dim bmp = New BitmapImage()
            bmp.CreateOptions = BitmapCreateOptions.None
            Dim fmt As String = "/" + New AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/{0}"
            bmp.UriSource = New Uri(String.Format(fmt, imageName), UriKind.Relative)
            Return bmp
#Else
			Dim imgConv = New ImageSourceConverter()
			Dim path As String = String.Format("pack://application:,,/Resources/{0}", imageName)
			Return CType(imgConv.ConvertFromString(path), ImageSource)
#End If
		End Function
	End Class

	''' <summary>
	''' Represents a grid cell that is bound to a song name.
	''' </summary>
	Public Class SongCell
		Inherits ImageCell
		Public Sub New(ByVal row_Renamed As Row)
			MyBase.New(row_Renamed)
		End Sub
		Public Overrides Function GetImageResourceName() As String
			Return "Song.png"
		End Function
	End Class

	''' <summary>
	''' Represents a grid cell that has a collapse/expand icon, an image, and some text.
	''' </summary>
	Public MustInherit Class NodeCell
		Inherits ImageCell
		Private Const ALPHA As Double = 0.5
		Private _gr As GroupRow
		Private _nodeImage As Image
		Private Shared _bmpExpanded, _bmpCollapsed As ImageSource

		Public Sub New(ByVal row_Renamed As Row)
			MyBase.New(row_Renamed)
			' create collapsed/expanded images
			If _bmpExpanded Is Nothing Then
				_bmpExpanded = ImageCell.GetImageSource("Expanded.png")
				_bmpCollapsed = ImageCell.GetImageSource("Collapsed.png")
			End If

			' store reference to row
			_gr = TryCast(row_Renamed, GroupRow)

			' initialize collapsed/expanded image
			_nodeImage = New Image()
			_nodeImage.Source = If(_gr.IsCollapsed, _bmpCollapsed, _bmpExpanded)
			_nodeImage.Height = 9
			_nodeImage.Width = _nodeImage.Height
			_nodeImage.VerticalAlignment = VerticalAlignment.Center
			_nodeImage.Stretch = Stretch.None
			AddHandler _nodeImage.MouseLeftButtonDown, AddressOf img_MouseLeftButtonDown
			AddHandler _nodeImage.MouseEnter, AddressOf img_MouseEnter
			AddHandler _nodeImage.MouseLeave, AddressOf img_MouseLeave
			_nodeImage.Opacity = ALPHA
			Children.Insert(0, _nodeImage)

			' make text bold
			TextBlock.FontWeight = FontWeights.Bold
		End Sub
		Private Sub img_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
			Dim img = TryCast(sender, Image)
			Dim cell = TryCast(img.Parent, NodeCell)
			cell.IsCollapsed = Not cell.IsCollapsed
		End Sub
		Private Sub img_MouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)
			Dim img = TryCast(sender, Image)
			img.Opacity = 1
		End Sub
		Private Sub img_MouseLeave(ByVal sender As Object, ByVal e As MouseEventArgs)
			Dim img = TryCast(sender, Image)
			img.Opacity = ALPHA
		End Sub
		Public Overrides WriteOnly Property Row() As Row
			Set(ByVal value As Row)
				' update image
				_gr = TryCast(value, GroupRow)
				_nodeImage.Source = If(_gr.IsCollapsed, _bmpCollapsed, _bmpExpanded)
				_nodeImage.Opacity = ALPHA

				' update text
				MyBase.Row = value
			End Set
		End Property
		Public Property IsCollapsed() As Boolean
			Get
				Return _gr.IsCollapsed
			End Get
			Set(ByVal value As Boolean)
				_gr.IsCollapsed = value
				_nodeImage.Source = If(value, _bmpCollapsed, _bmpExpanded)
			End Set
		End Property
	End Class

	''' <summary>
	''' Represents a grid cell that is bound to an artist.
	''' </summary>
	Public Class ArtistCell
		Inherits NodeCell
		Public Sub New(ByVal row_Renamed As Row)
			MyBase.New(row_Renamed)
		End Sub
		Public Overrides Function GetImageResourceName() As String
			Return "Artist.png"
		End Function
	End Class

	''' <summary>
	''' Represents a grid cell that is bound to an album.
	''' </summary>
	Public Class AlbumCell
		Inherits NodeCell
		Public Sub New(ByVal row_Renamed As Row)
			MyBase.New(row_Renamed)
		End Sub
		Public Overrides Function GetImageResourceName() As String
			Return "Album.png"
		End Function
	End Class
End Namespace
