Imports System.Collections.Generic
Imports System.Drawing
Imports FarPoint.Win.Spread
Imports Spread.Common.DataStore
Imports Spread.Common.Features.Spread.Common.Features.Sample
Imports Spread.Filtering.Spread.Filtering

Namespace Features
    Partial Public Class CustomFilters
        Inherits BaseSampleControl
#Region "Fields"
        Private _dataSource As AccessDataSource
#End Region

#Region "ctor"
        Public Sub New()
            InitializeComponent()
            Name = "Custom Filters"
            _dataSource = New DataService().AccessDataSource

            InitSpreadSheet(fpSpread.ActiveSheet)
            InitControls()
        End Sub
#End Region

#Region "Private methods"
        Private Sub InitSpreadSheet(sheet As SheetView)
            sheet.DataSource = _dataSource.GetDataFrom("fpWine.mdb", "select OrderID, CustomerID," & "SalesPerson, OrderDate, ShippedDate, Freight from Orders")

            sheet.Columns(2).Width = 120
            sheet.Columns(3, 4).Width = 80
            sheet.Columns(5).Width = 70

            sheet.SetMultipleRowHeights(0, sheet.RowCount, 20)

            Dim random As New Random()
            Dim colors = [Enum].GetValues(GetType(KnownColor))
            Dim row As Integer = random.[Next](0, 4)
            While row < sheet.RowCount
                sheet.Cells(row, 1).BackColor = Color.FromKnownColor(CType(colors.GetValue(random.[Next](27, colors.GetUpperBound(0) + 1)), KnownColor))
                row += random.[Next](1, 5)
            End While
            sheet.AutoFilter = True

            Dim hideRowFilter As New HideRowFilter(sheet)
            For col As Integer = 0 To sheet.ColumnCount - 1
                hideRowFilter.ColumnDefinitions.Add(New FilterColumnDefinition(col, FilterListBehavior.[Custom]))
                sheet.Columns(col).Tag = sheet.Columns(col).Label
            Next
            sheet.RowFilter = hideRowFilter
            sheet.RowFilter.ShowFilterIndicator = False
        End Sub

        Private Sub InitControls()
            For Each day As DayOfWeek In [Enum].GetValues(GetType(DayOfWeek))
                checkedListBox.Items.Add(day, False)
            Next

            comboDateCol.Items.AddRange(New Object() {"OrderDate", "ShippedDate"})
            AddHandler comboDateCol.SelectedIndexChanged, AddressOf ComboDateCol_SelectedIndexChanged

            AddHandler btnApply.Click, AddressOf BtnApply_Click
            AddHandler btnReset.Click, AddressOf BtnReset_Click
        End Sub

        Private Sub ResetFilter(colIndex As Integer)
            fpSpread.ActiveSheet.AutoFilterReset(colIndex)

            For i As Integer = 0 To checkedListBox.Items.Count - 1
                checkedListBox.SetItemChecked(i, False)
            Next
        End Sub
#End Region

#Region "Event handlers"
        Private Sub ComboDateCol_SelectedIndexChanged(sender As Object, e As EventArgs)
            ResetFilter(3 + (comboDateCol.SelectedIndex + 1) Mod 2)
            btnApply.Enabled = (comboDateCol.SelectedIndex <> -1)
        End Sub

        Private Sub BtnApply_Click(sender As Object, e As EventArgs)
            Dim sheet As SheetView = fpSpread.ActiveSheet
            Dim colIndex As Integer = sheet.Columns(comboDateCol.SelectedItem.ToString()).Index

            Dim days As New List(Of DayOfWeek)()
            For i As Integer = 0 To checkedListBox.CheckedItems.Count - 1
                days.Add(CType(checkedListBox.CheckedItems(i), DayOfWeek))
            Next

            Dim fcd = sheet.RowFilter.GetFilterColumnDefinition(colIndex)
            fcd.Filters.Clear()
            Dim filter As New DayCustomFilter(sheet, days.ToArray())
            fcd.Filters.Add(filter)

            sheet.AutoFilterColumn(colIndex, filter.DisplayName, 0)

            btnReset.Enabled = True
        End Sub

        Private Sub BtnReset_Click(sender As Object, e As EventArgs)
            Dim sheet As SheetView = fpSpread.ActiveSheet
            Dim colIndex As Integer = sheet.Columns(comboDateCol.SelectedItem.ToString()).Index
            ResetFilter(colIndex)

            btnReset.Enabled = False
        End Sub
#End Region
    End Class
End Namespace

'
'CustomFilters.Designer.vb
'

Namespace Features
	Partial Class CustomFilters
		''' <summary> 
		''' Required designer variable.
		''' </summary>
		Private components As System.ComponentModel.IContainer = Nothing

        ''' <summary> 
        ''' Clean up any resources being used.
        ''' </summary>
        ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        Protected Shadows Sub Dispose(disposing As Boolean)
            If disposing AndAlso (components IsNot Nothing) Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub

#Region "Component Designer generated code"

        ''' <summary> 
        ''' Required method for Designer support - do not modify 
        ''' the contents of this method with the code editor.
        ''' </summary>
        Private Sub InitializeComponent()
			Me.fpSpread = New FarPoint.Win.Spread.FpSpread()
			Me.fpSpread_Sheet1 = New FarPoint.Win.Spread.SheetView()
			Me.groupBox1 = New System.Windows.Forms.GroupBox()
			Me.btnReset = New System.Windows.Forms.Button()
			Me.btnApply = New System.Windows.Forms.Button()
			Me.checkedListBox = New System.Windows.Forms.CheckedListBox()
			Me.comboDateCol = New System.Windows.Forms.ComboBox()
			Me.label1 = New System.Windows.Forms.Label()
			Me.pnlMain.SuspendLayout()
			Me.pnlControls.SuspendLayout()
			DirectCast(Me.fpSpread, System.ComponentModel.ISupportInitialize).BeginInit()
			DirectCast(Me.fpSpread_Sheet1, System.ComponentModel.ISupportInitialize).BeginInit()
			Me.groupBox1.SuspendLayout()
			Me.SuspendLayout()
			' 
			' pnlMain
			' 
			Me.pnlMain.Controls.Add(Me.fpSpread)
			' 
			' pnlControls
			' 
			Me.pnlControls.Controls.Add(Me.groupBox1)
			' 
			' fpSpread
			' 
			Me.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill
			Me.fpSpread.Location = New System.Drawing.Point(0, 0)
			Me.fpSpread.Name = "fpSpread"
			Me.fpSpread.Sheets.AddRange(New FarPoint.Win.Spread.SheetView() {Me.fpSpread_Sheet1})
			Me.fpSpread.Size = New System.Drawing.Size(477, 487)
			Me.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White
			Me.fpSpread.StatusBarVisible = True
			Me.fpSpread.TabIndex = 0
			' 
			' fpSpread_Sheet1
			' 
			Me.fpSpread_Sheet1.Reset()
			Me.fpSpread_Sheet1.SheetName = "Sheet1"
			' Formulas and custom names must be loaded with R1C1 reference style
			Me.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
			Me.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1
			' 
			' groupBox1
			' 
			Me.groupBox1.Controls.Add(Me.btnReset)
			Me.groupBox1.Controls.Add(Me.btnApply)
			Me.groupBox1.Controls.Add(Me.checkedListBox)
			Me.groupBox1.Controls.Add(Me.comboDateCol)
			Me.groupBox1.Controls.Add(Me.label1)
			Me.groupBox1.Dock = System.Windows.Forms.DockStyle.Top
			Me.groupBox1.Location = New System.Drawing.Point(0, 0)
			Me.groupBox1.Name = "groupBox1"
			Me.groupBox1.Size = New System.Drawing.Size(157, 284)
			Me.groupBox1.TabIndex = 0
			Me.groupBox1.TabStop = False
			Me.groupBox1.Text = "DayOfWeek Filter"
			' 
			' btnReset
			' 
			Me.btnReset.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.btnReset.Enabled = False
			Me.btnReset.Location = New System.Drawing.Point(42, 218)
			Me.btnReset.Name = "btnReset"
			Me.btnReset.Size = New System.Drawing.Size(81, 23)
			Me.btnReset.TabIndex = 4
			Me.btnReset.Text = "Reset"
			Me.btnReset.UseVisualStyleBackColor = True
			' 
			' btnApply
			' 
			Me.btnApply.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.btnApply.Enabled = False
			Me.btnApply.Location = New System.Drawing.Point(42, 187)
			Me.btnApply.Name = "btnApply"
			Me.btnApply.Size = New System.Drawing.Size(81, 23)
			Me.btnApply.TabIndex = 3
			Me.btnApply.Text = "Apply"
			Me.btnApply.UseVisualStyleBackColor = True
			' 
			' checkedListBox
			' 
			Me.checkedListBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.checkedListBox.CheckOnClick = True
			Me.checkedListBox.FormattingEnabled = True
			Me.checkedListBox.Location = New System.Drawing.Point(9, 60)
			Me.checkedListBox.Name = "checkedListBox"
			Me.checkedListBox.Size = New System.Drawing.Size(142, 109)
			Me.checkedListBox.TabIndex = 2
			' 
			' comboDateCol
			' 
			Me.comboDateCol.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.comboDateCol.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
			Me.comboDateCol.FormattingEnabled = True
			Me.comboDateCol.Location = New System.Drawing.Point(57, 23)
			Me.comboDateCol.Name = "comboDateCol"
			Me.comboDateCol.Size = New System.Drawing.Size(94, 21)
			Me.comboDateCol.TabIndex = 1
			' 
			' label1
			' 
			Me.label1.AutoSize = True
			Me.label1.Location = New System.Drawing.Point(6, 26)
			Me.label1.Name = "label1"
			Me.label1.Size = New System.Drawing.Size(45, 13)
			Me.label1.TabIndex = 0
			Me.label1.Text = "Column:"
			' 
			' CustomFilters
			' 
			Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
			Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
			Me.Name = "CustomFilters"
			Me.pnlMain.ResumeLayout(False)
			Me.pnlControls.ResumeLayout(False)
			DirectCast(Me.fpSpread, System.ComponentModel.ISupportInitialize).EndInit()
			DirectCast(Me.fpSpread_Sheet1, System.ComponentModel.ISupportInitialize).EndInit()
			Me.groupBox1.ResumeLayout(False)
			Me.groupBox1.PerformLayout()
			Me.ResumeLayout(False)

		End Sub

		#End Region

		Private fpSpread As FarPoint.Win.Spread.FpSpread
		Private fpSpread_Sheet1 As FarPoint.Win.Spread.SheetView
		Private groupBox1 As System.Windows.Forms.GroupBox
		Private btnApply As System.Windows.Forms.Button
		Private checkedListBox As System.Windows.Forms.CheckedListBox
		Private comboDateCol As System.Windows.Forms.ComboBox
		Private label1 As System.Windows.Forms.Label
		Private btnReset As System.Windows.Forms.Button
	End Class
End Namespace
