Imports System.Threading
Imports System.Windows.Forms
Imports UTS_Core.UTSModule.Test.StatusMonitor.UtsKeyValueMonitor

Namespace UTSModule.Test.Controls
    Public Class UtsInputBox

        Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOk.Click
            InputText = TxtInputText.Text

            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 BtnCencle.Click
            InputText = Nothing

            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub

        Private _isPwd As Boolean = False

        Public Property TipText As String
        Public Property Title As String
        Public Property DefaultText As String

        ''' <summary>
        ''' 输入框值
        ''' </summary>
        ''' <returns></returns>
        Public Property InputText() As String

        ''' <summary>
        ''' 密码模式
        ''' </summary>
        ''' <returns></returns>
        Public Property IsPassWord As Boolean
            Get
                Return _isPwd
            End Get
            Set(value As Boolean)
                _isPwd = value
                If _isPwd Then
                    TxtInputText.PasswordChar = "*"c
                Else
                    TxtInputText.PasswordChar = ControlChars.NullChar
                End If
            End Set
        End Property

        Public Overloads Shared Function ShowDialog(tip As String, Optional title As String = "", Optional text As String = "", Optional isPwd As Boolean = False) As String

            Using box As New UtsInputBox
                box.TopLevel = True
                box.TipText = tip
                box.Title = title
                box.DefaultText = text
                box.IsPassWord = isPwd
                box.ShowDialog()
                Return box.InputText
            End Using
        End Function


        Private Sub UpdateKey(key As UtsKeyValueEnum)
            Try
                If BtnOk.InvokeRequired Then
                    BtnOk.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
                InputText = TxtInputText.Text

                Me.DialogResult = System.Windows.Forms.DialogResult.OK
                Me.Close()
            ElseIf key = UtsKeyValueEnum.No Then
                Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
                Me.Close()
            End If
        End Sub

        Private Sub UtsInputBox_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Text = Title
            LblTip.Text = TipText
            If _isPwd Then
                TxtInputText.PasswordChar = "*"c
            Else
                TxtInputText.PasswordChar = ControlChars.NullChar
            End If
            TxtInputText.Text = DefaultText
            TxtInputText.Focus()
            TxtInputText.SelectAll()

            AcceptButton = BtnOk
            CancelButton = BtnCencle

            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf CheckKeyValue))
        End Sub

        Sub CheckKeyValue(obj As Object)
            While DialogResult = DialogResult.None
                UpdateKey(UtsKeyValue)
                System.Windows.Forms.Application.DoEvents()
                Thread.Sleep(50)
            End While
        End Sub

        Private Sub UtsInputBox_KeyDown(sender As Object, e As Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            If e.KeyCode = Keys.Enter Then
                BtnOk.PerformClick()
            End If
        End Sub
    End Class

End Namespace
