Imports DevExpress.XtraEditors
Imports DevExpress.XtraSplashScreen
Imports DigitalData.GUIs.Common
Imports DigitalData.GUIs.ZooFlow.ClassConstants
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow

Public Class frmFlowSearch1
    Private Environment As Environment
    Private Logger As Logger
    Private SearchRunner As Search.SearchRunner

    Private Property BaseSearchSQL As String
    Private Property TogglesDate As List(Of ToggleSwitch)
    Private Property TogglesDateChanging As Boolean = False

    Private Sub ResetTogglesDate()
        ResetTogglesDate(Nothing)
    End Sub

    Private Sub ResetTogglesDate(pExcludeToggle As ToggleSwitch)
        TogglesDateChanging = True

        For Each oToggle As ToggleSwitch In TogglesDate
            If pExcludeToggle IsNot Nothing Then
                If oToggle.Name = pExcludeToggle.Name Then
                    Continue For
                End If
            End If
            oToggle.IsOn = False
        Next

        TogglesDateChanging = False
    End Sub

    Private Sub ToggleSwitchToday_Toggled(sender As Object, e As EventArgs) Handles _
        ToggleSwitchToday.Toggled,
        ToggleSwitchYesterday.Toggled,
        ToggleSwitchYear.Toggled,
        ToggleSwitchYear_Last.Toggled,
        ToggleSwitchMonth_Last.Toggled,
        ToggleSwitchMonth.Toggled,
        ToggleSwitchLastWeek.Toggled,
        ToggleSwitchTomorrow.Toggled

        If TogglesDateChanging = True Then
            Exit Sub
        End If

        Dim oToggle As ToggleSwitch = TryCast(sender, ToggleSwitch)

        If oToggle.IsOn Then
            TogglesDateChanging = True

            Select Case oToggle.Name
                Case "ToggleSwitchTomorrow"
                    ResetTogglesDate(ToggleSwitchTomorrow)
                    SearchRunner.SetDateConstraint(SearchRunner.DateConstraint.Tomorrow)

                Case "ToggleSwitchYear"
                    ResetTogglesDate(ToggleSwitchYear)
                    SearchRunner.SetDateConstraint(SearchRunner.DateConstraint.CurrentYear)

                Case "ToggleSwitchYear_Last"
                    ResetTogglesDate(ToggleSwitchYear_Last)
                    SearchRunner.SetDateConstraint(SearchRunner.DateConstraint.Today)

                Case "ToggleSwitchMonth"
                    ResetTogglesDate(ToggleSwitchMonth)
                    SearchRunner.SetDateConstraint(SearchRunner.DateConstraint.CurrentMonth)

                Case "ToggleSwitchMonth_Last"
                    ResetTogglesDate(ToggleSwitchMonth_Last)
                    SearchRunner.SetDateConstraint(SearchRunner.DateConstraint.LastMonth)

                Case "ToggleSwitchToday"
                    ResetTogglesDate(ToggleSwitchToday)
                    SearchRunner.SetDateConstraint(SearchRunner.DateConstraint.Today)

                Case "ToggleSwitchYesterday"
                    ResetTogglesDate(ToggleSwitchYesterday)
                    SearchRunner.SetDateConstraint(SearchRunner.DateConstraint.Yesterday)

                Case "ToggleSwitchLastWeek"
                    ResetTogglesDate(ToggleSwitchLastWeek)
                    SearchRunner.SetDateConstraint(SearchRunner.DateConstraint.Last7Days)

            End Select
            TogglesDateChanging = False

        Else
            If ToggleSwitchToday.IsOn = False And ToggleSwitchYesterday.IsOn = False And ToggleSwitchYear.IsOn = False And ToggleSwitchYear_Last.IsOn = False And
                ToggleSwitchMonth_Last.IsOn = False And ToggleSwitchMonth.IsOn = False And ToggleSwitchLastWeek.IsOn = False And ToggleSwitchTomorrow.IsOn = False Then
                SearchRunner.SetDateConstraint()
            End If
        End If
    End Sub



    Private Sub cmbLimitationDateAttr_SelectedValueChanged(sender As Object, e As EventArgs) Handles cmbLimitationDateAttr.SelectedValueChanged
        If cmbLimitationDateAttr.SelectedIndex <> -1 Then
            Try

                Dim oEditValue As String = cmbLimitationDateAttr.SelectedItem.ToString

                If oEditValue = IDB_ADDED_WHEN_String_German Or oEditValue = IDB_ADDED_WHEN_String_Englisch Then
                    SearchRunner.SetDateAttribute()
                Else
                    SearchRunner.SetDateAttribute(oEditValue)
                End If
            Catch ex As Exception
                Logger.Error(ex)
            End Try
        End If
    End Sub

    Private Sub frmFlowSearch1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Logger = My.LogConfig.GetLogger()
        SearchRunner = New Search.SearchRunner(My.LogConfig, My.Application.GetEnvironment, "FlowSearch")

        Try
            If My.Application.User.Language = "de-DE" Then
                cmbLimitationDateAttr.Items.Add(IDB_ADDED_WHEN_String_German)
            Else
                cmbLimitationDateAttr.Items.Add(IDB_ADDED_WHEN_String_Englisch)
            End If

            For Each oRow As DataRow In My.Tables.DTIDB_ATTRIBUTE.Rows
                If CBool(oRow.Item("SYS_ATTRIBUTE")) = False And (oRow.Item("TYP_ID") = 5 Or oRow.Item("TYP_ID") = 6) Then
                    cmbLimitationDateAttr.Items.Add(oRow.Item("ATTR_TITLE"))
                End If

            Next
            Environment = My.Application.GetEnvironment
            'Dim editor As BarEditItem = BarEditItemDate
            ' editor = TryCast(BarEditItemDate, ComboBoxEdit)
            If My.Application.User.Language = "de-DE" Then
                cmbLimitationDateAttr.SelectedIndex = 0
                '    BarEditItemDate.EditValue = "Erstellt Wann"
            Else
                cmbLimitationDateAttr.SelectedIndex = 1
                '    BarEditItemDate.EditValue = "Added When"
            End If

            For Each oRow As DataRow In My.Tables.DTIDB_COMMON_SQL.Rows
                If oRow.Item("TITLE") = SQLCMD_FLOW_SEARCH_BASE Then
                    BaseSearchSQL = oRow.Item("SQL_COMMAND")
                    BaseSearchSQL = BaseSearchSQL.Replace("@USER_ID", My.Application.User.UserId)
                    BaseSearchSQL = BaseSearchSQL.Replace("@LANG_CODE", My.Application.User.Language)
                End If
            Next

            SearchRunner.BaseSearchSQL = BaseSearchSQL


            Dim osql = $"EXEC PRIDB_SEARCH_AUTOSUGGEST '{My.Application.User.Language}',{My.Application.User.UserId}"
            Dim oDTSuggest As DataTable
            oDTSuggest = My.Database.GetDatatableIDB(osql)
            Dim collection As AutoCompleteStringCollection = New AutoCompleteStringCollection
            For Each orow As DataRow In oDTSuggest.Rows
                collection.Add(orow.Item("TERM"))
            Next

            TextEditSearch.MaskBox.AutoCompleteSource = AutoCompleteSource.CustomSource
            TextEditSearch.MaskBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend
            TextEditSearch.MaskBox.AutoCompleteCustomSource = collection

            TogglesDate = New List(Of ToggleSwitch) From {
                ToggleSwitchTomorrow,
                ToggleSwitchToday,
                ToggleSwitchYesterday,
                ToggleSwitchYear,
                ToggleSwitchYear_Last,
                ToggleSwitchMonth,
                ToggleSwitchMonth_Last,
                ToggleSwitchLastWeek
            }


        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            Logger.Error(ex)
        End Try
        BringToFront()
    End Sub

    Private Sub Form_NeedsRefresh(sender As Object, e As Integer)

    End Sub

    Private Sub BarButtonReset_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonReset.ItemClick
        TextEditSearch.Text = ""
        ResetTogglesDate()
        ToggleSwitchDateto.IsOn = False
        SearchRunner.ExplicitDate = False
    End Sub

    Private Sub ToggleSwitch1_Toggled(sender As Object, e As EventArgs) Handles ToggleSwitchDateto.Toggled
        Dim oToggle As ToggleSwitch = TryCast(sender, ToggleSwitch)

        If oToggle.IsOn Then
            DateEditTill.Enabled = True
            lblDatefrom.Text = "Datum von"
        Else
            DateEditTill.Enabled = False
            lblDatefrom.Text = "Datum"
        End If
    End Sub

    Private Sub DateEdit1_EditValueChanged(sender As Object, e As EventArgs) Handles DateEditFrom.EditValueChanged
        Dim oDateFrom = DateEditFrom.EditValue
        If Not IsNothing(oDateFrom) Then
            ResetTogglesDate()
            SearchRunner.ExplicitDate = True
        End If
    End Sub

    Private Async Sub SimpleButton1_Click(sender As Object, e As EventArgs)
        RunSearch()
    End Sub
    Private Async Sub RunSearch()
        'Dim oHandle = SplashScreenManager.ShowOverlayForm(Me)

        'Try
        '    Dim oResult = Await SearchRunner.RunWithSearchTerm(TextEditSearch.Text, DateEditFrom.EditValue, DateEditTill.EditValue)
        '    If oResult.OK = False Then
        '        bsiStatus.Caption = oResult.ErrorMessage
        '    End If
        'Catch ex As Exception
        '    Logger.Error(ex)
        'Finally
        '    SplashScreenManager.CloseOverlayForm(oHandle)
        'End Try
    End Sub

    Private Sub TextEditSearch_KeyUp(sender As Object, e As KeyEventArgs) Handles TextEditSearch.KeyUp
        If e.KeyCode = Keys.Return Then
            If TextEditSearch.Text = String.Empty Then
                Return
            End If

            RunSearch()
        End If
    End Sub

    Private Sub PictureEdit1_Click(sender As Object, e As EventArgs) Handles PictureEdit1.Click
        RunSearch()
    End Sub

    Private Async Function BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) As Threading.Tasks.Task Handles BarButtonStartSearch.ItemClick
        'Dim oHandle = SplashScreenManager.ShowOverlayForm(Me)

        'Try
        '    Dim oResult = Await SearchRunner.RunWithSearchTerm(TextEditSearch.Text, DateEditFrom.EditValue, DateEditTill.EditValue)
        '    If oResult.OK = False Then
        '        bsiStatus.Caption = oResult.ErrorMessage
        '    End If
        'Catch ex As Exception
        '    Logger.Error(ex)
        'Finally
        '    SplashScreenManager.CloseOverlayForm(oHandle)
        'End Try
    End Function
End Class