Imports UTS_Core.UTSModule.Test.StatusMonitor.UtsKeyValueMonitor

Namespace UTSModule.Test.Controls
    Public Class UtsMsgBox
        Enum UtsMsgBoxTypeEnum
            OkOnly = 0
            YesNo = 4
        End Enum


        Public Property MsgType() As UtsMsgBoxTypeEnum
        Public Property MsgText() As String
        Public Property MsgTitle() As String


        Public Overloads Shared Function ShowDialog(text As String, Optional type As UtsMsgBoxTypeEnum = UtsMsgBoxTypeEnum.OkOnly, Optional title As String = "") As System.Windows.Forms.DialogResult

            Using msg As New UtsMsgBox
                msg.TopLevel = True
                msg.MsgText = text
                msg.MsgType = type
                msg.MsgTitle = title

                Return msg.ShowDialog()
            End Using
        End Function

        Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnYes.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub

        Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNo.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub

        Private Sub UtsMsgBox_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Text = MsgTitle
            LblTipText.Text = MsgText



            If MsgType = UtsMsgBoxTypeEnum.OkOnly Then
                AcceptButton = BtnYes
                BtnNo.Visible = False
                BtnYes.Text = $"确定"
                BtnYes.Width += BtnNo.Width

            ElseIf MsgType = UtsMsgBoxTypeEnum.YesNo Then
                AcceptButton = BtnYes
                CancelButton = BtnNo

                BtnYes.Text = $"是"
                BtnNo.Text = $"否"
            End If

            AddHandler StatusMonitor.UtsKeyValueMonitor.UtsKeyDown, AddressOf UtsKeyDownCallback
        End Sub


        Private Sub UtsKeyDownCallback(sender As Object, e As StatusMonitor.UtsKeyDownEventArgs)
            If BtnYes.InvokeRequired Then
                BtnYes.Invoke(New Action(Of StatusMonitor.UtsKeyValueMonitor.UtsKeyValueEnum)(AddressOf UpdateKey), New Object() {e.KeyValue})
            Else
                UpdateKey(e.KeyValue)
            End If
        End Sub

        Private Sub UpdateKey(key As UtsKeyValueEnum)
            Try
                If BtnYes.InvokeRequired Then
                    BtnYes.Invoke(New Action(Of UtsKeyValueEnum)(AddressOf UpdateKey), New Object() {key})
                    Return
                End If

            Catch ex As Exception
                Return
            End Try

            If key = UtsKeyValueEnum.Yes Then
                Me.DialogResult = System.Windows.Forms.DialogResult.OK
                Me.Close()
            ElseIf key = UtsKeyValueEnum.No Then
                If MsgType = UtsMsgBoxTypeEnum.YesNo Then
                    Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
                    Me.Close()
                End If
            End If
        End Sub
    End Class
End Namespace
