Imports System.Drawing.Drawing2D

Public Class RoomData
    Public Property SQCOUNTX As Integer
    Public Property SQCOUNTY As Integer
    Public Property GridSizeX As Integer
    Public Property GridSizeY As Integer
    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 List(Of String())()
    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

    ' Property to save the useGradient value
    Public Property useGradient As Boolean

    Public Property Triggers As List(Of Trigger)

    ' 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 = CopyPlacedItemData(roomView.verticalWallItems)
        horizontalWallItems = CopyPlacedItemData(roomView.horizontalWallItems)

        ' Save floorsUseEdgeStyle
        floorsUseEdgeStyle = CloneJaggedArray(roomView.floorsUseEdgeStyle)

        ' Save the selected music
        SelectedMusic = roomView.SelectedMusic

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

        ' Save the useGradient value
        useGradient = roomView.useGradient

        ' Save triggers
        Triggers = roomView.TriggersList
    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 List(Of String)()()) As List(Of String())()
        If items Is Nothing Then Return Nothing
        Dim lengthOuter As Integer = items.Length
        Dim copiedItems As New List(Of String())(lengthOuter)
        For x As Integer = 0 To lengthOuter - 1
            If items(x) IsNot Nothing Then
                Dim innerList As New List(Of String)
                For y As Integer = 0 To items(x).Count - 1
                    If items(x)(y) IsNot Nothing Then
                        innerList.Add(items(x)(y))
                    Else
                        innerList.Add(Nothing)
                    End If
                Next
                copiedItems.Add(innerList)
            Else
                copiedItems.Add(Nothing)
            End If
        Next
        Return copiedItems.ToArray()
    End Function

    ' Helper method to copy placed items
    Private Function CopyPlacedItemData(items As PlacedItemData()()) 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 = RestorePlacedItemData(verticalWallItems)
        roomView.horizontalWallItems = RestorePlacedItemData(horizontalWallItems)

        ' Restore floorsUseEdgeStyle
        roomView.floorsUseEdgeStyle = CloneJaggedArray(floorsUseEdgeStyle)

        ' Restore the selected music
        roomView.SelectedMusic = SelectedMusic
        roomView.lblAudio.Text = SelectedMusic

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

        ' Restore the useGradient value
        roomView.useGradient = useGradient

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

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

        ' Restore triggers
        roomView.TriggersList = Triggers
    End Sub

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

    ' Helper method to restore placed items
    Private Function RestorePlacedItemData(data As PlacedItemData()()) As PlacedItemData()()
        If data Is Nothing Then Return Nothing
        Dim lengthOuter As Integer = data.Length
        Dim restoredItems As PlacedItemData()() = New PlacedItemData(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 PlacedItemData(lengthInner - 1) {}
                For y As Integer = 0 To lengthInner - 1
                    If data(x)(y) IsNot Nothing Then
                        restoredItems(x)(y) = New PlacedItemData 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

Value of type 'PlacedItem()()' cannot be converted to 'PlacedItemData()()' because 'PlacedItem()' is not derived from 'PlacedItemData()'.
Value of type 'List(Of String())()' cannot be converted to 'PlacedItem()()' because 'List(Of String())' is not derived from 'PlacedItem()'.
Value of type 'List(Of String())()' cannot be converted to 'PlacedItemData()()' because 'List(Of String())' is not derived from 'PlacedItemData()'.
Value of type 'List(Of String)' cannot be converted to 'List(Of String())'.
Value of type 'List(Of String)' cannot be converted to 'String'.
Value of type 'List(Of String)' cannot be converted to 'String()'.
Value of type 'PlacedItem()()' cannot be converted to 'List(Of String)()()' because 'PlacedItem()' is not derived from 'List(Of String)()'.
Value of type 'PlacedItem()()' cannot be converted to 'PlacedItemData()()' because 'PlacedItem()' is not derived from 'PlacedItemData()'.
Value of type 'PlacedItemData()()' cannot be converted to 'List(Of String())()' because 'PlacedItemData()' is not derived from 'List(Of String())'.
Value of type 'PlacedItemData()()' cannot be converted to 'PlacedItem()()' because 'PlacedItemData()' is not derived from 'PlacedItem()'.
Value of type 'PlacedItemData()()' cannot be converted to 'PlacedItem()()' because 'PlacedItemData()' is not derived from 'PlacedItem()'.
Value of type 'String()()' cannot be converted to 'List(Of String())()' because 'String()' is not derived from 'List(Of String())'.