Option Explicit On
Option Strict On

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO
Imports System.Text.Json
Imports System.Linq

Public Class frmDesigner
    Inherits Form

#Region "Variables"

    Private isSelectionReadyToDrag As Boolean = False
    Private isDraggingSelection As Boolean = False
    Private selectionOffset As Point ' The offset between mouse position and selection rectangle origin
    Private selectionData As SelectionData
    Private selectionStartCell As Point ' The grid cell coordinates corresponding to selectionRectangle's original position

    Private Const INDENT As Integer = 20
    Friend SQCOUNTX As Integer = 16
    Friend SQCOUNTY As Integer = 11
    Private Const WALL_THICKNESS As Integer = 10

    Friend wallsHatchStyle As HatchStyle = HatchStyle.Cross
    Friend wallsColor1 As Color = Color.Gray
    Friend wallsColor2 As Color = Color.DarkGray
    Friend floorsHatchStyle As HatchStyle = HatchStyle.SmallCheckerBoard
    Friend floorsColor1 As Color = Color.LightGray
    Friend floorsColor2 As Color = Color.White

    Friend floorsUseEdgeStyle()() As Boolean ' New array to track edge style usage

    Private SQSizeX As Integer
    Private SQSizeY As Integer

    Private isRightMouseDown As Boolean = False
    Private isLeftMouseDown As Boolean = False
    Private isMouseInsideGrid As Boolean = False

    Private highlightedX As Integer
    Private highlightedY As Integer
    Private highlightedWallX As Boolean = False
    Private highlightedWallY As Boolean = False
    Private highlightColor As Color = Color.Black
    Private highlightTimer As Timer

    Private isRestoringState As Boolean = False
    Private isActionInProgress As Boolean = False
    Private undoStack As New Stack(Of RoomState)
    Private redoStack As New Stack(Of RoomState)

    Private ReadOnly projectDir As String = GetProjectDirectory()
    Private ReadOnly premadesFolder As String = Path.Combine(projectDir, "Data", "Premades")
    Private ReadOnly savesFolder As String = Path.Combine(projectDir, "Data", "Saves")
    Private ReadOnly musicFolder As String = Path.Combine(projectDir, "Data", "Music")
    Private currentFilePath As String = ""
    Friend SelectedMusic As String = ""

    Friend verticalWalls()() As Boolean
    Friend horizontalWalls()() As Boolean

    Friend placedItems()() As PlacedItem
    Friend verticalWallItems()() As PlacedItem
    Friend horizontalWallItems()() As PlacedItem

    Private animatedImages As New List(Of Image)
    Private animationTimer As Timer

    Private isDraggingItem As Boolean = False
    Private draggedItem As PlacedItem = Nothing
    Private draggedItemIsWall As Boolean = False
    Private draggedItemIsVerticalWall As Boolean = False
    Private draggedItemOriginalX As Integer
    Private draggedItemOriginalY As Integer

    Private mouseX As Integer
    Private mouseY As Integer

    Private rand As New Random()

    Private rightMouseDownPosition As Point
    Private rightMouseDownTime As DateTime
    Private Const CLICK_DRAG_THRESHOLD As Integer = 5 ' pixels
    Private Const CLICK_DRAG_THRESHOLD_SQUARED As Integer = CLICK_DRAG_THRESHOLD * CLICK_DRAG_THRESHOLD

    Private isSelecting As Boolean = False
    Private isInSelectionMode As Boolean = False
    Private selectionStartPoint As Point
    Private selectionEndPoint As Point
    Private selectionRectangle As Rectangle
#End Region

#Region "Constructor and Initialization"

    Public Sub New()
        InitializeComponent()
        InitializeWalls()
        InitializeTimers()
        InitializeEventHandlers()
        LoadPremades()
        LoadMusic()
        InitializeAnimationForImages()
        If Not isRestoringState Then SaveStateForUndo()
        Me.KeyPreview = True

    End Sub
    Private Sub frmDesigner_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        ' Check for Ctrl+Z (Undo)
        If e.Control AndAlso e.KeyCode = Keys.Z Then
            ' Call your existing Undo method
            btnUndo_Click(sender, e)
            e.SuppressKeyPress = True ' Prevent further processing
        End If

        ' Check for Ctrl+Y (Redo)
        If e.Control AndAlso e.KeyCode = Keys.Y Then
            ' Call your existing Redo method
            btnRedo_Click(sender, e)
            e.SuppressKeyPress = True
        End If
    End Sub
    Private Function GetProjectDirectory() As String
        Dim dir As DirectoryInfo = New DirectoryInfo(Application.StartupPath)
        For i As Integer = 1 To 4 ' Adjust this number based on your directory structure
            If dir.Parent IsNot Nothing Then
                dir = dir.Parent
            End If
        Next
        Return dir.FullName
    End Function

    Friend Sub InitializeWalls()
        ' Initialize verticalWalls and verticalWallItems
        verticalWalls = New Boolean(SQCOUNTX)() {}
        verticalWallItems = New PlacedItem(SQCOUNTX)() {}
        For x As Integer = 0 To SQCOUNTX
            verticalWalls(x) = New Boolean(SQCOUNTY - 1) {}
            verticalWallItems(x) = New PlacedItem(SQCOUNTY - 1) {}
            For y As Integer = 0 To SQCOUNTY - 1
                verticalWalls(x)(y) = True
                verticalWallItems(x)(y) = Nothing
            Next
        Next

        ' Initialize horizontalWalls and horizontalWallItems
        horizontalWalls = New Boolean(SQCOUNTX - 1)() {}
        horizontalWallItems = New PlacedItem(SQCOUNTX - 1)() {}
        For x As Integer = 0 To SQCOUNTX - 1
            horizontalWalls(x) = New Boolean(SQCOUNTY) {}
            horizontalWallItems(x) = New PlacedItem(SQCOUNTY) {}
            For y As Integer = 0 To SQCOUNTY
                horizontalWalls(x)(y) = True
                horizontalWallItems(x)(y) = Nothing
            Next
        Next

        ' Initialize placedItems
        placedItems = New PlacedItem(SQCOUNTX - 1)() {}
        For x As Integer = 0 To SQCOUNTX - 1
            placedItems(x) = New PlacedItem(SQCOUNTY - 1) {}
            For y As Integer = 0 To SQCOUNTY - 1
                placedItems(x)(y) = Nothing
            Next
        Next

        ' Initialize floorsUseEdgeStyle
        floorsUseEdgeStyle = New Boolean(SQCOUNTX - 1)() {}
        For x As Integer = 0 To SQCOUNTX - 1
            floorsUseEdgeStyle(x) = New Boolean(SQCOUNTY - 1) {}
            For y As Integer = 0 To SQCOUNTY - 1
                floorsUseEdgeStyle(x)(y) = False
            Next
        Next
    End Sub
    Private Sub InitializeTimers()
        highlightTimer = New Timer() With {.Interval = 500}
        AddHandler highlightTimer.Tick, AddressOf HighlightTimer_Tick
        highlightTimer.Start()

        animationTimer = New Timer() With {.Interval = 100}
        AddHandler animationTimer.Tick, AddressOf OnAnimationTick
        animationTimer.Start()
    End Sub

    Private Sub InitializeEventHandlers()
        AddColorPictureBoxHandlers(Me)

        ' Add handlers for item picture boxes
        AddHandler picBlock1.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picBaddy1.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picBaddy2.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picLazer1.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picLazer2.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picFlipper1.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picFlipper2.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picFlipper3.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picFlipper4.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picBaddy3.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picBaddy4.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picCoin.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picGate.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picKey.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picStart.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picFinish.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picBlockStopper1.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picBall.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picBlockSwitch.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picBomb.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picWarp.MouseDown, AddressOf PicBlock_MouseDown
        AddHandler picWarp2.MouseDown, AddressOf PicBlock_MouseDown

        ' Add handlers for form-level mouse events
        AddHandler Me.MouseMove, AddressOf Form_MouseMove
        AddHandler Me.MouseUp, AddressOf Form_MouseUp

        ' Add handlers for picGame
        picGame.AllowDrop = True
        AddHandler picGame.MouseDown, AddressOf PicGame_MouseDown
        AddHandler picGame.MouseUp, AddressOf PicGame_MouseUp
        AddHandler picGame.MouseMove, AddressOf PicGame_MouseMove
        AddHandler picGame.Paint, AddressOf PicGame_Paint

        ' Add handlers for buttons
        AddHandler btnUndo.Click, AddressOf btnUndo_Click
        AddHandler btnRedo.Click, AddressOf btnRedo_Click
        AddHandler btnAll.Click, AddressOf btnAll_Click
        AddHandler btnWall.Click, AddressOf btnWalls_Click
        AddHandler btnFloor.Click, AddressOf btnFloors_Click
        AddHandler btnSavePremade.Click, AddressOf btnSavePremade_Click

        ' Add handler for premades list
        AddHandler lstPremades.SelectedIndexChanged, AddressOf lstPremades_SelectedIndexChanged

        ' Add handler for hatch styles list
        AddHandler lstHatchstyles.SelectedIndexChanged, AddressOf lstHatchstyles_SelectedIndexChanged
        AddHandler cboMusic.SelectedIndexChanged, AddressOf cboMusic_SelectedIndexChanged

        For Each ctl As Control In tabFloor1.Controls
            If TypeOf ctl Is PictureBox Then
                AddHandler ctl.MouseEnter, AddressOf PictureBox_MouseEnter
                AddHandler ctl.MouseLeave, AddressOf PictureBox_MouseLeave
            End If
        Next

        For Each ctl As Control In tabFloor2.Controls
            If TypeOf ctl Is PictureBox Then
                AddHandler ctl.MouseEnter, AddressOf PictureBox_MouseEnter
                AddHandler ctl.MouseLeave, AddressOf PictureBox_MouseLeave
            End If
        Next

        For Each ctl As Control In tabWalls.Controls
            If TypeOf ctl Is PictureBox Then
                AddHandler ctl.MouseEnter, AddressOf PictureBox_MouseEnter
                AddHandler ctl.MouseLeave, AddressOf PictureBox_MouseLeave
            End If
        Next
    End Sub
    Private Sub cboX_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboX.SelectedIndexChanged
        UpdateGridSize()
    End Sub

    Private Sub cboY_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboY.SelectedIndexChanged
        UpdateGridSize()
    End Sub
    Private Sub UpdateGridSize()
        ' Get new grid sizes from combo boxes
        Dim newSQCOUNTX As Integer
        Dim newSQCOUNTY As Integer
        If Integer.TryParse(cboX.SelectedItem?.ToString(), newSQCOUNTX) AndAlso Integer.TryParse(cboY.SelectedItem?.ToString(), newSQCOUNTY) Then
            ' Save current state for undo before changing the grid size
            SaveState(True)

            ' Store old grid sizes
            Dim oldSQCOUNTX As Integer = SQCOUNTX
            Dim oldSQCOUNTY As Integer = SQCOUNTY

            ' Update grid size variables
            SQCOUNTX = newSQCOUNTX
            SQCOUNTY = newSQCOUNTY

            ' Resize the arrays while keeping existing data
            ResizeArrays(oldSQCOUNTX, oldSQCOUNTY, SQCOUNTX, SQCOUNTY)

            ' Recalculate square sizes based on new grid dimensions
            CalculateSquareSizes(picGame.ClientSize.Width, picGame.ClientSize.Height)

            ' Invalidate the drawing area to redraw the grid
            picGame.Invalidate()

            ' Reset highlighting and selection states
            highlightedX = -1
            highlightedY = -1
            isMouseInsideGrid = False
            selectionRectangle = Rectangle.Empty
        End If
    End Sub
    Friend Sub ResizeArrays(oldX As Integer, oldY As Integer, newX As Integer, newY As Integer)
        ' Resize verticalWalls and verticalWallItems
        verticalWalls = ResizeJaggedArray(verticalWalls, oldX + 1, oldY - 1, newX + 1, newY - 1, True)
        verticalWallItems = ResizeJaggedArray(verticalWallItems, oldX + 1, oldY - 1, newX + 1, newY - 1, False)

        ' Resize horizontalWalls and horizontalWallItems
        horizontalWalls = ResizeJaggedArray(horizontalWalls, oldX - 1, oldY + 1, newX - 1, newY + 1, True)
        horizontalWallItems = ResizeJaggedArray(horizontalWallItems, oldX - 1, oldY + 1, newX - 1, newY + 1, False)

        ' Resize placedItems
        placedItems = ResizeJaggedArray(placedItems, oldX - 1, oldY - 1, newX - 1, newY - 1, False)

        ' Resize floorsUseEdgeStyle
        floorsUseEdgeStyle = ResizeJaggedArray(floorsUseEdgeStyle, oldX - 1, oldY - 1, newX - 1, newY - 1, False)
        FillBorderWalls()
    End Sub

    Private Function ResizeJaggedArray(Of T)(sourceArray As T()(), oldSizeX As Integer, oldSizeY As Integer, newSizeX As Integer, newSizeY As Integer, initializeValue As Boolean) As T()()
        Dim newArray As T()() = New T(newSizeX)() {}
        For x As Integer = 0 To newSizeX
            newArray(x) = New T(newSizeY) {}
            For y As Integer = 0 To newSizeY
                If x < sourceArray.Length AndAlso y < sourceArray(x).Length Then
                    ' Copy existing data if within old array bounds
                    newArray(x)(y) = sourceArray(x)(y)
                Else
                    ' Initialize with default values
                    If GetType(T) Is GetType(Boolean) AndAlso initializeValue Then
                        newArray(x)(y) = CType(Convert.ChangeType(True, GetType(T)), T)
                    Else
                        newArray(x)(y) = Nothing
                    End If
                End If
            Next
        Next
        Return newArray
    End Function
    Public Sub PictureBox_MouseEnter(sender As Object, e As EventArgs)
        Dim pb As PictureBox = DirectCast(sender, PictureBox)
        pb.BackColor = Color.LightBlue  ' Change the background color on hover
        Debug.WriteLine("Mouse entered: " & pb.Name)  ' Debug message
    End Sub

    Public Sub PictureBox_MouseLeave(sender As Object, e As EventArgs)
        Dim pb As PictureBox = DirectCast(sender, PictureBox)
        pb.BackColor = Color.Transparent  ' Reset the background color
        Debug.WriteLine("Mouse left: " & pb.Name)  ' Debug message
    End Sub

    Private Sub InitializeAnimationForImages()
        animatedImages.Add(picBaddy1.Image)
        animatedImages.Add(picBaddy2.Image)
        animatedImages.Add(picBaddy3.Image)
        animatedImages.Add(picBaddy4.Image)
        animatedImages.Add(picLazer1.Image)
        animatedImages.Add(picLazer2.Image)
        animatedImages.Add(picFlipper1.Image)
        animatedImages.Add(picFlipper2.Image)
        animatedImages.Add(picFlipper3.Image)
        animatedImages.Add(picFlipper4.Image)
        animatedImages.Add(picCoin.Image)
        animatedImages.Add(picGate.Image)
        animatedImages.Add(picKey.Image)
        animatedImages.Add(picStart.Image)
        animatedImages.Add(picFinish.Image)
        animatedImages.Add(picBall.Image)
        animatedImages.Add(picBomb.Image)
        animatedImages.Add(picWarp.Image)
        animatedImages.Add(picWarp2.Image)

        For Each img In animatedImages
            If img IsNot Nothing Then
                ImageAnimator.Animate(img, AddressOf OnFrameChanged)
            End If
        Next
    End Sub

#End Region

#Region "Event Handlers"

    Private Sub frmDesigner_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Remove event handlers temporarily
        RemoveHandler cboX.SelectedIndexChanged, AddressOf cboX_SelectedIndexChanged
        RemoveHandler cboY.SelectedIndexChanged, AddressOf cboY_SelectedIndexChanged

        ' Set default selections
        If cboX.Items.Contains(SQCOUNTX.ToString()) Then
            cboX.SelectedItem = SQCOUNTX.ToString()
        End If
        If cboY.Items.Contains(SQCOUNTY.ToString()) Then
            cboY.SelectedItem = SQCOUNTY.ToString()
        End If

        ' Reattach event handlers
        AddHandler cboX.SelectedIndexChanged, AddressOf cboX_SelectedIndexChanged
        AddHandler cboY.SelectedIndexChanged, AddressOf cboY_SelectedIndexChanged

        ' Other initialization code...
        LoadMusic()
        LoadPremades()
        UpdateSelections()
    End Sub
    Private Sub LoadPremades()
        If Not Directory.Exists(premadesFolder) Then Directory.CreateDirectory(premadesFolder)
        lstPremades.Items.Clear()
        For Each file As String In Directory.GetFiles(premadesFolder, "*.Premade")
            lstPremades.Items.Add(Path.GetFileNameWithoutExtension(file))
        Next
    End Sub

    Private Sub LoadMusic()
        If Not Directory.Exists(musicFolder) Then Directory.CreateDirectory(musicFolder)
        cboMusic.Items.Clear()
        For Each file As String In Directory.GetFiles(musicFolder, "*.wav")
            cboMusic.Items.Add(Path.GetFileNameWithoutExtension(file))
        Next
    End Sub

    Private Sub OnAnimationTick(sender As Object, e As EventArgs)
        For Each img In animatedImages
            ImageAnimator.UpdateFrames(img)
        Next
        picGame.Invalidate()
    End Sub

    Private Sub OnFrameChanged(o As Object, e As EventArgs)
        ' Required but can be left empty
    End Sub

    Private Sub HighlightTimer_Tick(sender As Object, e As EventArgs)
        highlightColor = If(highlightColor = Color.Black, Color.White, Color.Black)
        picGame.Invalidate()
    End Sub
    Private Sub lstPremades_SelectedIndexChanged(sender As Object, e As EventArgs)
        If lstPremades.SelectedIndex >= 0 Then
            If Not isRestoringState Then SaveStateForUndo()
            Dim selectedPremade As String = lstPremades.SelectedItem.ToString()
            LoadSettingsFromFile(Path.Combine(premadesFolder, selectedPremade & ".Premade"))

            ' Update lstHatchstyles selection after loading settings
            UpdateSelections()
            picGame.Invalidate()
        End If
    End Sub

    Private Sub mnuClearWalls_Click(sender As Object, e As EventArgs) Handles mnuClearWalls.Click
        If Not selectionRectangle.IsEmpty Then
            If Not isRestoringState Then SaveStateForUndo()

            For Each pt As Point In GetVerticalWallsInRectangle(selectionRectangle)
                If Not IsVerticalWallOuterBorder(pt.X) AndAlso Not IsVerticalWallConnectedToEdge(pt) Then
                    verticalWalls(pt.X)(pt.Y) = False
                    verticalWallItems(pt.X)(pt.Y) = Nothing
                End If
            Next

            For Each pt As Point In GetHorizontalWallsInRectangle(selectionRectangle)
                If Not IsHorizontalWallOuterBorder(pt.Y) AndAlso Not IsHorizontalWallConnectedToEdge(pt) Then
                    horizontalWalls(pt.X)(pt.Y) = False
                    horizontalWallItems(pt.X)(pt.Y) = Nothing
                End If
            Next

            selectionRectangle = Rectangle.Empty
            isInSelectionMode = False
            Cursor = Cursors.Default
            picGame.Invalidate()
        End If
    End Sub

    Private Sub mnuFillWalls_Click(sender As Object, e As EventArgs) Handles mnuFillWalls.Click
        If Not selectionRectangle.IsEmpty Then
            If Not isRestoringState Then SaveStateForUndo()

            For Each pt As Point In GetVerticalWallsInRectangle(selectionRectangle)
                If pt.X > 0 AndAlso pt.X < SQCOUNTX Then
                    verticalWalls(pt.X)(pt.Y) = True
                End If
            Next

            For Each pt As Point In GetHorizontalWallsInRectangle(selectionRectangle)
                If pt.Y > 0 AndAlso pt.Y < SQCOUNTY Then
                    horizontalWalls(pt.X)(pt.Y) = True
                End If
            Next

            selectionRectangle = Rectangle.Empty
            isInSelectionMode = False
            Cursor = Cursors.Default
            picGame.Invalidate()
        End If
    End Sub

    Private Sub mnuClearObjects_Click(sender As Object, e As EventArgs) Handles mnuClearObjects.Click
        If Not selectionRectangle.IsEmpty Then
            If Not isRestoringState Then SaveStateForUndo()

            For Each pt As Point In GetCellsInRectangle(selectionRectangle)
                placedItems(pt.X)(pt.Y) = Nothing
            Next

            For Each pt As Point In GetVerticalWallsInRectangle(selectionRectangle)
                verticalWallItems(pt.X)(pt.Y) = Nothing
            Next

            For Each pt As Point In GetHorizontalWallsInRectangle(selectionRectangle)
                horizontalWallItems(pt.X)(pt.Y) = Nothing
            Next

            selectionRectangle = Rectangle.Empty
            isInSelectionMode = False
            Cursor = Cursors.Default
            picGame.Invalidate()
        End If
    End Sub
    Private Sub mnuClearEdge_Click(sender As Object, e As EventArgs) Handles mnuClearEdge.Click
        If Not selectionRectangle.IsEmpty Then
            If Not isRestoringState Then SaveStateForUndo()

            For Each pt As Point In GetCellsInRectangle(selectionRectangle)
                floorsUseEdgeStyle(pt.X)(pt.Y) = False
            Next

            selectionRectangle = Rectangle.Empty
            isInSelectionMode = False
            Cursor = Cursors.Default
            picGame.Invalidate()
        End If
    End Sub

    Private Sub mnuAddEdge_Click(sender As Object, e As EventArgs) Handles mnuAddEdge.Click
        If Not selectionRectangle.IsEmpty Then
            If Not isRestoringState Then SaveStateForUndo()

            ' Loop through each cell within the selection rectangle
            For Each pt As Point In GetCellsInRectangle(selectionRectangle)
                ' Remove any floor items in the cell
                placedItems(pt.X)(pt.Y) = Nothing

                ' Remove edge style if previously applied
                floorsUseEdgeStyle(pt.X)(pt.Y) = True

                ' Remove wall items around the cell and place walls

                ' Left vertical wall at (pt.X, pt.Y)
                If pt.X >= 0 AndAlso pt.X < verticalWalls.Length Then
                    ' Remove wall item on this wall
                    verticalWallItems(pt.X)(pt.Y) = Nothing
                    ' Place the wall
                    verticalWalls(pt.X)(pt.Y) = True
                End If

                ' Right vertical wall at (pt.X + 1, pt.Y)
                If pt.X + 1 >= 0 AndAlso pt.X + 1 < verticalWalls.Length Then
                    verticalWallItems(pt.X + 1)(pt.Y) = Nothing
                    verticalWalls(pt.X + 1)(pt.Y) = True
                End If

                ' Top horizontal wall at (pt.X, pt.Y)
                If pt.X >= 0 AndAlso pt.X < horizontalWalls.Length AndAlso pt.Y >= 0 AndAlso pt.Y < horizontalWalls(0).Length Then
                    horizontalWallItems(pt.X)(pt.Y) = Nothing
                    horizontalWalls(pt.X)(pt.Y) = True
                End If

                ' Bottom horizontal wall at (pt.X, pt.Y + 1)
                If pt.X >= 0 AndAlso pt.X < horizontalWalls.Length AndAlso pt.Y + 1 >= 0 AndAlso pt.Y + 1 < horizontalWalls(0).Length Then
                    horizontalWallItems(pt.X)(pt.Y + 1) = Nothing
                    horizontalWalls(pt.X)(pt.Y + 1) = True
                End If
            Next

            ' Clear the selection and update the UI
            selectionRectangle = Rectangle.Empty
            isInSelectionMode = False
            Cursor = Cursors.Default
            picGame.Invalidate()
        End If
    End Sub

    Private Sub mnuMove_Click(sender As Object, e As EventArgs) Handles mnuMove.Click
        isSelectionReadyToDrag = True
        Cursor = Cursors.Hand
        ' Capture the data within the selection
        CaptureSelectionData()
    End Sub

    Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
        If isInSelectionMode Then
            isInSelectionMode = False
            Cursor = Cursors.Default
            selectionRectangle = Rectangle.Empty
            picGame.Invalidate()
        Else
            isInSelectionMode = True
            Cursor = Cursors.Cross
        End If
    End Sub

    Private Sub mnuCancel_Click(sender As Object, e As EventArgs) Handles mnuCancel.Click
        selectionRectangle = Rectangle.Empty
        isInSelectionMode = False
        isSelectionReadyToDrag = False
        Cursor = Cursors.Default
        picGame.Invalidate()
    End Sub

    Private Sub mnuNew_Click(sender As Object, e As EventArgs) Handles mnuNew.Click
        If Not isRestoringState Then SaveStateForUndo()
        wallsHatchStyle = HatchStyle.Cross
        wallsColor1 = Color.Gray
        wallsColor2 = Color.DarkGray
        floorsHatchStyle = HatchStyle.SmallCheckerBoard
        floorsColor1 = Color.LightGray
        floorsColor2 = Color.White
        InitializeWalls()
        currentFilePath = ""

        ' Clear level name and music selection
        txtName.Text = ""
        SelectedMusic = ""
        cboMusic.SelectedIndex = -1

        ' Update selections
        UpdateSelections()

        picGame.Invalidate()
    End Sub

    Private Sub mnuSave_Click(sender As Object, e As EventArgs) Handles mnuSave.Click
        If String.IsNullOrEmpty(currentFilePath) Then
            mnuSaveAs_Click(sender, e)
        Else
            SaveRoomToFile(currentFilePath)
        End If
    End Sub

    Private Sub mnuLoad_Click(sender As Object, e As EventArgs) Handles mnuLoad.Click
        Dim openDialog As New OpenFileDialog() With {
        .InitialDirectory = savesFolder,
        .Filter = "Room Files (*.room)|*.room",
        .DefaultExt = "room",
        .AddExtension = True,
        .Title = "Load Room"
    }
        If openDialog.ShowDialog() = DialogResult.OK Then
            ' Before loading, save the current state
            If Not isRestoringState Then SaveStateForUndo()
            LoadRoomFromFile(openDialog.FileName)
            currentFilePath = openDialog.FileName

            ' Update the UI elements related to grid size
            UpdateGridSizeUI()

            ' Update selections
            UpdateSelections()
            picGame.Invalidate()
        End If
    End Sub
    Private Sub mnuSaveAs_Click(sender As Object, e As EventArgs)
        Dim saveDialog As New SaveFileDialog() With {
            .InitialDirectory = savesFolder,
            .Filter = "Room Files (*.room)|*.room",
            .DefaultExt = "room",
            .AddExtension = True,
            .Title = "Save Room As"
        }
        If saveDialog.ShowDialog() = DialogResult.OK Then
            SaveRoomToFile(saveDialog.FileName)
            currentFilePath = saveDialog.FileName
        End If
    End Sub
    Private Sub SaveState(isNewAction As Boolean)
        If isRestoringState Then Return
        If isNewAction Then
            ' Clear redo stack when a new action is performed
            redoStack.Clear()
        End If
        ' Save current state to undo stack
        undoStack.Push(New RoomState(Me))
        If undoStack.Count > 50 Then
            undoStack = New Stack(Of RoomState)(undoStack.Reverse().Take(50))
        End If
    End Sub
    Private Sub SaveStateForUndo()
        If isRestoringState Then Return
        ' Do not clear redoStack here
        ' Save current state to undo stack
        undoStack.Push(New RoomState(Me))
        If undoStack.Count > 50 Then
            undoStack = New Stack(Of RoomState)(undoStack.Reverse().Take(50))
        End If
    End Sub
    Private Sub SaveStateForRedo()
        If isRestoringState Then Return
        ' Save current state to redo stack
        redoStack.Push(New RoomState(Me))
        If redoStack.Count > 50 Then
            redoStack = New Stack(Of RoomState)(redoStack.Reverse().Take(50))
        End If
    End Sub

    Private Sub btnUndo_Click(sender As Object, e As EventArgs) Handles btnUndo.Click
        If undoStack.Count > 1 Then
            ' Save current state to redo stack before undo
            SaveStateForRedo()
            ' Pop the last state from the undo stack
            undoStack.Pop()
            ' Restore the previous state
            isRestoringState = True
            Dim previousState As RoomState = undoStack.Peek()
            previousState.RestoreState(Me)
            isRestoringState = False

            ' Update grid size-related UI
            UpdateGridSizeUI()

            ' Update selections
            UpdateSelections()

            picGame.Invalidate()
        End If
    End Sub
    Private Sub btnRedo_Click(sender As Object, e As EventArgs) Handles btnRedo.Click
        If redoStack.Count > 0 Then
            ' Save current state to undo stack before redo
            SaveStateForUndo()
            ' Restore the state from redo stack
            isRestoringState = True
            Dim redoState As RoomState = redoStack.Pop()
            redoState.RestoreState(Me)
            isRestoringState = False

            ' Update grid size-related UI
            UpdateGridSizeUI()

            ' Update selections
            UpdateSelections()

            picGame.Invalidate()
        End If
    End Sub
    Private Sub btnSavePremade_Click(sender As Object, e As EventArgs)
        Dim saveDialog As New SaveFileDialog() With {
            .InitialDirectory = premadesFolder,
            .Filter = "Premade Files (*.Premade)|*.Premade",
            .DefaultExt = "Premade",
            .AddExtension = True,
            .Title = "Save Premade"
        }
        If saveDialog.ShowDialog() = DialogResult.OK Then
            SaveSettingsToFile(saveDialog.FileName)
            LoadPremades()
        End If
    End Sub

    Private Sub lstHatchstyles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstHatchstyles.SelectedIndexChanged
        If lstHatchstyles.SelectedItem IsNot Nothing Then
            Dim selectedHatchStyleName As String = lstHatchstyles.SelectedItem.ToString()
            Dim selectedHatchStyle As HatchStyle = CType([Enum].Parse(GetType(HatchStyle), selectedHatchStyleName), HatchStyle)
            If rdoFloors.Checked Then
                floorsHatchStyle = selectedHatchStyle
            ElseIf rdoWalls.Checked Then
                wallsHatchStyle = selectedHatchStyle
            End If
            If Not isRestoringState Then SaveStateForUndo()
            picGame.Invalidate()
        End If
    End Sub

    Private Sub cboMusic_SelectedIndexChanged(sender As Object, e As EventArgs)
        SelectedMusic = If(cboMusic.SelectedItem?.ToString(), "")
        If Not isRestoringState Then SaveStateForUndo()
    End Sub

    Private Sub btnAll_Click(sender As Object, e As EventArgs)
        If Not isRestoringState Then SaveStateForUndo()
        RandomizeWalls()
        RandomizeFloors()

        ' Update the HatchStyle list selection
        UpdateSelections()
        picGame.Invalidate()
    End Sub

    Private Sub btnWalls_Click(sender As Object, e As EventArgs)
        If Not isRestoringState Then SaveStateForUndo()
        RandomizeWalls()
        picGame.Invalidate()
    End Sub

    Private Sub btnFloors_Click(sender As Object, e As EventArgs)
        If Not isRestoringState Then SaveStateForUndo()
        RandomizeFloors()
        picGame.Invalidate()
    End Sub

    Private Sub rdoWalls_CheckedChanged(sender As Object, e As EventArgs) Handles rdoWalls.CheckedChanged
        If rdoWalls.Checked Then
            UpdateSelections()
        End If
    End Sub

    Private Sub rdoFloors_CheckedChanged(sender As Object, e As EventArgs) Handles rdoFloors.CheckedChanged
        If rdoFloors.Checked Then
            UpdateSelections()
        End If
    End Sub

    Private Sub rdoEdge_CheckedChanged(sender As Object, e As EventArgs) Handles rdoEdge.CheckedChanged
        If rdoEdge.Checked Then
            UpdateSelections()
        End If
    End Sub

    Private Sub PicGame_Paint(sender As Object, e As PaintEventArgs)
        CalculateSquareSizes(picGame.ClientSize.Width, picGame.ClientSize.Height)
        e.Graphics.SmoothingMode = SmoothingMode.None
        DrawGrid(e.Graphics)
        DrawHighlight(e.Graphics)

        If isDraggingItem AndAlso draggedItem IsNot Nothing Then
            Dim img As Image = GetImageByKey(draggedItem.ImageKey)
            If img IsNot Nothing Then
                ImageAnimator.UpdateFrames(img)
                Dim drawRect As Rectangle

                If draggedItemIsWall Then
                    If draggedItemIsVerticalWall Then
                        ' Draw vertical wall item under cursor
                        Dim wallWidth As Integer = WALL_THICKNESS
                        Dim wallHeight As Integer = SQSizeY
                        Dim xPos As Integer = mouseX - WALL_THICKNESS \ 2
                        Dim yPos As Integer = mouseY - SQSizeY \ 2

                        ' Keep original image width, stretch height
                        Dim newWidth As Integer = img.Width
                        Dim newHeight As Integer = wallHeight

                        ' Center horizontally
                        Dim centeredX As Integer = xPos + (wallWidth - newWidth) \ 2
                        drawRect = New Rectangle(centeredX, yPos, newWidth, newHeight)
                    Else
                        ' Draw horizontal wall item under cursor
                        Dim wallWidth As Integer = SQSizeX
                        Dim wallHeight As Integer = WALL_THICKNESS
                        Dim xPos As Integer = mouseX - SQSizeX \ 2
                        Dim yPos As Integer = mouseY - WALL_THICKNESS \ 2

                        ' Stretch width, keep original image height
                        Dim newWidth As Integer = wallWidth
                        Dim newHeight As Integer = img.Height

                        ' Center vertically
                        Dim centeredY As Integer = yPos + (wallHeight - newHeight) \ 2
                        drawRect = New Rectangle(xPos, centeredY, newWidth, newHeight)
                    End If
                Else
                    ' Draw floor item under cursor
                    Dim xPos As Integer = mouseX - SQSizeX \ 2
                    Dim yPos As Integer = mouseY - SQSizeY \ 2
                    drawRect = New Rectangle(xPos, yPos, SQSizeX, SQSizeY)
                End If
                e.Graphics.DrawImage(img, drawRect)
            End If
        End If

        If (isInSelectionMode OrElse isSelectionReadyToDrag OrElse isDraggingSelection) AndAlso Not selectionRectangle.IsEmpty Then
            Using selectionBrush As New SolidBrush(Color.FromArgb(64, Color.Blue))
                e.Graphics.FillRectangle(selectionBrush, selectionRectangle)
            End Using
            Using selectionPen As New Pen(Color.Blue, 2) With {.DashStyle = DashStyle.Dash}
                e.Graphics.DrawRectangle(selectionPen, selectionRectangle)
            End Using
        End If

        If isDraggingSelection Then
            ' Draw the selectionData at the selection rectangle position with transparency
            Dim drawCellX As Integer = (selectionRectangle.X - INDENT) \ (SQSizeX + WALL_THICKNESS)
            Dim drawCellY As Integer = (selectionRectangle.Y - INDENT) \ (SQSizeY + WALL_THICKNESS)
            DrawSelectionData(e.Graphics, drawCellX, drawCellY)
        End If
    End Sub

    Private Sub Form_MouseMove(sender As Object, e As MouseEventArgs)
        If isDraggingItem Then
            Dim screenPos As Point = Me.PointToScreen(New Point(e.X, e.Y))
            Dim picGameClientPoint As Point = picGame.PointToClient(screenPos)

            mouseX = picGameClientPoint.X
            mouseY = picGameClientPoint.Y

            If picGame.ClientRectangle.Contains(picGameClientPoint) Then
                UpdateHighlight(mouseX, mouseY)
                isMouseInsideGrid = True
            Else
                isMouseInsideGrid = False
            End If

            picGame.Invalidate()
        End If
    End Sub
    Private Function IsVerticalWallOuterBorder(x As Integer) As Boolean
        ' Vertical walls on the outer borders are at x = 0 and x = SQCOUNTX
        Return x = 0 OrElse x = SQCOUNTX
    End Function
    Private Function IsHorizontalWallOuterBorder(y As Integer) As Boolean
        ' Horizontal walls on the outer borders are at y = 0 and y = SQCOUNTY
        Return y = 0 OrElse y = SQCOUNTY
    End Function
    Private Sub Form_MouseUp(sender As Object, e As MouseEventArgs)
        If isDraggingItem AndAlso e.Button = MouseButtons.Left Then
            isDraggingItem = False
            Me.Capture = False
            Cursor.Current = Cursors.Default

            Dim screenPos As Point = Me.PointToScreen(New Point(e.X, e.Y))
            Dim picGameClientPoint As Point = picGame.PointToClient(screenPos)

            If picGame.ClientRectangle.Contains(picGameClientPoint) Then
                mouseX = picGameClientPoint.X
                mouseY = picGameClientPoint.Y
                UpdateHighlight(mouseX, mouseY)

                ' Place the item if the drop location is valid
                If isMouseInsideGrid Then
                    If highlightedWallX AndAlso draggedItemIsWall AndAlso draggedItemIsVerticalWall Then
                        ' Ensure indices are within bounds
                        If highlightedX >= 0 AndAlso highlightedX < verticalWallItems.Length AndAlso
                       verticalWallItems(highlightedX) IsNot Nothing AndAlso
                       highlightedY >= 0 AndAlso highlightedY < verticalWallItems(highlightedX).Length Then

                            If Not IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
                                ' Place vertical wall item
                                verticalWallItems(highlightedX)(highlightedY) = draggedItem
                                verticalWalls(highlightedX)(highlightedY) = False
                            Else
                                ' Invalid drop location, return item to original location
                                ReturnItemToOriginalLocation()
                            End If
                        End If
                    ElseIf highlightedWallY AndAlso draggedItemIsWall AndAlso Not draggedItemIsVerticalWall Then
                        ' Ensure indices are within bounds
                        If highlightedX >= 0 AndAlso highlightedX < horizontalWallItems.Length AndAlso
                       horizontalWallItems(highlightedX) IsNot Nothing AndAlso
                       highlightedY >= 0 AndAlso highlightedY < horizontalWallItems(highlightedX).Length Then

                            If Not IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
                                ' Place horizontal wall item
                                horizontalWallItems(highlightedX)(highlightedY) = draggedItem
                                horizontalWalls(highlightedX)(highlightedY) = False
                            Else
                                ' Invalid drop location, return item to original location
                                ReturnItemToOriginalLocation()
                            End If
                        End If
                    ElseIf Not highlightedWallX AndAlso Not highlightedWallY AndAlso Not draggedItemIsWall Then
                        ' Ensure indices are within bounds
                        If highlightedX >= 0 AndAlso highlightedX < placedItems.Length AndAlso
                       placedItems(highlightedX) IsNot Nothing AndAlso
                       highlightedY >= 0 AndAlso highlightedY < placedItems(highlightedX).Length Then

                            ' Place floor item
                            placedItems(highlightedX)(highlightedY) = draggedItem
                        End If
                    Else
                        ' Invalid drop location, return item to original location
                        ReturnItemToOriginalLocation()
                    End If
                Else
                    ' Not inside grid, return item to original location
                    ReturnItemToOriginalLocation()
                End If
            Else
                ' Not over picGame, return item to original location
                ReturnItemToOriginalLocation()
            End If

            draggedItem = Nothing
            If isActionInProgress Then
                isActionInProgress = False
                redoStack.Clear()
            End If

            picGame.Invalidate()
        End If
    End Sub
    Private Sub PicGame_MouseDown(sender As Object, e As MouseEventArgs) Handles picGame.MouseDown
        If isSelectionReadyToDrag Then
            If selectionRectangle.Contains(e.Location) Then
                If e.Button = MouseButtons.Left Then
                    ' Start dragging the selection immediately
                    isDraggingSelection = True
                    selectionOffset = New Point(e.X - selectionRectangle.X, e.Y - selectionRectangle.Y)
                    selectionStartCell = New Point(
                    (selectionRectangle.X - INDENT) \ (SQSizeX + WALL_THICKNESS),
                    (selectionRectangle.Y - INDENT) \ (SQSizeY + WALL_THICKNESS))
                    picGame.Capture = True
                    Cursor = Cursors.SizeAll
                ElseIf e.Button = MouseButtons.Right Then
                    ' Cancel the move operation
                    isSelectionReadyToDrag = False
                    selectionRectangle = Rectangle.Empty
                    Cursor = Cursors.Default
                    picGame.Invalidate()
                End If
            Else
                ' User clicked outside the selection, cancel the selection
                isSelectionReadyToDrag = False
                selectionRectangle = Rectangle.Empty
                Cursor = Cursors.Default
                picGame.Invalidate()
            End If
        ElseIf isInSelectionMode Then
            If e.Button = MouseButtons.Left Then
                isSelecting = True
                selectionStartPoint = e.Location
                selectionEndPoint = e.Location
                selectionRectangle = GetRectangle(selectionStartPoint, selectionEndPoint)
                picGame.Invalidate()
            End If
        Else
            If e.Button = MouseButtons.Right Then
                isRightMouseDown = True
                rightMouseDownPosition = e.Location
                rightMouseDownTime = DateTime.Now
                If Not isActionInProgress Then
                    isActionInProgress = True
                    If Not isRestoringState Then SaveStateForUndo()
                End If
            ElseIf e.Button = MouseButtons.Left Then
                UpdateHighlight(e.X, e.Y)
                Dim itemPickedUp As Boolean = False
                If isMouseInsideGrid Then
                    If Not isActionInProgress Then
                        isActionInProgress = True
                        If Not isRestoringState Then SaveStateForUndo()
                    End If

                    ' Try to pick up an item first
                    If highlightedWallX Then
                        If highlightedX >= 0 AndAlso highlightedX < verticalWallItems.Length AndAlso
                       verticalWallItems(highlightedX) IsNot Nothing AndAlso
                       highlightedY >= 0 AndAlso highlightedY < verticalWallItems(highlightedX).Length Then

                            If verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                                ' Pick up vertical wall item
                                isDraggingItem = True
                                draggedItem = verticalWallItems(highlightedX)(highlightedY)
                                draggedItemIsWall = True
                                draggedItemIsVerticalWall = True
                                draggedItemOriginalX = highlightedX
                                draggedItemOriginalY = highlightedY
                                verticalWallItems(highlightedX)(highlightedY) = Nothing
                                verticalWalls(highlightedX)(highlightedY) = True
                                picGame.Capture = True
                                itemPickedUp = True
                            End If
                        End If
                    ElseIf highlightedWallY Then
                        If highlightedX >= 0 AndAlso highlightedX < horizontalWallItems.Length AndAlso
                       horizontalWallItems(highlightedX) IsNot Nothing AndAlso
                       highlightedY >= 0 AndAlso highlightedY < horizontalWallItems(highlightedX).Length Then

                            If horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                                ' Pick up horizontal wall item
                                isDraggingItem = True
                                draggedItem = horizontalWallItems(highlightedX)(highlightedY)
                                draggedItemIsWall = True
                                draggedItemIsVerticalWall = False
                                draggedItemOriginalX = highlightedX
                                draggedItemOriginalY = highlightedY
                                horizontalWallItems(highlightedX)(highlightedY) = Nothing
                                horizontalWalls(highlightedX)(highlightedY) = True
                                picGame.Capture = True
                                itemPickedUp = True
                            End If
                        End If
                    Else
                        If highlightedX >= 0 AndAlso highlightedX < placedItems.Length AndAlso
                       placedItems(highlightedX) IsNot Nothing AndAlso
                       highlightedY >= 0 AndAlso highlightedY < placedItems(highlightedX).Length Then

                            If placedItems(highlightedX)(highlightedY) IsNot Nothing Then
                                ' Pick up floor item
                                isDraggingItem = True
                                draggedItem = placedItems(highlightedX)(highlightedY)
                                draggedItemIsWall = False
                                draggedItemIsVerticalWall = False
                                draggedItemOriginalX = highlightedX
                                draggedItemOriginalY = highlightedY
                                placedItems(highlightedX)(highlightedY) = Nothing
                                picGame.Capture = True
                                itemPickedUp = True
                            End If
                        End If
                    End If

                    If itemPickedUp Then
                        redoStack.Clear()
                        picGame.Invalidate()
                    Else
                        ' If no item was picked up, proceed to set edge style or add walls
                        If rdoEdge.Checked Then
                            isLeftMouseDown = True
                            SetEdgeStyleAtMousePosition(e.X, e.Y)
                        Else
                            ' Add wall at mouse position
                            isLeftMouseDown = True
                            AddWallAtMousePosition(e.X, e.Y)
                        End If
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub PicGame_MouseMove(sender As Object, e As MouseEventArgs) Handles picGame.MouseMove
        If isDraggingSelection Then
            ' Update the selection rectangle position as the mouse moves
            Dim newX As Integer = e.X - selectionOffset.X
            Dim newY As Integer = e.Y - selectionOffset.Y

            ' Calculate cell positions
            Dim cellX As Integer = (newX - INDENT + (SQSizeX + WALL_THICKNESS) \ 2) \ (SQSizeX + WALL_THICKNESS)
            Dim cellY As Integer = (newY - INDENT + (SQSizeY + WALL_THICKNESS) \ 2) \ (SQSizeY + WALL_THICKNESS)

            ' Constrain to grid boundaries
            cellX = Math.Max(0, Math.Min(SQCOUNTX - selectionData.selectionWidth, cellX))
            cellY = Math.Max(0, Math.Min(SQCOUNTY - selectionData.selectionHeight, cellY))

            ' Update selection rectangle position
            selectionRectangle.X = INDENT + cellX * (SQSizeX + WALL_THICKNESS)
            selectionRectangle.Y = INDENT + cellY * (SQSizeY + WALL_THICKNESS)
            selectionRectangle.Width = selectionData.selectionWidth * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS
            selectionRectangle.Height = selectionData.selectionHeight * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS

            picGame.Invalidate()
        ElseIf isInSelectionMode Then
            If isSelecting Then
                selectionEndPoint = e.Location
                selectionRectangle = GetRectangle(selectionStartPoint, selectionEndPoint)
                picGame.Invalidate()
            End If
        Else
            If isDraggingItem Then
                mouseX = e.X
                mouseY = e.Y
                UpdateHighlight(e.X, e.Y)
                picGame.Invalidate()
            Else
                mouseX = e.X
                mouseY = e.Y
                UpdateHighlight(e.X, e.Y)
                picGame.Invalidate()
                If isRightMouseDown Then
                    If rdoEdge.Checked Then
                        UpdateHighlight(e.X, e.Y)
                        If isMouseInsideGrid Then
                            RemoveEdgeStyleAtMousePosition(e.X, e.Y)
                        End If
                    Else
                        Dim deltaX As Integer = e.X - rightMouseDownPosition.X
                        Dim deltaY As Integer = e.Y - rightMouseDownPosition.Y
                        Dim distanceSquared As Integer = deltaX * deltaX + deltaY * deltaY
                        If distanceSquared > CLICK_DRAG_THRESHOLD_SQUARED Then
                            RemoveObjectAtMousePosition(e.X, e.Y, removeWalls:=True, removeItems:=False)
                        End If
                    End If
                ElseIf isLeftMouseDown Then
                    If rdoEdge.Checked Then
                        UpdateHighlight(e.X, e.Y)
                        If isMouseInsideGrid Then
                            SetEdgeStyleAtMousePosition(e.X, e.Y)
                        End If
                    Else
                        AddWallAtMousePosition(e.X, e.Y)
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub RemoveEdgeStyleAtMousePosition(mouseX As Integer, mouseY As Integer)
        UpdateHighlight(mouseX, mouseY)
        If Not highlightedWallX AndAlso Not highlightedWallY Then
            If highlightedX >= 0 AndAlso highlightedX < SQCOUNTX AndAlso highlightedY >= 0 AndAlso highlightedY < SQCOUNTY Then
                floorsUseEdgeStyle(highlightedX)(highlightedY) = False
                picGame.Invalidate()
            End If
        End If
    End Sub
    Private Sub PicGame_MouseUp(sender As Object, e As MouseEventArgs) Handles picGame.MouseUp
        If isDraggingSelection Then
            If e.Button = MouseButtons.Left Then
                ' Finish dragging
                isDraggingSelection = False
                picGame.Capture = False
                Cursor = Cursors.Default

                ' Get new cell positions
                Dim newCellX As Integer = (selectionRectangle.X - INDENT + WALL_THICKNESS \ 2) \ (SQSizeX + WALL_THICKNESS)
                Dim newCellY As Integer = (selectionRectangle.Y - INDENT + WALL_THICKNESS \ 2) \ (SQSizeY + WALL_THICKNESS)

                ' Ensure indices are within bounds
                newCellX = Math.Max(0, Math.Min(SQCOUNTX - selectionData.selectionWidth, newCellX))
                newCellY = Math.Max(0, Math.Min(SQCOUNTY - selectionData.selectionHeight, newCellY))

                ' First, clear data from original location
                ClearSelectionDataAt(selectionStartCell.X, selectionStartCell.Y)

                ' Then, move data to new location
                MoveSelectionDataTo(newCellX, newCellY)

                ' Reset selection-related variables
                selectionRectangle = Rectangle.Empty
                isSelectionReadyToDrag = False
                isInSelectionMode = False ' Exit selection mode after moving
                Cursor = Cursors.Default
                picGame.Invalidate()

                If Not isRestoringState Then SaveStateForUndo()
            ElseIf e.Button = MouseButtons.Right Then
                ' Cancel the move operation
                isDraggingSelection = False
                picGame.Capture = False
                Cursor = Cursors.Default

                ' Reset selection rectangle to original position
                selectionRectangle = New Rectangle(
                INDENT + selectionStartCell.X * (SQSizeX + WALL_THICKNESS),
                INDENT + selectionStartCell.Y * (SQSizeY + WALL_THICKNESS),
                selectionData.selectionWidth * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS,
                selectionData.selectionHeight * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS)

                ' Reset selection-related variables
                isSelectionReadyToDrag = False
                isInSelectionMode = False ' Exit selection mode after canceling
                Cursor = Cursors.Default
                picGame.Invalidate()
            End If
        ElseIf isInSelectionMode Then
            If isSelecting AndAlso e.Button = MouseButtons.Left Then
                isSelecting = False
                selectionEndPoint = e.Location
                selectionRectangle = GetRectangle(selectionStartPoint, selectionEndPoint)
                picGame.Invalidate()

                If Not selectionRectangle.IsEmpty Then
                    ' Show context menu at mouse position
                    Dim menuPosition As Point = e.Location
                    ctmMenu.Show(picGame, menuPosition)
                Else
                    ' If selection is empty, exit selection mode
                    isInSelectionMode = False
                    Cursor = Cursors.Default
                End If
            End If
        Else
            If e.Button = MouseButtons.Left Then
                If isDraggingItem Then
                    isDraggingItem = False
                    picGame.Capture = False

                    If isMouseInsideGrid Then
                        If highlightedWallX AndAlso draggedItemIsWall AndAlso draggedItemIsVerticalWall Then
                            If highlightedX >= 0 AndAlso highlightedX < verticalWallItems.Length AndAlso
                           verticalWallItems(highlightedX) IsNot Nothing AndAlso
                           highlightedY >= 0 AndAlso highlightedY < verticalWallItems(highlightedX).Length Then

                                ' Place vertical wall item
                                verticalWallItems(highlightedX)(highlightedY) = draggedItem
                                verticalWalls(highlightedX)(highlightedY) = False
                            Else
                                ' Invalid drop location, return item to original location
                                ReturnItemToOriginalLocation()
                            End If
                        ElseIf highlightedWallY AndAlso draggedItemIsWall AndAlso Not draggedItemIsVerticalWall Then
                            If highlightedX >= 0 AndAlso highlightedX < horizontalWallItems.Length AndAlso
                           horizontalWallItems(highlightedX) IsNot Nothing AndAlso
                           highlightedY >= 0 AndAlso highlightedY < horizontalWallItems(highlightedX).Length Then

                                ' Place horizontal wall item
                                horizontalWallItems(highlightedX)(highlightedY) = draggedItem
                                horizontalWalls(highlightedX)(highlightedY) = False
                            Else
                                ' Invalid drop location, return item to original location
                                ReturnItemToOriginalLocation()
                            End If
                        ElseIf Not highlightedWallX AndAlso Not highlightedWallY AndAlso Not draggedItemIsWall Then
                            If highlightedX >= 0 AndAlso highlightedX < placedItems.Length AndAlso
                           placedItems(highlightedX) IsNot Nothing AndAlso
                           highlightedY >= 0 AndAlso highlightedY < placedItems(highlightedX).Length Then

                                ' Place floor item
                                placedItems(highlightedX)(highlightedY) = draggedItem
                            Else
                                ' Invalid drop location, return item to original location
                                ReturnItemToOriginalLocation()
                            End If
                        Else
                            ' Invalid drop location, return item to original location
                            ReturnItemToOriginalLocation()
                        End If
                    Else
                        ' Not inside grid, return item to original location
                        ReturnItemToOriginalLocation()
                    End If

                    draggedItem = Nothing
                    If isActionInProgress Then
                        isActionInProgress = False
                        redoStack.Clear()
                    End If

                    picGame.Invalidate()
                Else
                    isLeftMouseDown = False
                    If isActionInProgress Then
                        isActionInProgress = False
                        redoStack.Clear()
                    End If
                End If
            ElseIf e.Button = MouseButtons.Right Then
                isRightMouseDown = False
                If isActionInProgress Then
                    isActionInProgress = False
                    redoStack.Clear()
                End If

                If rdoEdge.Checked Then
                    ' Remove edge style at the mouse position
                    UpdateHighlight(e.X, e.Y)
                    If isMouseInsideGrid Then
                        If Not highlightedWallX AndAlso Not highlightedWallY Then
                            If highlightedX >= 0 AndAlso highlightedX < SQCOUNTX AndAlso
                           highlightedY >= 0 AndAlso highlightedY < SQCOUNTY Then

                                floorsUseEdgeStyle(highlightedX)(highlightedY) = False
                                picGame.Invalidate()
                            End If
                        End If
                    End If
                Else
                    ' Existing logic for right-click when not in edge mode
                    Dim deltaX As Integer = e.X - rightMouseDownPosition.X
                    Dim deltaY As Integer = e.Y - rightMouseDownPosition.Y
                    Dim distanceSquared As Integer = deltaX * deltaX + deltaY * deltaY
                    If distanceSquared < CLICK_DRAG_THRESHOLD_SQUARED Then
                        RemoveObjectAtMousePosition(e.X, e.Y, removeWalls:=False, removeItems:=True)
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub CaptureSelectionData()
        ' Calculate start and end cell indices based on the selection rectangle
        Dim startCellX As Integer = (selectionRectangle.X - INDENT + WALL_THICKNESS \ 2) \ (SQSizeX + WALL_THICKNESS)
        Dim startCellY As Integer = (selectionRectangle.Y - INDENT + WALL_THICKNESS \ 2) \ (SQSizeY + WALL_THICKNESS)
        Dim endCellX As Integer = ((selectionRectangle.Right - INDENT - WALL_THICKNESS \ 2 - 1)) \ (SQSizeX + WALL_THICKNESS)
        Dim endCellY As Integer = ((selectionRectangle.Bottom - INDENT - WALL_THICKNESS \ 2 - 1)) \ (SQSizeY + WALL_THICKNESS)

        ' Ensure the indices are within the grid boundaries
        startCellX = Math.Max(0, Math.Min(SQCOUNTX - 1, startCellX))
        startCellY = Math.Max(0, Math.Min(SQCOUNTY - 1, startCellY))
        endCellX = Math.Max(0, Math.Min(SQCOUNTX - 1, endCellX))
        endCellY = Math.Max(0, Math.Min(SQCOUNTY - 1, endCellY))

        Dim selectionWidth As Integer = endCellX - startCellX + 1
        Dim selectionHeight As Integer = endCellY - startCellY + 1

        selectionData = New SelectionData()
        selectionData.selectionWidth = selectionWidth
        selectionData.selectionHeight = selectionHeight

        ' Prepare arrays with the appropriate sizes
        ' Placed Items and Floors Edge Style
        selectionData.placedItems = New PlacedItem(selectionWidth - 1)() {}
        selectionData.floorsUseEdgeStyle = New Boolean(selectionWidth - 1)() {}
        For x As Integer = 0 To selectionWidth - 1
            selectionData.placedItems(x) = New PlacedItem(selectionHeight - 1) {}
            selectionData.floorsUseEdgeStyle(x) = New Boolean(selectionHeight - 1) {}
        Next

        ' Vertical Walls and Items
        Dim verticalWallsWidth As Integer = selectionWidth + 1
        Dim verticalWallsHeight As Integer = selectionHeight
        selectionData.verticalWalls = New Boolean(verticalWallsWidth - 1)() {}
        selectionData.verticalWallItems = New PlacedItem(verticalWallsWidth - 1)() {}
        For x As Integer = 0 To verticalWallsWidth - 1
            selectionData.verticalWalls(x) = New Boolean(verticalWallsHeight - 1) {}
            selectionData.verticalWallItems(x) = New PlacedItem(verticalWallsHeight - 1) {}
        Next

        ' Horizontal Walls and Items
        Dim horizontalWallsWidth As Integer = selectionWidth
        Dim horizontalWallsHeight As Integer = selectionHeight + 1
        selectionData.horizontalWalls = New Boolean(horizontalWallsWidth - 1)() {}
        selectionData.horizontalWallItems = New PlacedItem(horizontalWallsWidth - 1)() {}
        For x As Integer = 0 To horizontalWallsWidth - 1
            selectionData.horizontalWalls(x) = New Boolean(horizontalWallsHeight - 1) {}
            selectionData.horizontalWallItems(x) = New PlacedItem(horizontalWallsHeight - 1) {}
        Next

        ' Copy data from the main arrays into selectionData
        ' Placed Items and Floors Edge Style
        For x As Integer = 0 To selectionWidth - 1
            For y As Integer = 0 To selectionHeight - 1
                selectionData.placedItems(x)(y) = placedItems(startCellX + x)(startCellY + y)
                selectionData.floorsUseEdgeStyle(x)(y) = floorsUseEdgeStyle(startCellX + x)(startCellY + y)
            Next
        Next

        ' Vertical Walls and Items
        For x As Integer = 0 To verticalWallsWidth - 1
            For y As Integer = 0 To verticalWallsHeight - 1
                selectionData.verticalWalls(x)(y) = verticalWalls(startCellX + x)(startCellY + y)
                selectionData.verticalWallItems(x)(y) = verticalWallItems(startCellX + x)(startCellY + y)
            Next
        Next

        ' Horizontal Walls and Items
        For x As Integer = 0 To horizontalWallsWidth - 1
            For y As Integer = 0 To horizontalWallsHeight - 1
                selectionData.horizontalWalls(x)(y) = horizontalWalls(startCellX + x)(startCellY + y)
                selectionData.horizontalWallItems(x)(y) = horizontalWallItems(startCellX + x)(startCellY + y)
            Next
        Next

        ' Store the starting cell of the selection
        selectionStartCell = New Point(startCellX, startCellY)
    End Sub

    Private Sub MoveSelectionDataTo(newCellX As Integer, newCellY As Integer)
        ' Placed Items and Floors Edge Style
        For x As Integer = 0 To selectionData.selectionWidth - 1
            For y As Integer = 0 To selectionData.selectionHeight - 1
                If newCellX + x >= 0 AndAlso newCellX + x < SQCOUNTX AndAlso newCellY + y >= 0 AndAlso newCellY + y < SQCOUNTY Then
                    placedItems(newCellX + x)(newCellY + y) = selectionData.placedItems(x)(y)
                    floorsUseEdgeStyle(newCellX + x)(newCellY + y) = selectionData.floorsUseEdgeStyle(x)(y)
                End If
            Next
        Next

        ' Vertical Walls and Items
        For x As Integer = 0 To selectionData.verticalWalls.Length - 1
            For y As Integer = 0 To selectionData.verticalWalls(0).Length - 1
                If newCellX + x >= 0 AndAlso newCellX + x < verticalWalls.Length AndAlso newCellY + y >= 0 AndAlso newCellY + y < verticalWalls(0).Length Then
                    verticalWalls(newCellX + x)(newCellY + y) = selectionData.verticalWalls(x)(y)
                    verticalWallItems(newCellX + x)(newCellY + y) = selectionData.verticalWallItems(x)(y)
                End If
            Next
        Next

        ' Horizontal Walls and Items
        For x As Integer = 0 To selectionData.horizontalWalls.Length - 1
            For y As Integer = 0 To selectionData.horizontalWalls(0).Length - 1
                If newCellX + x >= 0 AndAlso newCellX + x < horizontalWalls.Length AndAlso newCellY + y >= 0 AndAlso newCellY + y < horizontalWalls(0).Length Then
                    horizontalWalls(newCellX + x)(newCellY + y) = selectionData.horizontalWalls(x)(y)
                    horizontalWallItems(newCellX + x)(newCellY + y) = selectionData.horizontalWallItems(x)(y)
                End If
            Next
        Next
    End Sub

    Private Sub ClearSelectionDataAt(oldCellX As Integer, oldCellY As Integer)
        ' Placed Items and Floors Edge Style
        For x As Integer = 0 To selectionData.selectionWidth - 1
            For y As Integer = 0 To selectionData.selectionHeight - 1
                If oldCellX + x >= 0 AndAlso oldCellX + x < SQCOUNTX AndAlso oldCellY + y >= 0 AndAlso oldCellY + y < SQCOUNTY Then
                    placedItems(oldCellX + x)(oldCellY + y) = Nothing
                    floorsUseEdgeStyle(oldCellX + x)(oldCellY + y) = False
                End If
            Next
        Next

        ' Vertical Walls and Items
        For x As Integer = 0 To selectionData.verticalWalls.Length - 1
            For y As Integer = 0 To selectionData.verticalWalls(0).Length - 1
                If oldCellX + x >= 0 AndAlso oldCellX + x < verticalWalls.Length AndAlso oldCellY + y >= 0 AndAlso oldCellY + y < verticalWalls(0).Length Then
                    verticalWalls(oldCellX + x)(oldCellY + y) = True ' Default state
                    verticalWallItems(oldCellX + x)(oldCellY + y) = Nothing
                End If
            Next
        Next

        ' Horizontal Walls and Items
        For x As Integer = 0 To selectionData.horizontalWalls.Length - 1
            For y As Integer = 0 To selectionData.horizontalWalls(0).Length - 1
                If oldCellX + x >= 0 AndAlso oldCellX + x < horizontalWalls.Length AndAlso oldCellY + y >= 0 AndAlso oldCellY + y < horizontalWalls(0).Length Then
                    horizontalWalls(oldCellX + x)(oldCellY + y) = True ' Default state
                    horizontalWallItems(oldCellX + x)(oldCellY + y) = Nothing
                End If
            Next
        Next
    End Sub

    Private Sub DrawSelectionData(g As Graphics, baseCellX As Integer, baseCellY As Integer)
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT

        ' Apply a semi-transparent color matrix
        Dim colorMatrix As New Imaging.ColorMatrix()
        colorMatrix.Matrix33 = 0.5F ' Set the alpha to 50%
        Dim imgAttributes As New Imaging.ImageAttributes()
        imgAttributes.SetColorMatrix(colorMatrix, Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)

        ' Draw Placed Items
        For x As Integer = 0 To selectionData.selectionWidth - 1
            For y As Integer = 0 To selectionData.selectionHeight - 1
                If selectionData.placedItems(x)(y) IsNot Nothing Then
                    Dim img As Image = GetImageByKey(selectionData.placedItems(x)(y).ImageKey)
                    If img IsNot Nothing Then
                        ImageAnimator.UpdateFrames(img)
                        Dim xPos As Integer = xOrigin + (baseCellX + x) * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
                        Dim yPos As Integer = yOrigin + (baseCellY + y) * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
                        Dim destRect As New Rectangle(xPos, yPos, SQSizeX, SQSizeY)
                        g.DrawImage(img, destRect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes)
                    End If
                End If
            Next
        Next
    End Sub

    Public Sub UpdateGridSizeUI()
        ' Remove event handlers temporarily to prevent triggering events
        RemoveHandler cboX.SelectedIndexChanged, AddressOf cboX_SelectedIndexChanged
        RemoveHandler cboY.SelectedIndexChanged, AddressOf cboY_SelectedIndexChanged

        ' Update cboX
        If Not cboX.Items.Contains(SQCOUNTX.ToString()) Then
            cboX.Items.Add(SQCOUNTX.ToString())
        End If
        cboX.SelectedItem = SQCOUNTX.ToString()

        ' Update cboY
        If Not cboY.Items.Contains(SQCOUNTY.ToString()) Then
            cboY.Items.Add(SQCOUNTY.ToString())
        End If
        cboY.SelectedItem = SQCOUNTY.ToString()

        ' Recalculate square sizes
        CalculateSquareSizes(picGame.ClientSize.Width, picGame.ClientSize.Height)

        ' Reattach event handlers
        AddHandler cboX.SelectedIndexChanged, AddressOf cboX_SelectedIndexChanged
        AddHandler cboY.SelectedIndexChanged, AddressOf cboY_SelectedIndexChanged

        ' Invalidate picGame to redraw the grid
        picGame.Invalidate()
    End Sub
#End Region

#Region "Utility and Helper Methods"
    Private Function IsHorizontalWallConnectedToEdge(pt As Point) As Boolean
        ' Horizontal walls are between cells (x, y-1) and (x, y)
        Dim x As Integer = pt.X
        Dim y As Integer = pt.Y

        ' Check if adjacent cells have edge style applied
        If y > 0 AndAlso floorsUseEdgeStyle(x)(y - 1) Then Return True
        If y < SQCOUNTY AndAlso floorsUseEdgeStyle(x)(y) Then Return True

        Return False
    End Function
    Private Function IsVerticalWallConnectedToEdge(pt As Point) As Boolean
        ' Vertical walls are between cells (x-1, y) and (x, y)
        Dim x As Integer = pt.X
        Dim y As Integer = pt.Y

        ' Check if adjacent cells have edge style applied
        If x > 0 AndAlso floorsUseEdgeStyle(x - 1)(y) Then Return True
        If x < SQCOUNTX AndAlso floorsUseEdgeStyle(x)(y) Then Return True

        Return False
    End Function
    Private Sub CalculateSquareSizes(clientWidth As Integer, clientHeight As Integer)
        ' Ensure SQCOUNTX and SQCOUNTY are greater than zero
        If SQCOUNTX <= 0 OrElse SQCOUNTY <= 0 Then
            Throw New InvalidOperationException("Grid dimensions must be greater than zero.")
        End If

        SQSizeX = (clientWidth - 2 * INDENT - (SQCOUNTX + 1) * WALL_THICKNESS) \ SQCOUNTX
        SQSizeY = (clientHeight - 2 * INDENT - (SQCOUNTY + 1) * WALL_THICKNESS) \ SQCOUNTY
    End Sub
    Private Sub DrawGrid(g As Graphics)
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT

        ' Draw floor squares
        For x As Integer = 0 To SQCOUNTX - 1
            For y As Integer = 0 To SQCOUNTY - 1
                Dim floorRect As New Rectangle(
                xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
                yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
                SQSizeX,
                SQSizeY
            )

                If floorsUseEdgeStyle(x)(y) Then
                    Using floorBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
                        g.FillRectangle(floorBrush, floorRect)
                    End Using
                Else
                    Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
                        g.FillRectangle(floorBrush, floorRect)
                    End Using
                End If
            Next
        Next

        ' Draw wall intersections
        For x As Integer = 0 To SQCOUNTX
            For y As Integer = 0 To SQCOUNTY
                Dim intersectionRect As New Rectangle(
                xOrigin + x * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2,
                yOrigin + y * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2,
                WALL_THICKNESS,
                WALL_THICKNESS
            )

                Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
                    g.FillRectangle(wallBrush, intersectionRect)
                End Using
            Next
        Next

        ' Draw vertical walls or floor
        For x As Integer = 0 To SQCOUNTX
            For y As Integer = 0 To SQCOUNTY - 1
                Dim wallRect As New Rectangle(
                xOrigin + x * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2,
                yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
                WALL_THICKNESS,
                SQSizeY
            )

                If x >= 0 AndAlso x < verticalWalls.Length AndAlso
               verticalWalls(x) IsNot Nothing AndAlso
               y >= 0 AndAlso y < verticalWalls(x).Length Then

                    If verticalWalls(x)(y) Then
                        ' Draw wall
                        Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
                            g.FillRectangle(wallBrush, wallRect)
                        End Using
                    Else
                        ' Draw floor
                        Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
                            g.FillRectangle(floorBrush, wallRect)
                        End Using
                    End If
                End If
            Next
        Next

        ' Draw horizontal walls or floor
        For x As Integer = 0 To SQCOUNTX - 1
            For y As Integer = 0 To SQCOUNTY
                Dim wallRect As New Rectangle(
                xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
                yOrigin + y * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2,
                SQSizeX,
                WALL_THICKNESS
            )

                If x >= 0 AndAlso x < horizontalWalls.Length AndAlso
               horizontalWalls(x) IsNot Nothing AndAlso
               y >= 0 AndAlso y < horizontalWalls(x).Length Then

                    If horizontalWalls(x)(y) Then
                        ' Draw wall
                        Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
                            g.FillRectangle(wallBrush, wallRect)
                        End Using
                    Else
                        ' Draw floor
                        Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
                            g.FillRectangle(floorBrush, wallRect)
                        End Using
                    End If
                End If
            Next
        Next

        ' Draw items
        DrawItems(g)
        DrawWallItems(g)

        ' Draw grid numbers (X-axis and Y-axis)
        DrawGridNumbers(g)
    End Sub
    Private Sub DrawGridNumbers(g As Graphics)
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT
        Dim font As New Font("Arial", 10, FontStyle.Regular)
        Dim brush As New SolidBrush(Color.Black)

        ' Adjustments
        Dim columnNumberOffsetY As Single = -20.0F  ' Move column numbers higher
        Dim rowNumberOffsetX As Single = -5.0F      ' Move row numbers to the right

        ' Drawing numbers along the X-axis (top)
        For x As Integer = 0 To SQCOUNTX - 1
            Dim xPos As Single = xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2 + CSng(SQSizeX) / 2
            Dim yPos As Single = yOrigin + columnNumberOffsetY  ' Adjusted position

            Dim label As String = x.ToString()
            Dim xLabelSize As SizeF = g.MeasureString(label, font)
            g.DrawString(label, font, brush, xPos - xLabelSize.Width / 2.0F, yPos)
        Next

        ' Drawing numbers along the Y-axis (left)
        For y As Integer = 0 To SQCOUNTY - 1
            Dim xPos As Single = xOrigin + rowNumberOffsetX  ' Adjusted position
            Dim yPos As Single = yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2 + CSng(SQSizeY) / 2

            Dim label As String = y.ToString()
            Dim yLabelSize As SizeF = g.MeasureString(label, font)
            g.DrawString(label, font, brush, xPos - yLabelSize.Width, yPos - yLabelSize.Height / 2.0F)
        Next

        ' Clean up
        font.Dispose()
        brush.Dispose()
    End Sub
    Friend Sub FillBorderWalls()
        ' Fill vertical walls along the left and right borders
        For y As Integer = 0 To SQCOUNTY - 1
            verticalWalls(0)(y) = True
            verticalWalls(SQCOUNTX)(y) = True
        Next

        ' Fill horizontal walls along the top and bottom borders
        For x As Integer = 0 To SQCOUNTX - 1
            horizontalWalls(x)(0) = True
            horizontalWalls(x)(SQCOUNTY) = True
        Next
    End Sub
    Private Sub DrawItems(g As Graphics)
        For x As Integer = 0 To placedItems.Length - 1
            If placedItems(x) IsNot Nothing Then
                For y As Integer = 0 To placedItems(x).Length - 1
                    If placedItems(x)(y) IsNot Nothing Then
                        Dim img As Image = GetImageByKey(placedItems(x)(y).ImageKey)
                        If img IsNot Nothing Then
                            ImageAnimator.UpdateFrames(img)
                            Dim xPos As Integer = INDENT + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
                            Dim yPos As Integer = INDENT + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
                            Dim destRect As New Rectangle(xPos, yPos, SQSizeX, SQSizeY)

                            ' Draw the item with preserved transparency
                            Dim imgAttributes As New Imaging.ImageAttributes()
                            imgAttributes.SetWrapMode(WrapMode.TileFlipXY)
                            g.DrawImage(img, destRect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes)
                        End If
                    End If
                Next
            End If
        Next
    End Sub

    Private Sub DrawWallItems(g As Graphics)
        ' Draw vertical wall items
        For x As Integer = 0 To verticalWallItems.Length - 1
            If verticalWallItems(x) IsNot Nothing Then
                For y As Integer = 0 To verticalWallItems(x).Length - 1
                    If verticalWallItems(x)(y) IsNot Nothing Then
                        Dim img As Image = GetImageByKey(verticalWallItems(x)(y).ImageKey)
                        If img IsNot Nothing Then
                            ImageAnimator.UpdateFrames(img)
                            Dim wallX As Integer = INDENT + x * (SQSizeX + WALL_THICKNESS) - (WALL_THICKNESS \ 2)
                            Dim wallY As Integer = INDENT + y * (SQSizeY + WALL_THICKNESS) + (WALL_THICKNESS \ 2)
                            Dim wallWidth As Integer = WALL_THICKNESS
                            Dim wallHeight As Integer = SQSizeY

                            ' Keep original image width, stretch height
                            Dim newWidth As Integer = img.Width
                            Dim newHeight As Integer = wallHeight

                            ' Center horizontally
                            Dim centeredX As Integer = wallX + (wallWidth - newWidth) \ 2
                            Dim drawRect As New Rectangle(centeredX, wallY, newWidth, newHeight)
                            g.DrawImage(img, drawRect)
                        End If
                    End If
                Next
            End If
        Next

        ' Draw horizontal wall items
        For x As Integer = 0 To horizontalWallItems.Length - 1
            If horizontalWallItems(x) IsNot Nothing Then
                For y As Integer = 0 To horizontalWallItems(x).Length - 1
                    If horizontalWallItems(x)(y) IsNot Nothing Then
                        Dim img As Image = GetImageByKey(horizontalWallItems(x)(y).ImageKey)
                        If img IsNot Nothing Then
                            ImageAnimator.UpdateFrames(img)
                            Dim wallX As Integer = INDENT + x * (SQSizeX + WALL_THICKNESS) + (WALL_THICKNESS \ 2)
                            Dim wallY As Integer = INDENT + y * (SQSizeY + WALL_THICKNESS) - (WALL_THICKNESS \ 2)
                            Dim wallWidth As Integer = SQSizeX
                            Dim wallHeight As Integer = WALL_THICKNESS

                            ' Stretch width, keep original image height
                            Dim newWidth As Integer = wallWidth
                            Dim newHeight As Integer = img.Height

                            ' Center vertically
                            Dim centeredY As Integer = wallY + (wallHeight - newHeight) \ 2
                            Dim drawRect As New Rectangle(wallX, centeredY, newWidth, newHeight)
                            g.DrawImage(img, drawRect)
                        End If
                    End If
                Next
            End If
        Next
    End Sub

    Private Sub DrawHighlight(g As Graphics)
        If Not isMouseInsideGrid OrElse highlightedX < 0 OrElse highlightedY < 0 Then
            Return
        End If

        Using highlightPen As New Pen(highlightColor, 2) With {.DashStyle = DashStyle.Dash}
            If highlightedWallX Then
                Dim x As Integer = INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2
                Dim y As Integer = INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
                Dim rect As New Rectangle(x, y, WALL_THICKNESS, SQSizeY)
                g.DrawRectangle(highlightPen, rect)
            ElseIf highlightedWallY Then
                Dim x As Integer = INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
                Dim y As Integer = INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2
                Dim rect As New Rectangle(x, y, SQSizeX, WALL_THICKNESS)
                g.DrawRectangle(highlightPen, rect)
            Else
                Dim x As Integer = INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
                Dim y As Integer = INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
                Dim rect As New Rectangle(x, y, SQSizeX, SQSizeY)
                g.DrawRectangle(highlightPen, rect)
            End If
        End Using
    End Sub
    Private Sub UpdateHighlight(mouseX As Integer, mouseY As Integer)
        mouseX -= INDENT
        mouseY -= INDENT

        isMouseInsideGrid = False
        highlightedWallX = False
        highlightedWallY = False
        highlightedX = -1
        highlightedY = -1

        If mouseX >= 0 AndAlso mouseY >= 0 Then
            Dim cellSizeX As Integer = SQSizeX + WALL_THICKNESS
            Dim cellSizeY As Integer = SQSizeY + WALL_THICKNESS
            Dim cellX As Integer = mouseX \ cellSizeX
            Dim cellY As Integer = mouseY \ cellSizeY

            ' Check if cellX and cellY are within grid bounds
            If cellX >= 0 AndAlso cellX < SQCOUNTX AndAlso
           cellY >= 0 AndAlso cellY < SQCOUNTY Then

                Dim offsetX As Integer = mouseX Mod cellSizeX
                Dim offsetY As Integer = mouseY Mod cellSizeY

                highlightedWallX = offsetX < WALL_THICKNESS
                highlightedWallY = offsetY < WALL_THICKNESS
                highlightedX = cellX
                highlightedY = cellY

                isMouseInsideGrid = True
            End If
        End If
    End Sub
    Private Function GetRectangle(p1 As Point, p2 As Point) As Rectangle
        Dim x As Integer = Math.Min(p1.X, p2.X)
        Dim y As Integer = Math.Min(p1.Y, p2.Y)
        Dim width As Integer = Math.Abs(p1.X - p2.X)
        Dim height As Integer = Math.Abs(p1.Y - p2.Y)
        Return New Rectangle(x, y, width, height)
    End Function

    Private Sub SetEdgeStyleAtMousePosition(mouseX As Integer, mouseY As Integer)
        UpdateHighlight(mouseX, mouseY)
        If Not highlightedWallX AndAlso Not highlightedWallY Then
            If highlightedX >= 0 AndAlso highlightedX < SQCOUNTX AndAlso
           highlightedY >= 0 AndAlso highlightedY < SQCOUNTY Then
                floorsUseEdgeStyle(highlightedX)(highlightedY) = True

                ' Remove wall items around the cell and place walls
                ' Left vertical wall
                If highlightedX >= 0 AndAlso highlightedX < verticalWalls.Length AndAlso
               verticalWalls(highlightedX) IsNot Nothing AndAlso
               highlightedY >= 0 AndAlso highlightedY < verticalWalls(highlightedX).Length Then
                    verticalWallItems(highlightedX)(highlightedY) = Nothing
                    verticalWalls(highlightedX)(highlightedY) = True
                End If

                ' Right vertical wall
                If highlightedX + 1 >= 0 AndAlso highlightedX + 1 < verticalWalls.Length AndAlso
               verticalWalls(highlightedX + 1) IsNot Nothing AndAlso
               highlightedY >= 0 AndAlso highlightedY < verticalWalls(highlightedX + 1).Length Then
                    verticalWallItems(highlightedX + 1)(highlightedY) = Nothing
                    verticalWalls(highlightedX + 1)(highlightedY) = True
                End If

                ' Similar checks for horizontal walls...

                picGame.Invalidate()
            End If
        End If
    End Sub

    Private Function GetCellsInRectangle(rect As Rectangle) As List(Of Point)
        Dim selectedCells As New List(Of Point)
        For x As Integer = 0 To SQCOUNTX - 1
            For y As Integer = 0 To SQCOUNTY - 1
                Dim cellRect As Rectangle = GetCellRectangle(x, y)
                If rect.IntersectsWith(cellRect) Then
                    selectedCells.Add(New Point(x, y))
                End If
            Next
        Next
        Return selectedCells
    End Function

    Private Function GetCellRectangle(x As Integer, y As Integer) As Rectangle
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT
        Return New Rectangle(
        xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
        yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
        SQSizeX, SQSizeY)
    End Function

    Private Function GetVerticalWallsInRectangle(rect As Rectangle) As List(Of Point)
        Dim selectedWalls As New List(Of Point)
        For x As Integer = 0 To SQCOUNTX
            For y As Integer = 0 To SQCOUNTY - 1
                Dim wallRect As Rectangle = GetVerticalWallRectangle(x, y)
                If rect.IntersectsWith(wallRect) Then
                    selectedWalls.Add(New Point(x, y))
                End If
            Next
        Next
        Return selectedWalls
    End Function

    Private Function GetVerticalWallRectangle(x As Integer, y As Integer) As Rectangle
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT
        Return New Rectangle(
        xOrigin + x * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2,
        yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
        WALL_THICKNESS, SQSizeY)
    End Function

    Private Function GetHorizontalWallsInRectangle(rect As Rectangle) As List(Of Point)
        Dim selectedWalls As New List(Of Point)
        For x As Integer = 0 To SQCOUNTX - 1
            For y As Integer = 0 To SQCOUNTY
                Dim wallRect As Rectangle = GetHorizontalWallRectangle(x, y)
                If rect.IntersectsWith(wallRect) Then
                    selectedWalls.Add(New Point(x, y))
                End If
            Next
        Next
        Return selectedWalls
    End Function

    Private Function GetHorizontalWallRectangle(x As Integer, y As Integer) As Rectangle
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT
        Return New Rectangle(
        xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
        yOrigin + y * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2,
        SQSizeX, WALL_THICKNESS)
    End Function

    Private Sub RandomizeWalls()
        wallsColor1 = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))
        wallsColor2 = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))
        Dim hatchStyles As Array = [Enum].GetValues(GetType(HatchStyle))
        wallsHatchStyle = CType(hatchStyles.GetValue(rand.Next(hatchStyles.Length)), HatchStyle)

        ' Update the HatchStyle list selection if Walls radio button is checked
        UpdateSelections()
    End Sub

    Private Sub RandomizeFloors()
        floorsColor1 = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))
        floorsColor2 = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))
        Dim hatchStyles As Array = [Enum].GetValues(GetType(HatchStyle))
        floorsHatchStyle = CType(hatchStyles.GetValue(rand.Next(hatchStyles.Length)), HatchStyle)

        ' Update the HatchStyle list selection if Floors radio button is checked
        UpdateSelections()
    End Sub

    Private Sub UpdateSelections()
        ' Update Hatch Style Selection
        If rdoWalls.Checked Then
            lstHatchstyles.SelectedItem = wallsHatchStyle.ToString()
        ElseIf rdoFloors.Checked Then
            lstHatchstyles.SelectedItem = floorsHatchStyle.ToString()
        ElseIf rdoEdge.Checked Then
            lstHatchstyles.SelectedItem = wallsHatchStyle.ToString()
        End If

        ' Update Music Selection
        If Not String.IsNullOrEmpty(SelectedMusic) Then
            If cboMusic.Items.Contains(SelectedMusic) Then
                cboMusic.SelectedItem = SelectedMusic
            Else
                MessageBox.Show("The music file '" & SelectedMusic & "' is not available.", "Music Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                cboMusic.SelectedIndex = -1
            End If
        Else
            cboMusic.SelectedIndex = -1 ' No selection
        End If
    End Sub

    Private Sub AddColorPictureBoxHandlers(parent As Control)
        For Each ctrl As Control In parent.Controls
            If TypeOf ctrl Is PictureBox AndAlso ctrl.Name.StartsWith("col") Then
                AddHandler ctrl.Click, AddressOf ColorPictureBox_Click
            End If
            If ctrl.HasChildren Then
                AddColorPictureBoxHandlers(ctrl)
            End If
        Next
    End Sub

    Private Sub ColorPictureBox_Click(sender As Object, e As EventArgs)
        If Not isRestoringState Then SaveStateForUndo()
        Dim pb As PictureBox = CType(sender, PictureBox)
        Dim selectedColor As Color = pb.BackColor

        If pb.Name.StartsWith("col") Then
            Dim numStr As String = pb.Name.Substring(3)
            Dim num As Integer
            If Integer.TryParse(numStr, num) Then
                If num >= 1 AndAlso num <= 48 Then
                    ' Update Color1
                    If rdoFloors.Checked Then
                        floorsColor1 = selectedColor
                    ElseIf rdoWalls.Checked Then
                        wallsColor1 = selectedColor
                    ElseIf rdoEdge.Checked Then
                        wallsColor1 = selectedColor ' For edge, use wall's color
                    End If
                ElseIf num >= 51 AndAlso num <= 98 Then
                    ' Update Color2
                    If rdoFloors.Checked Then
                        floorsColor2 = selectedColor
                    ElseIf rdoWalls.Checked Then
                        wallsColor2 = selectedColor
                    ElseIf rdoEdge.Checked Then
                        wallsColor2 = selectedColor ' For edge, use wall's color
                    End If
                End If
            End If
        End If

        picGame.Invalidate()
    End Sub

    Private Sub PicBlock_MouseDown(sender As Object, e As MouseEventArgs)
        Dim pic As PictureBox = CType(sender, PictureBox)

        isDraggingItem = True
        isActionInProgress = True
        If Not isRestoringState Then SaveStateForUndo()

        ' Create a new PlacedItem with the ImageKey
        draggedItem = New PlacedItem() With {.ImageKey = pic.Name}

        ' Determine if it's a wall item
        draggedItemIsWall = IsVerticalItem(pic.Name) OrElse IsHorizontalItem(pic.Name)
        draggedItemIsVerticalWall = IsVerticalItem(pic.Name)

        ' Since it's being dragged from the toolbox, there is no original position
        draggedItemOriginalX = -1
        draggedItemOriginalY = -1

        ' Capture the mouse events at the form level
        Me.Capture = True

        Cursor.Current = Cursors.Hand

        picGame.Invalidate()
    End Sub

    Private Sub ReturnItemToOriginalLocation()
        If draggedItemIsWall Then
            If draggedItemOriginalX >= 0 AndAlso draggedItemOriginalY >= 0 Then
                If draggedItemIsVerticalWall Then
                    verticalWallItems(draggedItemOriginalX)(draggedItemOriginalY) = draggedItem
                    verticalWalls(draggedItemOriginalX)(draggedItemOriginalY) = False
                Else
                    horizontalWallItems(draggedItemOriginalX)(draggedItemOriginalY) = draggedItem
                    horizontalWalls(draggedItemOriginalX)(draggedItemOriginalY) = False
                End If
            End If
        Else
            If draggedItemOriginalX >= 0 AndAlso draggedItemOriginalY >= 0 Then
                placedItems(draggedItemOriginalX)(draggedItemOriginalY) = draggedItem
            End If
        End If
    End Sub

    Private Function GetImageByKey(imageKey As String) As Image
        Select Case imageKey
            Case "picBlock1"
                Return picBlock1.Image
            Case "picBaddy1"
                Return picBaddy1.Image
            Case "picBaddy2"
                Return picBaddy2.Image
            Case "picLazer1"
                Return picLazer1.Image
            Case "picLazer2"
                Return picLazer2.Image
            Case "picFlipper1"
                Return picFlipper1.Image
            Case "picFlipper2"
                Return picFlipper2.Image
            Case "picFlipper3"
                Return picFlipper3.Image
            Case "picFlipper4"
                Return picFlipper4.Image
            Case "picBaddy3"
                Return picBaddy3.Image
            Case "picBaddy4"
                Return picBaddy4.Image
            Case "picCoin"
                Return picCoin.Image
            Case "picGate"
                Return picGate.Image
            Case "picKey"
                Return picKey.Image
            Case "picStart"
                Return picStart.Image
            Case "picFinish"
                Return picFinish.Image
            Case "picBlockStopper1"
                Return picBlockStopper1.Image
            Case "picBall"
                Return picBall.Image
            Case "picBlockSwitch"
                Return picBlockSwitch.Image
            Case "picBomb"
                Return picBomb.Image
            Case "picWarp"
                Return picWarp.Image
            Case "picWarp2"
                Return picWarp2.Image
            Case Else
                Return Nothing
        End Select
    End Function
    Private Function IsVerticalItem(imgKey As String) As Boolean
        Return imgKey = "picLazer1" OrElse imgKey = "picFlipper1" OrElse imgKey = "picFlipper2"
    End Function

    Private Function IsHorizontalItem(imgKey As String) As Boolean
        Return imgKey = "picLazer2" OrElse imgKey = "picFlipper3" OrElse imgKey = "picFlipper4"
    End Function

    Private Sub AddWallAtMousePosition(mouseX As Integer, mouseY As Integer)
        UpdateHighlight(mouseX, mouseY)
        If highlightedWallX Then
            If highlightedX >= 0 AndAlso highlightedX < verticalWalls.Length AndAlso
           highlightedY >= 0 AndAlso highlightedY < verticalWalls(0).Length Then
                If verticalWallItems(highlightedX)(highlightedY) Is Nothing AndAlso
               highlightedX > 0 AndAlso highlightedX < SQCOUNTX Then
                    verticalWalls(highlightedX)(highlightedY) = True
                    picGame.Invalidate()
                End If
            End If
        ElseIf highlightedWallY Then
            If highlightedX >= 0 AndAlso highlightedX < horizontalWalls.Length AndAlso
           highlightedY >= 0 AndAlso highlightedY < horizontalWalls(0).Length Then
                If horizontalWallItems(highlightedX)(highlightedY) Is Nothing AndAlso
               highlightedY > 0 AndAlso highlightedY < SQCOUNTY Then
                    horizontalWalls(highlightedX)(highlightedY) = True
                    picGame.Invalidate()
                End If
            End If
        End If
    End Sub
    Private Sub RemoveObjectAtMousePosition(mouseX As Integer, mouseY As Integer, removeWalls As Boolean, removeItems As Boolean)
        UpdateHighlight(mouseX, mouseY)

        If Not isMouseInsideGrid Then
            ' Mouse is outside of grid; do nothing
            Return
        End If

        If highlightedWallX Then
            If highlightedX >= 0 AndAlso highlightedX < verticalWalls.Length AndAlso
           verticalWalls(highlightedX) IsNot Nothing AndAlso
           highlightedY >= 0 AndAlso highlightedY < verticalWalls(highlightedX).Length Then

                If removeItems AndAlso verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                    verticalWallItems(highlightedX)(highlightedY) = Nothing
                    picGame.Invalidate()
                ElseIf removeWalls AndAlso verticalWalls(highlightedX)(highlightedY) Then
                    ' Check if it's an outer wall
                    If Not IsVerticalWallOuterBorder(highlightedX) AndAlso Not IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
                        verticalWalls(highlightedX)(highlightedY) = False
                        picGame.Invalidate()
                    End If
                End If
            End If
        ElseIf highlightedWallY Then
            If highlightedX >= 0 AndAlso highlightedX < horizontalWalls.Length AndAlso
           horizontalWalls(highlightedX) IsNot Nothing AndAlso
           highlightedY >= 0 AndAlso highlightedY < horizontalWalls(highlightedX).Length Then

                If removeItems AndAlso horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                    horizontalWallItems(highlightedX)(highlightedY) = Nothing
                    picGame.Invalidate()
                ElseIf removeWalls AndAlso horizontalWalls(highlightedX)(highlightedY) Then
                    ' Check if it's an outer wall
                    If Not IsHorizontalWallOuterBorder(highlightedY) AndAlso Not IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
                        horizontalWalls(highlightedX)(highlightedY) = False
                        picGame.Invalidate()
                    End If
                End If
            End If
        Else
            If highlightedX >= 0 AndAlso highlightedX < placedItems.Length AndAlso
           placedItems(highlightedX) IsNot Nothing AndAlso
           highlightedY >= 0 AndAlso highlightedY < placedItems(highlightedX).Length Then

                If removeItems AndAlso placedItems(highlightedX)(highlightedY) IsNot Nothing Then
                    placedItems(highlightedX)(highlightedY) = Nothing
                    picGame.Invalidate()
                End If
            End If
        End If
    End Sub
    Private Sub LoadRoomFromFile(filePath As String)
        Try
            If Not File.Exists(filePath) Then Return

            Dim options As New JsonSerializerOptions With {
            .IncludeFields = True
        }
            Dim jsonString As String = File.ReadAllText(filePath)
            Dim roomData As RoomData = JsonSerializer.Deserialize(Of RoomData)(jsonString, options)

            If roomData Is Nothing Then
                MessageBox.Show("Error: Failed to deserialize room data.")
                Return
            End If

            ' Ensure music is loaded before restoring state
            LoadMusic()

            ' Restore the state
            isRestoringState = True
            roomData.RestoreState(Me)
            isRestoringState = False

            ' Update selections
            UpdateSelections()
        Catch ex As Exception
            MessageBox.Show("Error loading room: " & ex.Message)
        End Try
    End Sub
    Private Sub LoadSettingsFromFile(filePath As String)
        Try
            If Not File.Exists(filePath) Then Return
            Dim lines As String() = File.ReadAllLines(filePath)
            Dim section As String = ""
            For Each line As String In lines
                line = line.Trim()
                If String.IsNullOrEmpty(line) OrElse line.StartsWith(";") Then Continue For
                If line.StartsWith("[") AndAlso line.EndsWith("]") Then
                    section = line.Substring(1, line.Length - 2)
                Else
                    Dim parts() As String = line.Split(New Char() {":"c}, 2)
                    If parts.Length = 2 Then
                        LoadProperty(parts(0).Trim(), parts(1).Trim(), section)
                    End If
                End If
            Next

            ' Update lstHatchstyles selection after loading settings
            UpdateSelections()
        Catch ex As Exception
            MessageBox.Show("Error loading settings: " & ex.Message)
        End Try
    End Sub

    Private Sub LoadProperty(key As String, value As String, section As String)
        Dim isWall As Boolean = (section = "Walls")
        Select Case key
            Case "HatchStyle"
                Dim hatchStyle As HatchStyle
                If [Enum].TryParse(value, hatchStyle) Then
                    If isWall Then wallsHatchStyle = hatchStyle Else floorsHatchStyle = hatchStyle
                End If
            Case "Color1"
                Dim color As Color = ParseColor(value)
                If isWall Then wallsColor1 = color Else floorsColor1 = color
            Case "Color2"
                Dim color As Color = ParseColor(value)
                If isWall Then wallsColor2 = color Else floorsColor2 = color
        End Select
    End Sub

    Private Function ParseColor(colorString As String) As Color
        Dim parts() As String = colorString.Split(","c)
        If parts.Length = 4 Then
            Dim a, r, g, b As Integer
            If Integer.TryParse(parts(0), a) AndAlso Integer.TryParse(parts(1), r) AndAlso
               Integer.TryParse(parts(2), g) AndAlso Integer.TryParse(parts(3), b) Then
                Return Color.FromArgb(a, r, g, b)
            End If
        End If
        Return Color.Black
    End Function

    Private Sub SaveSettingsToFile(filePath As String)
        Try
            Using sw As New StreamWriter(filePath)
                sw.WriteLine("[Walls]")
                sw.WriteLine("HatchStyle: " & wallsHatchStyle.ToString())
                sw.WriteLine("Color1: " & ColorToString(wallsColor1))
                sw.WriteLine("Color2: " & ColorToString(wallsColor2))
                sw.WriteLine()
                sw.WriteLine("[Floors]")
                sw.WriteLine("HatchStyle: " & floorsHatchStyle.ToString())
                sw.WriteLine("Color1: " & ColorToString(floorsColor1))
                sw.WriteLine("Color2: " & ColorToString(floorsColor2))
            End Using
        Catch ex As Exception
            MessageBox.Show("Error saving settings: " & ex.Message)
        End Try
    End Sub

    Private Function ColorToString(color As Color) As String
        Return $"{color.A},{color.R},{color.G},{color.B}"
    End Function

    Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
        ' Save the current room data to a temporary file
        Dim tempFilePath As String = Path.Combine(Path.GetTempPath(), "temp.room")
        SaveRoomToFile(tempFilePath)

        ' Open frmGame and pass the temp file path
        Dim gameForm As New frmGame()
        gameForm.RoomFilePath = tempFilePath
        gameForm.Show()
    End Sub
    ' Helper method to clone jagged arrays
    Private Function CloneJaggedArray(Of T)(sourceArray As T()()) As T()()
        Dim lengthOuter As Integer = sourceArray.Length
        Dim newArray As T()() = New T(lengthOuter - 1)() {}
        For i As Integer = 0 To lengthOuter - 1
            If sourceArray(i) IsNot Nothing Then
                Dim lengthInner As Integer = sourceArray(i).Length
                newArray(i) = New T(lengthInner - 1) {}
                Array.Copy(sourceArray(i), newArray(i), lengthInner)
            End If
        Next
        Return newArray
    End Function

    Private Function ConvertPlacedItemsToStrings(items As PlacedItem()()) As String()()
        Dim lengthOuter As Integer = items.Length
        Dim stringItems As String()() = New String(lengthOuter - 1)() {}
        For x As Integer = 0 To lengthOuter - 1
            If items(x) IsNot Nothing Then
                Dim lengthInner As Integer = items(x).Length
                stringItems(x) = New String(lengthInner - 1) {}
                For y As Integer = 0 To lengthInner - 1
                    If items(x)(y) IsNot Nothing Then
                        stringItems(x)(y) = items(x)(y).ImageKey
                    Else
                        stringItems(x)(y) = Nothing
                    End If
                Next
            End If
        Next
        Return stringItems
    End Function
    ' In frmDesigner.vb
    Public Sub SaveRoomToFile(filePath As String)
        Try
            ' Create a new RoomData object capturing the current state
            Dim currentState As New RoomData(Me)

            ' Set up JSON serialization options
            Dim options As New JsonSerializerOptions With {
                .WriteIndented = True,
                .IncludeFields = True
            }

            ' Serialize the RoomData object to a JSON string
            Dim jsonString As String = JsonSerializer.Serialize(currentState, options)

            ' Write the JSON string to the specified file
            File.WriteAllText(filePath, jsonString)
        Catch ex As Exception
            MessageBox.Show("Error saving room: " & ex.Message)
        End Try
    End Sub

#End Region
End Class

Option Explicit On
Option Strict On

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Text.Json
Imports System.Text.Json.Serialization

' RoomState class represents the state of the room and can save/restore it
Public Class RoomState
    ' In RoomState class
    Public Property GridSizeX As Integer
    Public Property GridSizeY As Integer
    ' Properties to save colors and styles
    Public wallsHatchStyleValue As Integer
    Public wallsColor1Argb As Integer
    Public wallsColor2Argb As Integer
    Public floorsHatchStyleValue As Integer
    Public floorsColor1Argb As Integer
    Public floorsColor2Argb As Integer

    ' Arrays to save wall states
    Public verticalWalls()() As Boolean
    Public horizontalWalls()() As Boolean

    ' Arrays to save placed items
    Public placedItems()() As PlacedItemData
    Public verticalWallItems()() As PlacedItemData
    Public horizontalWallItems()() As PlacedItemData

    ' Arrays to save floor edge styles
    Public floorsUseEdgeStyle()() As Boolean

    ' Property to save the selected music
    Public SelectedMusic As String

    ' Property to save the level name
    Public LevelName As String

    ' Parameterless constructor for deserialization
    Public Sub New()
        ' This constructor is used by the deserializer
    End Sub

    ' Constructor: captures the current state of the frmDesigner
    ' Constructor: captures the current state of the frmDesigner
    Public Sub New(roomView As frmDesigner)
        ' Save colors and styles as integers
        wallsHatchStyleValue = CInt(roomView.wallsHatchStyle)
        wallsColor1Argb = roomView.wallsColor1.ToArgb()
        wallsColor2Argb = roomView.wallsColor2.ToArgb()
        floorsHatchStyleValue = CInt(roomView.floorsHatchStyle)
        floorsColor1Argb = roomView.floorsColor1.ToArgb()
        floorsColor2Argb = roomView.floorsColor2.ToArgb()

        ' Save grid size
        GridSizeX = roomView.SQCOUNTX
        GridSizeY = roomView.SQCOUNTY

        ' Save wall states
        verticalWalls = CloneJaggedArray(roomView.verticalWalls)
        horizontalWalls = CloneJaggedArray(roomView.horizontalWalls)

        ' Save placed items
        placedItems = CopyPlacedItems(roomView.placedItems)
        verticalWallItems = CopyPlacedItems(roomView.verticalWallItems)
        horizontalWallItems = CopyPlacedItems(roomView.horizontalWallItems)

        ' Save floorsUseEdgeStyle
        floorsUseEdgeStyle = CloneJaggedArray(roomView.floorsUseEdgeStyle)

        ' Save the selected music
        SelectedMusic = roomView.SelectedMusic

        ' Save the level name
        LevelName = roomView.txtName.Text
    End Sub
    ' Helper method to clone jagged arrays
    Private Function CloneJaggedArray(Of T)(sourceArray As T()()) As T()()
        Dim lengthOuter As Integer = sourceArray.Length
        Dim newArray As T()() = New T(lengthOuter - 1)() {}
        For i As Integer = 0 To lengthOuter - 1
            If sourceArray(i) IsNot Nothing Then
                Dim lengthInner As Integer = sourceArray(i).Length
                newArray(i) = New T(lengthInner - 1) {}
                Array.Copy(sourceArray(i), newArray(i), lengthInner)
            End If
        Next
        Return newArray
    End Function

    ' Helper method to copy placed items
    Private Function CopyPlacedItems(items As PlacedItem()()) As PlacedItemData()()
        Dim lengthOuter As Integer = items.Length
        Dim copiedItems As PlacedItemData()() = New PlacedItemData(lengthOuter - 1)() {}
        For x As Integer = 0 To lengthOuter - 1
            If items(x) IsNot Nothing Then
                Dim lengthInner As Integer = items(x).Length
                copiedItems(x) = New PlacedItemData(lengthInner - 1) {}
                For y As Integer = 0 To lengthInner - 1
                    If items(x)(y) IsNot Nothing Then
                        copiedItems(x)(y) = New PlacedItemData With {.ImageKey = items(x)(y).ImageKey}
                    Else
                        copiedItems(x)(y) = Nothing
                    End If
                Next
            End If
        Next
        Return copiedItems
    End Function

    ' Helper method to restore placed items
    Private Function RestorePlacedItems(data As PlacedItemData()()) As PlacedItem()()
        Dim lengthOuter As Integer = data.Length
        Dim restoredItems As PlacedItem()() = New PlacedItem(lengthOuter - 1)() {}
        For x As Integer = 0 To lengthOuter - 1
            If data(x) IsNot Nothing Then
                Dim lengthInner As Integer = data(x).Length
                restoredItems(x) = New PlacedItem(lengthInner - 1) {}
                For y As Integer = 0 To lengthInner - 1
                    If data(x)(y) IsNot Nothing Then
                        restoredItems(x)(y) = New PlacedItem With {.ImageKey = data(x)(y).ImageKey}
                    Else
                        restoredItems(x)(y) = Nothing
                    End If
                Next
            End If
        Next
        Return restoredItems
    End Function

    ' Restore the saved state back into the frmDesigner
    Public Sub RestoreState(roomView As frmDesigner)
        ' Restore the grid size
        Dim oldSQCOUNTX As Integer = roomView.SQCOUNTX
        Dim oldSQCOUNTY As Integer = roomView.SQCOUNTY
        roomView.SQCOUNTX = GridSizeX
        roomView.SQCOUNTY = GridSizeY

        ' Resize arrays to match the saved grid size and preserve data
        roomView.ResizeArrays(oldSQCOUNTX, oldSQCOUNTY, roomView.SQCOUNTX, roomView.SQCOUNTY)

        ' Restore colors and styles from stored values
        roomView.wallsHatchStyle = CType(wallsHatchStyleValue, HatchStyle)
        roomView.wallsColor1 = Color.FromArgb(wallsColor1Argb)
        roomView.wallsColor2 = Color.FromArgb(wallsColor2Argb)
        roomView.floorsHatchStyle = CType(floorsHatchStyleValue, HatchStyle)
        roomView.floorsColor1 = Color.FromArgb(floorsColor1Argb)
        roomView.floorsColor2 = Color.FromArgb(floorsColor2Argb)

        ' Restore wall states
        roomView.verticalWalls = CloneJaggedArray(verticalWalls)
        roomView.horizontalWalls = CloneJaggedArray(horizontalWalls)

        ' Restore placed items
        roomView.placedItems = RestorePlacedItems(placedItems)
        roomView.verticalWallItems = RestorePlacedItems(verticalWallItems)
        roomView.horizontalWallItems = RestorePlacedItems(horizontalWallItems)

        ' Restore floorsUseEdgeStyle
        roomView.floorsUseEdgeStyle = CloneJaggedArray(floorsUseEdgeStyle)

        ' Restore the selected music
        roomView.SelectedMusic = SelectedMusic

        ' Restore the level name
        roomView.txtName.Text = LevelName

        ' Update grid size-related UI elements
        roomView.UpdateGridSizeUI()

        ' Invalidate the drawing area to redraw the grid
        roomView.picGame.Invalidate()
    End Sub
End Class

' PlacedItemData class is used for serialization
Public Class SelectionData
    Public placedItems As PlacedItem()()
    Public verticalWalls As Boolean()()
    Public horizontalWalls As Boolean()()
    Public verticalWallItems As PlacedItem()()
    Public horizontalWallItems As PlacedItem()()
    Public floorsUseEdgeStyle As Boolean()()
    Public selectionWidth As Integer ' in cells
    Public selectionHeight As Integer ' in cells
End Class

Option Explicit On
Option Strict On

Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class RoomData
    Public Property GridSizeX As Integer
    Public Property GridSizeY As Integer

    ' Properties to save colors and styles
    Public Property wallsHatchStyleValue As Integer
    Public Property wallsColor1Argb As Integer
    Public Property wallsColor2Argb As Integer
    Public Property floorsHatchStyleValue As Integer
    Public Property floorsColor1Argb As Integer
    Public Property floorsColor2Argb As Integer

    ' Arrays to save wall states
    Public Property verticalWalls As Boolean()()
    Public Property horizontalWalls As Boolean()()

    ' Arrays to save placed items
    Public Property placedItems As PlacedItemData()()
    Public Property verticalWallItems As PlacedItemData()()
    Public Property horizontalWallItems As PlacedItemData()()

    ' Arrays to save floor edge styles
    Public Property floorsUseEdgeStyle As Boolean()()
    ' Property to save the selected music
    Public Property SelectedMusic As String

    ' Property to save the level name
    Public Property LevelName As String

    ' Parameterless constructor for deserialization
    Public Sub New()
    End Sub

    ' Constructor: captures the current state of the frmDesigner
    Public Sub New(roomView As frmDesigner)
        ' Save colors and styles as integers
        wallsHatchStyleValue = CInt(roomView.wallsHatchStyle)
        wallsColor1Argb = roomView.wallsColor1.ToArgb()
        wallsColor2Argb = roomView.wallsColor2.ToArgb()
        floorsHatchStyleValue = CInt(roomView.floorsHatchStyle)
        floorsColor1Argb = roomView.floorsColor1.ToArgb()
        floorsColor2Argb = roomView.floorsColor2.ToArgb()

        ' Save grid size
        GridSizeX = roomView.SQCOUNTX
        GridSizeY = roomView.SQCOUNTY

        ' Save wall states
        verticalWalls = CloneJaggedArray(roomView.verticalWalls)
        horizontalWalls = CloneJaggedArray(roomView.horizontalWalls)

        ' Save placed items
        placedItems = CopyPlacedItems(roomView.placedItems)
        verticalWallItems = CopyPlacedItems(roomView.verticalWallItems)
        horizontalWallItems = CopyPlacedItems(roomView.horizontalWallItems)

        ' Save floorsUseEdgeStyle
        floorsUseEdgeStyle = CloneJaggedArray(roomView.floorsUseEdgeStyle)

        ' Save the selected music
        SelectedMusic = roomView.SelectedMusic

        ' Save the level name
        LevelName = roomView.txtName.Text
    End Sub

    ' Helper method to clone jagged arrays
    Private Function CloneJaggedArray(Of T)(sourceArray As T()()) As T()()
        If sourceArray Is Nothing Then Return Nothing
        Dim lengthOuter As Integer = sourceArray.Length
        Dim newArray As T()() = New T(lengthOuter - 1)() {}
        For i As Integer = 0 To lengthOuter - 1
            If sourceArray(i) IsNot Nothing Then
                Dim lengthInner As Integer = sourceArray(i).Length
                newArray(i) = New T(lengthInner - 1) {}
                Array.Copy(sourceArray(i), newArray(i), lengthInner)
            Else
                newArray(i) = Nothing
            End If
        Next
        Return newArray
    End Function

    ' Helper method to copy placed items
    Private Function CopyPlacedItems(items As PlacedItem()()) As PlacedItemData()()
        If items Is Nothing Then Return Nothing
        Dim lengthOuter As Integer = items.Length
        Dim copiedItems As PlacedItemData()() = New PlacedItemData(lengthOuter - 1)() {}
        For x As Integer = 0 To lengthOuter - 1
            If items(x) IsNot Nothing Then
                Dim lengthInner As Integer = items(x).Length
                copiedItems(x) = New PlacedItemData(lengthInner - 1) {}
                For y As Integer = 0 To lengthInner - 1
                    If items(x)(y) IsNot Nothing Then
                        copiedItems(x)(y) = New PlacedItemData With {.ImageKey = items(x)(y).ImageKey}
                    Else
                        copiedItems(x)(y) = Nothing
                    End If
                Next
            Else
                copiedItems(x) = Nothing
            End If
        Next
        Return copiedItems
    End Function

    ' Restore the saved state back into the frmDesigner
    Public Sub RestoreState(roomView As frmDesigner)
        ' Restore the grid size
        Dim oldSQCOUNTX As Integer = roomView.SQCOUNTX
        Dim oldSQCOUNTY As Integer = roomView.SQCOUNTY
        roomView.SQCOUNTX = GridSizeX
        roomView.SQCOUNTY = GridSizeY

        ' Resize arrays to match the saved grid size and preserve data
        roomView.ResizeArrays(oldSQCOUNTX, oldSQCOUNTY, roomView.SQCOUNTX, roomView.SQCOUNTY)

        ' Restore colors and styles from stored values
        roomView.wallsHatchStyle = CType(wallsHatchStyleValue, HatchStyle)
        roomView.wallsColor1 = Color.FromArgb(wallsColor1Argb)
        roomView.wallsColor2 = Color.FromArgb(wallsColor2Argb)
        roomView.floorsHatchStyle = CType(floorsHatchStyleValue, HatchStyle)
        roomView.floorsColor1 = Color.FromArgb(floorsColor1Argb)
        roomView.floorsColor2 = Color.FromArgb(floorsColor2Argb)

        ' Restore wall states
        roomView.verticalWalls = CloneJaggedArray(verticalWalls)
        roomView.horizontalWalls = CloneJaggedArray(horizontalWalls)

        ' Restore placed items
        roomView.placedItems = RestorePlacedItems(placedItems)
        roomView.verticalWallItems = RestorePlacedItems(verticalWallItems)
        roomView.horizontalWallItems = RestorePlacedItems(horizontalWallItems)

        ' Restore floorsUseEdgeStyle
        roomView.floorsUseEdgeStyle = CloneJaggedArray(floorsUseEdgeStyle)

        ' Restore the selected music
        roomView.SelectedMusic = SelectedMusic

        ' Restore the level name
        roomView.txtName.Text = LevelName

        ' Update grid size-related UI elements
        roomView.UpdateGridSizeUI()

        ' Invalidate the drawing area to redraw the grid
        roomView.picGame.Invalidate()
    End Sub

    ' Helper method to restore placed items
    Private Function RestorePlacedItems(data As PlacedItemData()()) As PlacedItem()()
        If data Is Nothing Then Return Nothing
        Dim lengthOuter As Integer = data.Length
        Dim restoredItems As PlacedItem()() = New PlacedItem(lengthOuter - 1)() {}
        For x As Integer = 0 To lengthOuter - 1
            If data(x) IsNot Nothing Then
                Dim lengthInner As Integer = data(x).Length
                restoredItems(x) = New PlacedItem(lengthInner - 1) {}
                For y As Integer = 0 To lengthInner - 1
                    If data(x)(y) IsNot Nothing Then
                        restoredItems(x)(y) = New PlacedItem With {.ImageKey = data(x)(y).ImageKey}
                    Else
                        restoredItems(x)(y) = Nothing
                    End If
                Next
            Else
                restoredItems(x) = Nothing
            End If
        Next
        Return restoredItems
    End Function
End Class

' PlacedItemData class used for serialization/deserialization
Public Class PlacedItemData
    Public Property ImageKey As String
End Class

' PlacedItem class represents an item placed on the grid
Public Class PlacedItem
    Public Property ImageKey As String
End Class

Public Class SimpleRoomData
    ' Existing properties...
    Public WallsHatchStyleValue As Integer
    Public WallsColor1Argb As Integer
    Public WallsColor2Argb As Integer
    Public FloorsHatchStyleValue As Integer
    Public FloorsColor1Argb As Integer
    Public FloorsColor2Argb As Integer
    Public VerticalWalls()() As Boolean
    Public HorizontalWalls()() As Boolean
    Public placedItems()() As PlacedItemData
    Public verticalWallItems()() As PlacedItemData
    Public horizontalWallItems()() As PlacedItemData
    Public FloorsUseEdgeStyle()() As Boolean

    ' Add these new properties:
    Public LevelName As String
    Public SelectedMusic As String
End Class

Option Explicit On
Option Strict On

Imports System.IO
Imports System.Text.Json
Imports System.Text.Json.Serialization
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Media
Imports System.ComponentModel

Public Class frmGame
    ' Property to receive the room file path from frmDesigner
    Public Property RoomFilePath As String
    Private SelectedMusic As String

    ' Constants for grid layout
    Private Const INDENT As Integer = 20
    Private Const WALL_THICKNESS As Integer = 10

    ' Variables for grid size
    Private SQCOUNTX As Integer
    Private SQCOUNTY As Integer

    ' Variables for square sizes
    Private SQSizeX As Integer
    Private SQSizeY As Integer

    ' Variables for wall and floor styles
    Private wallsHatchStyle As HatchStyle
    Private wallsColor1 As Color
    Private wallsColor2 As Color
    Private floorsHatchStyle As HatchStyle
    Private floorsColor1 As Color
    Private floorsColor2 As Color

    ' Arrays to store walls and items
    Private verticalWalls()() As Boolean
    Private horizontalWalls()() As Boolean
    Private placedItems()() As String
    Private verticalWallItems()() As String
    Private horizontalWallItems()() As String
    Private floorsUseEdgeStyle()() As Boolean

    ' Image resources
    Private imageDictionary As New Dictionary(Of String, Image)
    Private animatedImages As New List(Of Image)

    ' Animation timer
    Private animationTimer As Timer

    ' Flag to indicate when the room data is loaded
    Private isRoomDataLoaded As Boolean = False

    Private musicPlayer As SoundPlayer

    ' Paths
    Private ReadOnly projectDir As String = GetProjectDirectory()
    Private ReadOnly ImagesFolder As String = Path.Combine(projectDir, "Data", "Images")
    Private ReadOnly musicFolder As String = Path.Combine(projectDir, "Data", "Music")

    ' Class to represent the simple room data
    Public Class SimpleRoomData
        Public Property GridSizeX As Integer
        Public Property GridSizeY As Integer

        ' Properties to save colors and styles
        Public Property wallsHatchStyleValue As Integer
        Public Property wallsColor1Argb As Integer
        Public Property wallsColor2Argb As Integer
        Public Property floorsHatchStyleValue As Integer
        Public Property floorsColor1Argb As Integer
        Public Property floorsColor2Argb As Integer

        ' Arrays to save wall states
        Public Property verticalWalls As Boolean()()
        Public Property horizontalWalls As Boolean()()

        ' Arrays to save placed items
        Public Property placedItems As PlacedItemData()()
        Public Property verticalWallItems As PlacedItemData()()
        Public Property horizontalWallItems As PlacedItemData()()

        ' Arrays to save floor edge styles
        Public Property floorsUseEdgeStyle As Boolean()()

        ' Property to save the selected music
        Public Property SelectedMusic As String

        ' Property to save the level name
        Public Property LevelName As String
    End Class

    ' Event handler for form load
    Private Sub frmGame_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Load the room data from file
        If Not String.IsNullOrEmpty(RoomFilePath) AndAlso File.Exists(RoomFilePath) Then
            LoadRoomFromFile(RoomFilePath)
        Else
            MessageBox.Show("Room file not found.")
            Me.Close()
            Return
        End If

        ' Initialize images and animations
        InitializeImages()

        ' Start the animation timer
        animationTimer = New Timer() With {.Interval = 100}
        AddHandler animationTimer.Tick, AddressOf OnAnimationTick
        animationTimer.Start()

        ' Force picTest to repaint
        picTest.Invalidate()
    End Sub

    ' Method to load the room data from file
    Private Sub LoadRoomFromFile(filePath As String)
        Try
            Dim options As New JsonSerializerOptions With {
            .IncludeFields = True,
            .PropertyNameCaseInsensitive = True
        }
            Dim jsonString As String = File.ReadAllText(filePath)
            Dim roomData As SimpleRoomData = JsonSerializer.Deserialize(Of SimpleRoomData)(jsonString, options)

            If roomData Is Nothing Then
                MessageBox.Show("Error: Failed to deserialize room data.")
                Return
            End If

            ' Ensure images are loaded before restoring state
            InitializeImages()

            ' Restore room data
            RestoreRoomData(roomData)
        Catch ex As Exception
            MessageBox.Show("Error loading room: " & ex.ToString())
        End Try
    End Sub

    Private Sub PlaySelectedMusic()
        If musicPlayer IsNot Nothing Then
            ' Stop any currently playing music
            musicPlayer.Stop()
            musicPlayer.Dispose()
            musicPlayer = Nothing
        End If

        If Not String.IsNullOrEmpty(SelectedMusic) Then
            Dim musicFilePath As String = Path.Combine(musicFolder, SelectedMusic & ".wav")
            If File.Exists(musicFilePath) Then
                Try
                    musicPlayer = New SoundPlayer(musicFilePath)
                    ' Set LoadCompleted event handler to play music on repeat
                    AddHandler musicPlayer.LoadCompleted, AddressOf MusicLoadCompleted
                    musicPlayer.LoadAsync()
                Catch ex As Exception
                    MessageBox.Show("Error playing music: " & ex.Message)
                End Try
            Else
                MessageBox.Show("Selected music file not found: " & SelectedMusic)
            End If
        End If
    End Sub

    Private Sub MusicLoadCompleted(sender As Object, e As AsyncCompletedEventArgs)
        If musicPlayer IsNot Nothing Then
            musicPlayer.PlayLooping()
        End If
    End Sub

    ' Method to restore room data from the loaded SimpleRoomData object
    Private Sub RestoreRoomData(roomData As SimpleRoomData)
        Try
            ' Restore grid sizes
            SQCOUNTX = roomData.GridSizeX
            SQCOUNTY = roomData.GridSizeY

            ' Restore colors and styles
            wallsHatchStyle = CType(roomData.wallsHatchStyleValue, HatchStyle)
            wallsColor1 = Color.FromArgb(roomData.wallsColor1Argb)
            wallsColor2 = Color.FromArgb(roomData.wallsColor2Argb)
            floorsHatchStyle = CType(roomData.floorsHatchStyleValue, HatchStyle)
            floorsColor1 = Color.FromArgb(roomData.floorsColor1Argb)
            floorsColor2 = Color.FromArgb(roomData.floorsColor2Argb)

            ' Restore walls and items
            verticalWalls = roomData.verticalWalls
            If verticalWalls Is Nothing OrElse verticalWalls.Length <> SQCOUNTX + 1 Then
                InitializeVerticalWalls()
            End If

            horizontalWalls = roomData.horizontalWalls
            If horizontalWalls Is Nothing OrElse horizontalWalls.Length <> SQCOUNTX Then
                InitializeHorizontalWalls()
            End If

            floorsUseEdgeStyle = roomData.floorsUseEdgeStyle
            If floorsUseEdgeStyle Is Nothing OrElse floorsUseEdgeStyle.Length <> SQCOUNTX - 1 Then
                InitializeFloorsUseEdgeStyle()
            End If

            ' Restore placed items
            placedItems = RestorePlacedItems(roomData.placedItems)
            If placedItems Is Nothing OrElse placedItems.Length <> SQCOUNTX - 1 Then
                InitializePlacedItems()
            End If

            verticalWallItems = RestorePlacedItems(roomData.verticalWallItems)
            If verticalWallItems Is Nothing OrElse verticalWallItems.Length <> SQCOUNTX + 1 Then
                InitializeVerticalWallItems()
            End If

            horizontalWallItems = RestorePlacedItems(roomData.horizontalWallItems)
            If horizontalWallItems Is Nothing OrElse horizontalWallItems.Length <> SQCOUNTX - 1 Then
                InitializeHorizontalWallItems()
            End If

            ' Restore level name and selected music
            SelectedMusic = roomData.SelectedMusic
            lblName.Text = roomData.LevelName
            PlaySelectedMusic()

            ' Set flag indicating room data is loaded
            isRoomDataLoaded = True

            ' Refresh the game picture
            picTest.Invalidate()
        Catch ex As Exception
            MessageBox.Show("Error restoring room data: " & ex.ToString())
        End Try
    End Sub

    Private Function RestorePlacedItems(data As PlacedItemData()()) As String()()
        If data Is Nothing Then
            Return Nothing
        End If

        Dim lengthOuter As Integer = data.Length
        Dim restoredItems As String()() = New String(lengthOuter - 1)() {}

        For x As Integer = 0 To lengthOuter - 1
            If data(x) IsNot Nothing Then
                Dim lengthInner As Integer = data(x).Length
                restoredItems(x) = New String(lengthInner - 1) {}
                For y As Integer = 0 To lengthInner - 1
                    If data(x)(y) IsNot Nothing Then
                        restoredItems(x)(y) = data(x)(y).ImageKey
                    Else
                        restoredItems(x)(y) = Nothing
                    End If
                Next
            Else
                ' Initialize with empty array if data(x) is Nothing
                restoredItems(x) = New String(SQCOUNTY - 1) {}
                For y As Integer = 0 To SQCOUNTY - 1
                    restoredItems(x)(y) = Nothing
                Next
            End If
        Next
        Return restoredItems
    End Function
    Private Sub InitializeVerticalWalls()
        verticalWalls = New Boolean(SQCOUNTX)() {}
        For x As Integer = 0 To SQCOUNTX
            verticalWalls(x) = New Boolean(SQCOUNTY - 1) {}
            For y As Integer = 0 To SQCOUNTY - 1
                verticalWalls(x)(y) = True ' Default to True for walls
            Next
        Next
    End Sub

    Private Sub InitializeHorizontalWalls()
        horizontalWalls = New Boolean(SQCOUNTX - 1)() {}
        For x As Integer = 0 To SQCOUNTX - 1
            horizontalWalls(x) = New Boolean(SQCOUNTY) {}
            For y As Integer = 0 To SQCOUNTY
                horizontalWalls(x)(y) = True ' Default to True for walls
            Next
        Next
    End Sub

    Private Sub InitializePlacedItems()
        placedItems = New String(SQCOUNTX - 1)() {}
        For x As Integer = 0 To SQCOUNTX - 1
            placedItems(x) = New String(SQCOUNTY - 1) {}
            For y As Integer = 0 To SQCOUNTY - 1
                placedItems(x)(y) = Nothing
            Next
        Next
    End Sub

    Private Sub InitializeVerticalWallItems()
        verticalWallItems = New String(SQCOUNTX)() {}
        For x As Integer = 0 To SQCOUNTX
            verticalWallItems(x) = New String(SQCOUNTY - 1) {}
            For y As Integer = 0 To SQCOUNTY - 1
                verticalWallItems(x)(y) = Nothing
            Next
        Next
    End Sub

    Private Sub InitializeHorizontalWallItems()
        horizontalWallItems = New String(SQCOUNTX - 1)() {}
        For x As Integer = 0 To SQCOUNTX - 1
            horizontalWallItems(x) = New String(SQCOUNTY) {}
            For y As Integer = 0 To SQCOUNTY
                horizontalWallItems(x)(y) = Nothing
            Next
        Next
    End Sub

    Private Sub InitializeFloorsUseEdgeStyle()
        floorsUseEdgeStyle = New Boolean(SQCOUNTX - 1)() {}
        For x As Integer = 0 To SQCOUNTX - 1
            floorsUseEdgeStyle(x) = New Boolean(SQCOUNTY - 1) {}
            For y As Integer = 0 To SQCOUNTY - 1
                floorsUseEdgeStyle(x)(y) = False
            Next
        Next
    End Sub

    ' Method to initialize images and animations
    Private Sub InitializeImages()
        Try
            imageDictionary("picBlock1") = Image.FromFile(Path.Combine(ImagesFolder, "Block.png"))
            imageDictionary("picBaddy1") = Image.FromFile(Path.Combine(ImagesFolder, "Baddy 1 anim.gif"))
            imageDictionary("picBaddy2") = Image.FromFile(Path.Combine(ImagesFolder, "Baddy 2 anim.gif"))
            imageDictionary("picBaddy3") = Image.FromFile(Path.Combine(ImagesFolder, "Baddy3.gif"))
            imageDictionary("picBaddy4") = Image.FromFile(Path.Combine(ImagesFolder, "Baddy4.gif"))
            imageDictionary("picBall") = Image.FromFile(Path.Combine(ImagesFolder, "Ball.gif"))
            imageDictionary("picBlockSwitch") = Image.FromFile(Path.Combine(ImagesFolder, "BlockSwitch.png"))
            imageDictionary("picBomb") = Image.FromFile(Path.Combine(ImagesFolder, "Bomb.gif"))
            imageDictionary("picCoin") = Image.FromFile(Path.Combine(ImagesFolder, "Coin.gif"))
            imageDictionary("picFinish") = Image.FromFile(Path.Combine(ImagesFolder, "Finish.gif"))
            imageDictionary("picFlipper1") = Image.FromFile(Path.Combine(ImagesFolder, "Flipper1.gif"))
            imageDictionary("picFlipper2") = Image.FromFile(Path.Combine(ImagesFolder, "Flipper2.gif"))
            imageDictionary("picFlipper3") = Image.FromFile(Path.Combine(ImagesFolder, "Flipper3.gif"))
            imageDictionary("picFlipper4") = Image.FromFile(Path.Combine(ImagesFolder, "Flipper4.gif"))
            imageDictionary("picGate") = Image.FromFile(Path.Combine(ImagesFolder, "Gate.gif"))
            imageDictionary("picKey") = Image.FromFile(Path.Combine(ImagesFolder, "Key.gif"))
            imageDictionary("picLazer1") = Image.FromFile(Path.Combine(ImagesFolder, "Lazer1.gif"))
            imageDictionary("picLazer2") = Image.FromFile(Path.Combine(ImagesFolder, "Lazer2.gif"))
            imageDictionary("picWarp") = Image.FromFile(Path.Combine(ImagesFolder, "Warp.gif"))
            imageDictionary("picWarp2") = Image.FromFile(Path.Combine(ImagesFolder, "Warp2.gif"))
            imageDictionary("picStart") = Image.FromFile(Path.Combine(ImagesFolder, "Guy.gif"))
            imageDictionary("picBlockStopper1") = Image.FromFile(Path.Combine(ImagesFolder, "Debris1.png"))
            ' Add other images similarly...

            ' List of image keys that are animated
            Dim animatedImageKeys As String() = {
                "picBaddy1", "picBaddy2", "picBaddy3", "picBaddy4",
                "picLazer1", "picLazer2", "picFlipper1", "picFlipper2",
                "picFlipper3", "picFlipper4", "picCoin", "picGate",
                "picKey", "picFinish", "picBall", "picBomb",
                "picWarp", "picWarp2", "picStart"
            }

            ' Add images that require animation to the animatedImages list
            For Each imgKey In animatedImageKeys
                If imageDictionary.ContainsKey(imgKey) Then
                    Dim img As Image = imageDictionary(imgKey)
                    animatedImages.Add(img)
                    ImageAnimator.Animate(img, AddressOf OnFrameChanged)
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("Error loading images: " & ex.Message)
        End Try
    End Sub

    ' Event handler for animation timer tick
    Private Sub OnAnimationTick(sender As Object, e As EventArgs)
        For Each img In animatedImages
            ImageAnimator.UpdateFrames(img)
        Next
        picTest.Invalidate()
    End Sub

    ' Event handler for image frame change (required by ImageAnimator)
    Private Sub OnFrameChanged(o As Object, e As EventArgs)
        ' Required but can be left empty
    End Sub

    Private Sub picTest_Paint(sender As Object, e As PaintEventArgs) Handles picTest.Paint
        If Not isRoomDataLoaded Then
            Return
        End If

        ' Fill the entire background with the walls' hatch style and colors
        Using backgroundBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
            e.Graphics.FillRectangle(backgroundBrush, picTest.ClientRectangle)
        End Using

        ' Calculate square sizes based on the current size of picTest
        CalculateSquareSizes(picTest.ClientSize.Width, picTest.ClientSize.Height)

        e.Graphics.SmoothingMode = SmoothingMode.None

        ' Draw the game grid, walls, and items
        DrawGrid(e.Graphics)
    End Sub

    ' Method to calculate square sizes based on control size
    Private Sub CalculateSquareSizes(clientWidth As Integer, clientHeight As Integer)
        SQSizeX = (clientWidth - 2 * INDENT - (SQCOUNTX + 1) * WALL_THICKNESS) \ SQCOUNTX
        SQSizeY = (clientHeight - 2 * INDENT - (SQCOUNTY + 1) * WALL_THICKNESS) \ SQCOUNTY
    End Sub

    ' Method to draw the grid, walls, and items
    Private Sub DrawGrid(g As Graphics)
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT

        ' Draw floor squares
        For x As Integer = 0 To SQCOUNTX - 1
            For y As Integer = 0 To SQCOUNTY - 1
                Dim floorRect As New Rectangle(
                    xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
                    yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
                    SQSizeX,
                    SQSizeY
                )

                If floorsUseEdgeStyle IsNot Nothing AndAlso floorsUseEdgeStyle.Length > x AndAlso
                   floorsUseEdgeStyle(x) IsNot Nothing AndAlso floorsUseEdgeStyle(x).Length > y AndAlso
                   floorsUseEdgeStyle(x)(y) Then

                    ' Draw with wall's hatch style and colors (edge style)
                    Using floorBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
                        g.FillRectangle(floorBrush, floorRect)
                    End Using
                Else
                    ' Draw with floor's hatch style and colors
                    Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
                        g.FillRectangle(floorBrush, floorRect)
                    End Using
                End If
            Next
        Next

        ' Draw wall intersections
        For x As Integer = 0 To SQCOUNTX
            For y As Integer = 0 To SQCOUNTY
                Dim intersectionRect As New Rectangle(
                    xOrigin + x * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2,
                    yOrigin + y * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2,
                    WALL_THICKNESS,
                    WALL_THICKNESS
                )
                Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
                    g.FillRectangle(wallBrush, intersectionRect)
                End Using
            Next
        Next

        ' Draw vertical walls or floor
        For x As Integer = 0 To SQCOUNTX
            For y As Integer = 0 To SQCOUNTY - 1
                Dim wallRect As New Rectangle(
                    xOrigin + x * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2,
                    yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
                    WALL_THICKNESS,
                    SQSizeY
                )

                If x >= 0 AndAlso x < verticalWalls.Length AndAlso
                   verticalWalls(x) IsNot Nothing AndAlso
                   y >= 0 AndAlso y < verticalWalls(x).Length Then

                    If verticalWalls(x)(y) Then
                        ' Draw wall
                        Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
                            g.FillRectangle(wallBrush, wallRect)
                        End Using
                    Else
                        ' Draw floor
                        Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
                            g.FillRectangle(floorBrush, wallRect)
                        End Using
                    End If
                End If
            Next
        Next

        ' Draw horizontal walls or floor
        For x As Integer = 0 To SQCOUNTX - 1
            For y As Integer = 0 To SQCOUNTY
                Dim wallRect As New Rectangle(
                    xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
                    yOrigin + y * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2,
                    SQSizeX,
                    WALL_THICKNESS
                )

                If x >= 0 AndAlso x < horizontalWalls.Length AndAlso
                   horizontalWalls(x) IsNot Nothing AndAlso
                   y >= 0 AndAlso y < horizontalWalls(x).Length Then

                    If horizontalWalls(x)(y) Then
                        ' Draw wall
                        Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
                            g.FillRectangle(wallBrush, wallRect)
                        End Using
                    Else
                        ' Draw floor
                        Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
                            g.FillRectangle(floorBrush, wallRect)
                        End Using
                    End If
                End If
            Next
        Next

        ' Draw items on the floor squares
        DrawItems(g)

        ' Draw items on the walls
        DrawWallItems(g)
    End Sub

    ' Method to draw items on floor squares
    Private Sub DrawItems(g As Graphics)
        If placedItems Is Nothing Then Return

        For x As Integer = 0 To placedItems.Length - 1
            If placedItems(x) IsNot Nothing Then
                For y As Integer = 0 To placedItems(x).Length - 1
                    Dim imageKey As String = placedItems(x)(y)
                    If Not String.IsNullOrEmpty(imageKey) AndAlso imageDictionary.ContainsKey(imageKey) Then
                        Dim img As Image = imageDictionary(imageKey)
                        Dim xPos As Integer = INDENT + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
                        Dim yPos As Integer = INDENT + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
                        Dim destRect As New Rectangle(xPos, yPos, SQSizeX, SQSizeY)

                        ' Draw the item
                        g.DrawImage(img, destRect)
                    End If
                Next
            End If
        Next
    End Sub

    ' Method to draw items on walls
    Private Sub DrawWallItems(g As Graphics)
        ' Draw vertical wall items
        If verticalWallItems IsNot Nothing Then
            For x As Integer = 0 To verticalWallItems.Length - 1
                If verticalWallItems(x) IsNot Nothing Then
                    For y As Integer = 0 To verticalWallItems(x).Length - 1
                        Dim imageKey As String = verticalWallItems(x)(y)
                        If Not String.IsNullOrEmpty(imageKey) AndAlso imageDictionary.ContainsKey(imageKey) Then
                            Dim img As Image = imageDictionary(imageKey)
                            ImageAnimator.UpdateFrames(img)
                            Dim wallX As Integer = INDENT + x * (SQSizeX + WALL_THICKNESS) - (WALL_THICKNESS \ 2)
                            Dim wallY As Integer = INDENT + y * (SQSizeY + WALL_THICKNESS) + (WALL_THICKNESS \ 2)
                            Dim wallWidth As Integer = WALL_THICKNESS
                            Dim wallHeight As Integer = SQSizeY

                            ' Keep original image width, stretch height
                            Dim newWidth As Integer = img.Width
                            Dim newHeight As Integer = wallHeight

                            ' Center horizontally
                            Dim centeredX As Integer = wallX + (wallWidth - newWidth) \ 2

                            Dim drawRect As New Rectangle(centeredX, wallY, newWidth, newHeight)
                            g.DrawImage(img, drawRect)
                        End If
                    Next
                End If
            Next
        End If

        ' Draw horizontal wall items
        If horizontalWallItems IsNot Nothing Then
            For x As Integer = 0 To horizontalWallItems.Length - 1
                If horizontalWallItems(x) IsNot Nothing Then
                    For y As Integer = 0 To horizontalWallItems(x).Length - 1
                        Dim imageKey As String = horizontalWallItems(x)(y)
                        If Not String.IsNullOrEmpty(imageKey) AndAlso imageDictionary.ContainsKey(imageKey) Then
                            Dim img As Image = imageDictionary(imageKey)
                            ImageAnimator.UpdateFrames(img)
                            Dim wallX As Integer = INDENT + x * (SQSizeX + WALL_THICKNESS) + (WALL_THICKNESS \ 2)
                            Dim wallY As Integer = INDENT + y * (SQSizeY + WALL_THICKNESS) - (WALL_THICKNESS \ 2)
                            Dim wallWidth As Integer = SQSizeX
                            Dim wallHeight As Integer = WALL_THICKNESS

                            ' Stretch width, keep original image height
                            Dim newWidth As Integer = wallWidth
                            Dim newHeight As Integer = img.Height

                            ' Center vertically
                            Dim centeredY As Integer = wallY + (wallHeight - newHeight) \ 2

                            Dim drawRect As New Rectangle(wallX, centeredY, newWidth, newHeight)
                            g.DrawImage(img, drawRect)
                        End If
                    Next
                End If
            Next
        End If
    End Sub

    ' Helper method to get the project directory
    Private Function GetProjectDirectory() As String
        Dim dir As DirectoryInfo = New DirectoryInfo(Application.StartupPath)
        For i As Integer = 1 To 4 ' Adjust this number based on your directory structure
            If dir.Parent IsNot Nothing Then
                dir = dir.Parent
            End If
        Next
        Return dir.FullName
    End Function

    ' Ensure that the animation timer is disposed when the form closes
    Private Sub frmGame_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If animationTimer IsNot Nothing Then
            animationTimer.Stop()
            animationTimer.Dispose()
            animationTimer = Nothing
        End If

        If musicPlayer IsNot Nothing Then
            musicPlayer.Stop()
            musicPlayer.Dispose()
            musicPlayer = Nothing
        End If
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class

Please note. frmDesigner andf RoomState are for reference only. They will not be a part of the end result. So please avoid any suggestions that will share code.

Can you see why frmGame is not drawing correctly? It won't draw any items. and the walls seem quite random.