Imports DigitalData.Modules.Windows
Imports DigitalData.Modules.Windows.Window

Public Class frmControlCapture
    Public Property TopLeft As RectangleInfo
    Public Property TopRight As RectangleInfo
    Public Property BottomLeft As RectangleInfo
    Public Property BottomRight As RectangleInfo

    Public Property ControlName As String = String.Empty
    Public Property ControlBounds As String

    'Private WithEvents Watcher As ClipboardWatcher = ClipboardWatcher.Singleton
    Private WithEvents Watcher2 As ClassClipboardWatcher = ClassClipboardWatcher.Singleton
    Private Window As Window

    Private EditMode As Boolean = False


    Public Sub New(EditMode As Boolean, Optional ControlBounds As String = "", Optional ControlName As String = "")
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        Me.EditMode = EditMode
        Me.ControlName = ControlName
        Me.ControlBounds = ControlBounds
    End Sub

    Private Sub frmControlCapture_Load(sender As Object, e As EventArgs) Handles Me.Load
        Window = New Window(LogConfig)

        rbControlName.Checked = True

        If ControlName <> String.Empty Then
            rbControlName.Checked = True
            txtControlName.Text = ControlName
        End If

        If ControlBounds <> String.Empty Then
            rbControlPosition.Checked = True
            txtControlBounds.Text = ControlBounds
        End If

        'AddHandler Watcher.Changed, AddressOf Watcher_Changed
        AddHandler Watcher2.Changed, AddressOf Watcher_Changed
    End Sub

    Private Sub frmControlCapture_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        RemoveHandler Watcher2.Changed, AddressOf Watcher_Changed
    End Sub

    Private Sub Watcher_Changed(sender As Object, e As String)
        Try
            ' === CONTROL NAME ===
            Dim oControl As WindowInfo = Window.GetFocusedControl(Handle)

            If oControl IsNot Nothing Then
                txtControlName.Text = oControl.ControlName
                ControlName = oControl.ControlName
            End If
        Catch ex As Exception
            Logger.Error(ex)
            MsgBox($"Control Name konnte nicht ausgelesen werden!{vbNewLine}Dies kann ein temporärer Fehler sein. Bitte versuchen Sie es noch einmal.", MsgBoxStyle.Exclamation, Text)
        End Try

        Try
            ' === CONTROL POSITION ===
            Dim oRectangles As Dictionary(Of String, RectangleInfo) = Window.GetFocusedControlLocation(Handle)

            For Each oRect As KeyValuePair(Of String, RectangleInfo) In oRectangles
                Select Case oRect.Key
                    Case Window.Anchor.TopLeft.ToString
                        If oRect.Value IsNot Nothing Then
                            TopLeft = oRect.Value
                        End If

                    Case Window.Anchor.TopRight.ToString
                        If oRect.Value IsNot Nothing Then
                            TopRight = oRect.Value
                        End If

                    Case Window.Anchor.BottomLeft.ToString
                        If oRect.Value IsNot Nothing Then
                            BottomLeft = oRect.Value
                        End If

                    Case Window.Anchor.BottomRight.ToString
                        If oRect.Value IsNot Nothing Then
                            BottomRight = oRect.Value
                        End If
                End Select
            Next

            txtControlBounds.Text = GetBoundsString(TopLeft, TopRight, BottomLeft, BottomRight)
        Catch ex As Exception
            Logger.Error(ex)
            MsgBox($"Control Koordinaten konnten nicht ausgelesen werden!{vbNewLine}Dies kann ein temporärer Fehler sein. Bitte versuchen Sie es noch einmal.", MsgBoxStyle.Exclamation, Text)
        End Try

        UpdateOKButton()
    End Sub

    Public Shared Function GetBoundsString(TopLeft As RectangleInfo, TopRight As RectangleInfo, BottomLeft As RectangleInfo, BottomRight As RectangleInfo)
        Dim oResult As String = String.Empty

        If TopLeft IsNot Nothing Then
            oResult &= TopLeft.ToString & vbNewLine
        End If
        If TopRight IsNot Nothing Then
            oResult &= TopRight.ToString & vbNewLine
        End If
        If BottomLeft IsNot Nothing Then
            oResult &= BottomLeft.ToString & vbNewLine
        End If
        If BottomRight IsNot Nothing Then
            oResult &= BottomRight.ToString & vbNewLine
        End If

        Return oResult
    End Function

    Private Sub UpdateOKButton()
        If rbControlName.Checked Then
            txtControlName.Enabled = rbControlName.Checked
            txtControlBounds.Enabled = Not rbControlName.Checked

            btnOK.Enabled = rbControlName.Checked And ControlName <> String.Empty
        ElseIf rbControlPosition.Checked Then
            txtControlBounds.Enabled = rbControlPosition.Checked
            txtControlName.Enabled = Not rbControlPosition.Checked

            btnOK.Enabled = rbControlPosition.Checked And TopLeft IsNot Nothing
        End If
    End Sub


    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles rbControlName.CheckedChanged
        UpdateOKButton()
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles rbControlPosition.CheckedChanged
        UpdateOKButton()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        If rbControlPosition.Checked Then
            ControlName = String.Empty
        ElseIf rbControlName.Checked Then
            TopLeft = New RectangleInfo()
            TopRight = New RectangleInfo()
            BottomLeft = New RectangleInfo()
            BottomRight = New RectangleInfo()
        End If
    End Sub
End Class