Option Explicit On
Option Strict On

Public Class frmTriggerEditor

    Private frmDesigner As frmDesigner

    ' Variables to hold selections
    Private conditionSubject As SelectedObject
    Private conditionObject As SelectedObject
    Private actionTarget As SelectedObject

    Private Sub frmTriggerEditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Get reference to frmDesigner
        For Each openForm As Form In Application.OpenForms
            If TypeOf openForm Is frmDesigner Then
                frmDesigner = CType(openForm, frmDesigner)
                Exit For
            End If
        Next

        If frmDesigner Is Nothing Then
            MessageBox.Show("frmDesigner is not open.")
            Me.Close()
            Return
        End If

        ' Initialize controls
        cboCreateRemove.Items.Add("Create")
        cboCreateRemove.Items.Add("Remove")

        lstItems.Items.AddRange(New String() {"Coin", "Blockstopper", "Key", "Warp", "Warp2"})

        trvTriggers.Nodes.Clear()

        ' Load existing triggers
        For Each trigger As GameTrigger In frmDesigner.gameTriggers
            Dim node As New TreeNode(FormatTriggerDescription(trigger))
            trvTriggers.Nodes.Add(node)
        Next
    End Sub

    Private Sub btnSelect1_Click(sender As Object, e As EventArgs) Handles btnSelect1.Click
        AddHandler frmDesigner.ObjectSelected, AddressOf Designer_ObjectSelected
        frmDesigner.SelectionMode = True
        frmDesigner.SelectionRequestedByForm = Me
        frmDesigner.SelectionRequestedFor = "Select1"
        Me.Enabled = False
    End Sub

    Private Sub btnSelect2_Click(sender As Object, e As EventArgs) Handles btnSelect2.Click
        AddHandler frmDesigner.ObjectSelected, AddressOf Designer_ObjectSelected
        frmDesigner.SelectionMode = True
        frmDesigner.SelectionRequestedByForm = Me
        frmDesigner.SelectionRequestedFor = "Select2"
        Me.Enabled = False
    End Sub

    Private Sub btnSelect3_Click(sender As Object, e As EventArgs) Handles btnSelect3.Click
        AddHandler frmDesigner.ObjectSelected, AddressOf Designer_ObjectSelected
        frmDesigner.SelectionMode = True
        frmDesigner.SelectionRequestedByForm = Me
        frmDesigner.SelectionRequestedFor = "Select3"
        Me.Enabled = False
    End Sub

    Private Sub Designer_ObjectSelected(selectedObject As SelectedObject)
        If frmDesigner.SelectionRequestedFor = "Select1" Then
            conditionSubject = selectedObject
            btnSelect1.Text = FormatSelectedObject(selectedObject)
        ElseIf frmDesigner.SelectionRequestedFor = "Select2" Then
            conditionObject = selectedObject
            btnSelect2.Text = FormatSelectedObject(selectedObject)
        ElseIf frmDesigner.SelectionRequestedFor = "Select3" Then
            actionTarget = selectedObject
            btnSelect3.Text = FormatSelectedObject(selectedObject)
        End If

        RemoveHandler frmDesigner.ObjectSelected, AddressOf Designer_ObjectSelected
        frmDesigner.SelectionMode = False
        frmDesigner.SelectionRequestedByForm = Nothing
        frmDesigner.SelectionRequestedFor = Nothing
        Me.Enabled = True
    End Sub

    Private Function FormatSelectedObject(selectedObject As SelectedObject) As String
        If selectedObject.Type = "Square" AndAlso selectedObject.Coordinates.HasValue Then
            Return $"Square ({selectedObject.Coordinates.Value.X}, {selectedObject.Coordinates.Value.Y})"
        ElseIf selectedObject.Type = "Item" Then
            Return selectedObject.ImageKey
        Else
            Return "Unknown"
        End If
    End Function

    Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
        ' Validate selections
        If conditionSubject Is Nothing Then
            MessageBox.Show("Please select the condition subject.")
            Return
        End If

        If conditionObject Is Nothing Then
            MessageBox.Show("Please select the condition object.")
            Return
        End If

        If cboCreateRemove.SelectedIndex = -1 Then
            MessageBox.Show("Please select an action (Create or Remove).")
            Return
        End If

        If lstItems.SelectedIndex = -1 Then
            MessageBox.Show("Please select an item.")
            Return
        End If

        If actionTarget Is Nothing Then
            MessageBox.Show("Please select the action target.")
            Return
        End If

        ' Get action details
        Dim actionType As String = cboCreateRemove.SelectedItem.ToString()
        Dim item As String = lstItems.SelectedItem.ToString()

        ' Create the trigger
        Dim trigger As New GameTrigger With {
            .Condition = New TriggerCondition With {
                .Subject = conditionSubject,
                .Action = "Enters",
                .ObjectRef = conditionObject
            },
            .Action = New TriggerAction With {
                .ActionType = actionType,
                .Item = item,
                .Target = actionTarget
            }
        }

        ' Add trigger to frmDesigner
        frmDesigner.gameTriggers.Add(trigger)

        ' Update TreeView
        Dim node As New TreeNode(FormatTriggerDescription(trigger))
        trvTriggers.Nodes.Add(node)

        ' Reset selections
        conditionSubject = Nothing
        conditionObject = Nothing
        actionTarget = Nothing
        btnSelect1.Text = "Select Object"
        btnSelect2.Text = "Select Square"
        btnSelect3.Text = "Select Target"
        cboCreateRemove.SelectedIndex = -1
        lstItems.ClearSelected()
    End Sub

End Class

'FormatTriggerDescription' is not declared. It may be inaccessible due to its protection level.
'FormatTriggerDescription' is not declared. It may be inaccessible due to its protection level.
'FormatTriggerDescription' is not declared. It may be inaccessible due to its protection level.
'FormatTriggerDescription' is not declared. It may be inaccessible due to its protection level.

Fixy!