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.Animation
Imports System.Windows.Shapes
Imports System.Collections
Imports C1.Silverlight.FlexGrid

Namespace ExcelStyleMerge
	Public Class ExcelStyleMergeManager
		Implements IMergeManager
		' ** fields
		Private _grid As C1FlexGrid
		Private _mergedRanges As New List(Of CellRange)()

		' ** ctor
		Public Sub New(ByVal grid As C1FlexGrid)
			_grid = grid
		End Sub

		' ** IMergeManager
        Public Function GetMergedRange(ByVal grid As C1FlexGrid, ByVal cellType_Renamed As CellType, ByVal range As CellRange) As CellRange Implements IMergeManager.GetMergedRange
            ' look for merged ranges that contain the given cell
            For Each mergedRange In _mergedRanges
                If mergedRange.Contains(range) Then
                    Return mergedRange
                End If
            Next mergedRange

            ' not found, not merged
            Return range
        End Function

		' ** object model
		Public Sub AddMergedRange(ByVal rng As CellRange)
			If Not rng.IsSingleCell Then
				RemoveMergedRange(rng)
				_mergedRanges.Add(New CellRange(rng.TopRow, rng.LeftColumn, rng.BottomRow, rng.RightColumn))
				_grid.Invalidate()
			End If
		End Sub
		Public Sub RemoveMergedRange(ByVal rng As CellRange)
			Dim i As Integer = 0
			Do While i < _mergedRanges.Count
				If rng.Intersects(_mergedRanges(i)) Then
					_mergedRanges.RemoveAt(i)
					i -= 1
				End If
				i += 1
			Loop
			_grid.Invalidate()
		End Sub
	End Class
End Namespace
