Imports System.Drawing
Imports System.Windows.Forms
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow.Params

Public Class ProfileMatch
    Private _Logger As Logger

    Public Sub New(LogConfig As LogConfig)
        _Logger = LogConfig.GetLogger()
    End Sub

    Public Function FindNodeByTag(ByVal nodes As TreeNodeCollection, ByVal Tag As String) As TreeNode
        For Each node As TreeNode In nodes
            If (node.Tag.Equals(Tag)) Then
                Return node
            End If

            Dim oNext As TreeNode = FindNodeByTag(node.Nodes, Tag)
            If oNext IsNot Nothing Then
                Return oNext
            End If
        Next
        Return Nothing
    End Function

    Public Function NewProfileNode(Profile As ProfileData) As TreeNode
        _Logger.Debug("New Profile Node for Profile {0}", Profile.Name)

        Dim oNode As New TreeNode() With {
            .Text = $"Profile: {Profile.Name}",
            .ImageIndex = ProfileFilter.ImageIndex.Profile,
            .Tag = Profile.Name & "-PROFILE"
        }

        Return oNode
    End Function

    Public Function NewClipboardRegexNode(Profile As ProfileData, IsMatch As Boolean) As TreeNode
        _Logger.Debug("New Clipboard Regex Node for Profile {0}", Profile.Name)

        Dim oText = $"{GetMatchText(IsMatch)} on Global Clipboard Regex {Profile.Regex}"
        Dim oNode As New TreeNode() With {
            .Text = oText,
            .ImageIndex = ProfileFilter.ImageIndex.Clipboard,
            .Tag = Profile.Name & "-REGEX",
            .BackColor = GetMatchColor(IsMatch)
        }

        Return oNode
    End Function
    Public Function NewProcessNode(Profile As ProfileData, Process As ProcessData, IsMatch As Boolean, IsCatchAll As Boolean) As TreeNode
        _Logger.Debug("New Process Node for Profile {0} and ProcessName {1}", Profile.Name, Process.ProcessName)

        Dim oText As String
        If IsCatchAll Then
            oText = $"{GetMatchText(IsMatch)} on Catch-All Process"
        Else
            oText = $"{GetMatchText(IsMatch)} on ProcessName {Process.ProcessName}"
        End If

        Dim oNode As New TreeNode() With {
            .Text = oText,
            .ImageIndex = ProfileFilter.ImageIndex.Process,
            .Tag = Process.Guid & "-PROCESS",
            .BackColor = GetMatchColor(IsMatch)
        }

        Return oNode
    End Function

    Public Function NewWindowNode(Profile As ProfileData, Window As WindowData, IsMatch As Boolean) As TreeNode
        _Logger.Debug("New Window Node for Profile {0} and WindowTitle {1}", Profile.Name, Window.Title)

        Dim oMatchText = IIf(IsMatch, "MATCH", "NO MATCH")
        Dim oText = $"{GetMatchText(IsMatch)} on WindowTitle {Window.Title}"
        Dim oNode As New TreeNode() With {
            .Text = oText,
            .ImageIndex = ProfileFilter.ImageIndex.Window,
            .Tag = Window.Guid & "-WINDOW",
            .BackColor = GetMatchColor(IsMatch)
        }

        Return oNode
    End Function

    Public Function NewControlNode(Profile As ProfileData, Control As ControlData, IsMatch As Boolean) As TreeNode
        _Logger.Debug("New Control Node for Profile {0} and Control {1}", Profile.Name, Control.Description)

        Dim oMatchText = IIf(IsMatch, "MATCH", "NO MATCH")
        Dim oText = $"{GetMatchText(IsMatch)} on Control {Control.Description}"

        Dim oNode As New TreeNode() With {
            .Text = oText,
            .ImageIndex = ProfileFilter.ImageIndex.Control,
            .Tag = Control.Guid & "-CONTROL",
            .BackColor = GetMatchColor(IsMatch)
        }

        Return oNode
    End Function

    Private Function GetLowestNode(ByVal Node As TreeNode) As TreeNode
        If Node.GetNodeCount(False) = 1 Then
            Return GetLowestNode(Node.Nodes.Item(0))
        Else
            Return Node
        End If
    End Function

    Public Function GetMatchText(IsMatch As Boolean)
        Return IIf(IsMatch, "MATCH", "NO MATCH")
    End Function

    Public Function GetMatchColor(IsMatch As Boolean)
        Return IIf(IsMatch, Color.LightGreen, Color.LightCoral)
    End Function
End Class
