Imports Mercury.Mobile.Forms
Imports Mercury.Mobile.Data
Imports Mercury.Mobile.Threading
Imports System.Data
Imports System.ComponentModel

Partial Public Class TestForm

    Private testFlaggedData As DataTable = Nothing
    Private testTableView As DataView = Nothing
    Private testGridManager As CurrencyManager = Nothing
    Private WithEvents _gridView As AsyncListView(Of MarkedData)

    Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Get datatable
        Me.testFlaggedData = GetComboBoxData()
        For i As Integer = Me.testFlaggedData.Rows.Count To 4999
            Me.testFlaggedData.Rows.Add(String.Format("Row:{0}", i), False)
        Next
        Me.testFlaggedData.TableName = "TestData"

        Me.MobileComboBox1.DataSource = Me.GetComboBoxData
        Me.MobileComboBox1.SelectedIndex = 0
        Me.MobileComboBox1.FlagColumnName = "Flag"
        Me.MobileComboBox1.FlagRowHighlightColor = Color.FromArgb(192, 255, 192)
        Me.MobileComboBox1.CanHighlightFlagRows = True

        ' native combobox
        Me.ComboBox1.DataSource = Me.testFlaggedData
        Me.ComboBox1.DisplayMember = "Main"

        ' Mobile listbox
        Me.MobileListBox1.DataSource = Me.GetTestComboBoxData
        Me.MobileListBox1.DisplayMember = "Main"
        Me.MobileListBox1.FlagColumnName = "Flag"
        Me.MobileListBox1.FlagRowHighlightColor = Color.FromArgb(192, 255, 192)
        Me.MobileListBox1.CanHighlightFlagRows = True

        ' Setup datagrid
        'Me.testTableView = New DataView(Me.testFlaggedData)
        Me._gridView = Me.GetItemCollection()
        Me.FormatDataGrid(Me._gridView.GetItemProperties(Nothing))
        Me.DataGrid1.DataSource = Me._gridView
        Me.testGridManager = CType(Me.DataGrid1.BindingContext(Me._gridView), CurrencyManager)
        Me.DataGrid1.RowHeadersVisible = False
        Dim ct = Me._gridView.Count

        '' Format
        'FormatDataGrid()

        'Me.DataGrid1.SetDisplayRow(10)
        'Me.DataGrid1.Refresh()
    End Sub

    Private Sub _GridViewLoaded(ByVal sender As Object, ByVal e As EventArgs) Handles _gridView.CollectionReset
        If Me._gridView.Count > 0 Then
            Me.HandleClientAreaUpdate()
        End If
    End Sub

    Private Sub _DataContainerPropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
        Cursor.Current = Cursors.Default
        Dim container = TryCast(sender, DataContainer(Of MarkedData))
        If container IsNot Nothing Then _
            RemoveHandler container.PropertyChanged, AddressOf _DataContainerPropertyChanged
        Me.DataGrid1.Refresh()
    End Sub

    Private Sub _ScrollEnded(ByVal sender As Object, ByVal e As EventArgs) Handles DataGrid1.ScrollCompleted
        Me.HandleClientAreaUpdate()
    End Sub

    Private Sub HandleClientAreaUpdate()
        Dim lastIdx = (Me.DataGrid1.CalculateVisibleRowCount() + Me.DataGrid1.FirstVisibleRow - 1)
        lastIdx = Math.Min(lastIdx, Me._gridView.Count - 1)
        Dim container = Me._gridView.Item(lastIdx)
        If container IsNot Nothing AndAlso container.IsLoading Then _
            AddHandler container.PropertyChanged, AddressOf _DataContainerPropertyChanged
    End Sub

    Private Sub FormatDataGrid(ByVal pdc As PropertyDescriptorCollection)
        Dim tableStyle As New DataGridTableStyle()
        tableStyle.MappingName = ""
        Dim columnStyle As MarkableDataGridTextBoxColumn

        Dim pd = pdc.Item("Id")
        columnStyle = New MarkableDataGridTextBoxColumn(pd)
        columnStyle.HeaderText = "ID"
        columnStyle.IsReadOnly = True
        columnStyle.Owner = Me.DataGrid1
        columnStyle.HighlightFlagCellColor = Color.FromArgb(192, 255, 192)
        columnStyle.HighlightFlagColumnName = "Flag"
        tableStyle.GridColumnStyles.Add(columnStyle)

        pd = pdc.Item("Flag")
        columnStyle = New MarkableDataGridTextBoxColumn(pd)
        columnStyle.IsReadOnly = True
        columnStyle.Owner = Me.DataGrid1
        columnStyle.HighlightFlagCellColor = Color.FromArgb(192, 255, 192)
        columnStyle.HighlightFlagColumnName = "Flag"
        tableStyle.GridColumnStyles.Add(columnStyle)

        'clear default table style
        Me.DataGrid1.TableStyles.Clear()
        Me.DataGrid1.TableStyles.Add(tableStyle)
    End Sub

    Private Sub FormatDataGrid()
        Dim tableStyle As New DataGridTableStyle
        tableStyle.MappingName = "TestData"
        Dim columnStyle As MarkableDataGridTextBoxColumn

        ' Main
        columnStyle = New MarkableDataGridTextBoxColumn
        columnStyle.Owner = Me.DataGrid1
        columnStyle.IsReadOnly = True
        columnStyle.MappingName = "Main"
        columnStyle.HeaderText = "Main"
        columnStyle.Width = 140
        columnStyle.HighlightFlagCellColor = Color.FromArgb(192, 255, 192)
        columnStyle.HighlightFlagColumnName = "Flag"
        tableStyle.GridColumnStyles.Add(columnStyle)

        ' Flag
        columnStyle = New MarkableDataGridTextBoxColumn
        columnStyle.Owner = Me.DataGrid1
        columnStyle.IsReadOnly = True
        columnStyle.MappingName = "Flag"
        columnStyle.HeaderText = "Flag"
        columnStyle.Width = 100
        columnStyle.HighlightFlagCellColor = Color.FromArgb(192, 255, 192)
        columnStyle.HighlightFlagColumnName = "Flag"
        tableStyle.GridColumnStyles.Add(columnStyle)

        'clear default table style
        Me.DataGrid1.TableStyles.Clear()
        Me.DataGrid1.TableStyles.Add(tableStyle)
    End Sub

    Public Class MarkedData

        Private _id As String
        Private _flag As Boolean

        Public Property Id() As String
            Get
                Return Me._id
            End Get
            Set(ByVal value As String)
                Me._id = value
            End Set
        End Property

        Public Property Flag() As Boolean
            Get
                Return Me._flag
            End Get
            Set(ByVal value As Boolean)
                Me._flag = value
            End Set
        End Property

    End Class

    Private Function GetItemCollection() As AsyncListView(Of MarkedData)
        Dim dataList = New List(Of MarkedData)()
        Dim row As MarkedData = Nothing
        For i As Integer = 0 To 4999
            row = New MarkedData() With {.Id = String.Format("Row:{0}", i)}
            If i > 0 AndAlso i Mod 2 = 0 Then
                row.Flag = True
            Else
                row.Flag = False
            End If
            dataList.Add(row)
        Next
        Dim ctx = New WinFormsSynchronizationContext(Me.DataGrid1)
        ThreadSynchronizationContext.SetSynchronizationContext(ctx)
        Dim provider = New DefaultItemProvider(Of MarkedData)(dataList, New PropertySortComparer(Of MarkedData))
        provider.Delay = TimeSpan.FromSeconds(1)
        Dim collection = New AsyncListView(Of MarkedData)(provider, 75, 5000, ctx, False)
        Return collection
    End Function

    Private Function GetTestComboBoxData() As DataTable
        Dim testData As DataTable = New DataTable("TestTable")
        testData.Columns.Add("Main", GetType(String))
        testData.Columns.Add("Flag", GetType(Boolean))

        Dim newRow As DataRow = testData.NewRow()
        newRow.Item(0) = "Obscurest"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "night"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "involved"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        Return testData
    End Function

    Private Function GetComboBoxData() As DataTable
        Dim testData As DataTable = New DataTable("TestTable")
        testData.Columns.Add("Main", GetType(String))
        testData.Columns.Add("Flag", GetType(Boolean))

        Dim newRow As DataRow = testData.NewRow()
        newRow.Item(0) = "Obscurest"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "night"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "involved"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "the"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "sky"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "Th'"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "Atlantic"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "billows"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "roared"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "When"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "such"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "a"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "destined"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "wretch"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "as"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "I"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "washed"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "headlong"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "from"
        newRow.Item(1) = True
        testData.Rows.Add(newRow)

        newRow = testData.NewRow()
        newRow.Item(0) = "onboard"
        newRow.Item(1) = False
        testData.Rows.Add(newRow)

        Return testData
    End Function

    Private Sub SomeEvent_AAAHH(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Click
        'Me.MobileComboBox1.Invalidate()
    End Sub

    Private Sub MobileComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MobileComboBox1.SelectedIndexChanged
        Try
            ' If selected item
            If Me.MobileComboBox1.SelectedIndex >= 0 Then
                Dim locationData As DataRow = CType(Me.MobileComboBox1.DataSource, DataTable).Rows(Me.MobileComboBox1.SelectedIndex)
                ' Set some variables
                Dim mainLine As String = CType(locationData(0), String)
                Dim flagLine As String = CType(locationData(1), String)

                ' Redraw combobox
                Me.MobileComboBox1.Invalidate()
            End If
        Catch ex As Exception
            ' Display notification
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Exception Notification")
        End Try
    End Sub

End Class