// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename       : Structs.cs
// Author(s)      : Natalia Portillo <claunia@claunia.com>
//
// Component      : Opera filesystem plugin.
//
// --[ License ] --------------------------------------------------------------
//
//     This library is free software; you can redistribute it and/or modify
//     it under the terms of the GNU Lesser General Public License as
//     published by the Free Software Foundation; either version 2.1 of the
//     License, or (at your option) any later version.
//
//     This library is distributed in the hope that it will be useful, but
//     WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//     Lesser General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General Public
//     License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2026 Natalia Portillo
// ****************************************************************************/

using System.Runtime.InteropServices;
using Aaru.CommonTypes.Attributes;
using Aaru.CommonTypes.Interfaces;

namespace Aaru.Filesystems;

public sealed partial class OperaFS
{
#region Nested type: DirectoryEntry

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct DirectoryEntry
    {
        /// <summary>File flags, see <see cref="FileFlags" /></summary>
        public uint flags;
        /// <summary>Unique file identifier</summary>
        public uint id;
        /// <summary>Entry type</summary>
        public uint type;
        /// <summary>Block size</summary>
        public uint block_size;
        /// <summary>Size in bytes</summary>
        public uint byte_count;
        /// <summary>Block count</summary>
        public uint block_count;
        /// <summary>Unknown</summary>
        public uint burst;
        /// <summary>Unknown</summary>
        public uint gap;
        /// <summary>Filename</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_NAME)]
        public byte[] name;
        /// <summary>Last copy</summary>
        public uint last_copy;
    }

#endregion

#region Nested type: DirectoryEntryWithPointers

    sealed class DirectoryEntryWithPointers
    {
        public DirectoryEntry Entry;
        public uint[]         Pointers;
    }

#endregion

#region Nested type: DirectoryHeader

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct DirectoryHeader
    {
        /// <summary>Next block from this directory, -1 if last</summary>
        public int next_block;
        /// <summary>Previous block from this directory, -1 if first</summary>
        public int prev_block;
        /// <summary>Directory flags</summary>
        public uint flags;
        /// <summary>Offset to first free unused byte in the directory</summary>
        public uint first_free;
        /// <summary>Offset to first directory entry</summary>
        public uint first_used;
    }

#endregion

#region Nested type: OperaDirNode

    sealed class OperaDirNode : IDirNode
    {
        internal string[] Contents;
        internal int      Position;

#region IDirNode Members

        /// <inheritdoc />
        public string Path { get; init; }

#endregion
    }

#endregion

#region Nested type: OperaFileNode

    sealed class OperaFileNode : IFileNode
    {
        internal DirectoryEntryWithPointers Dentry;

#region IFileNode Members

        /// <inheritdoc />
        public string Path { get; init; }

        /// <inheritdoc />
        public long Length { get; init; }

        /// <inheritdoc />
        public long Offset { get; set; }

#endregion
    }

#endregion

#region Nested type: SuperBlock

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct SuperBlock
    {
        /// <summary>0x000, Record type, must be 1</summary>
        public byte record_type;
        /// <summary>0x001, 5 bytes, "ZZZZZ"</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
        public byte[] sync_bytes;
        /// <summary>0x006, Record version, must be 1</summary>
        public byte record_version;
        /// <summary>0x007, Volume flags</summary>
        public byte volume_flags;
        /// <summary>0x008, 32 bytes, volume comment</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_NAME)]
        public byte[] volume_comment;
        /// <summary>0x028, 32 bytes, volume label</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_NAME)]
        public byte[] volume_label;
        /// <summary>0x048, Volume ID</summary>
        public uint volume_id;
        /// <summary>0x04C, Block size in bytes</summary>
        public uint block_size;
        /// <summary>0x050, Blocks in volume</summary>
        public uint block_count;
        /// <summary>0x054, Root directory ID</summary>
        public uint root_dirid;
        /// <summary>0x058, Root directory blocks</summary>
        public uint rootdir_blocks;
        /// <summary>0x05C, Root directory block size</summary>
        public uint rootdir_bsize;
        /// <summary>0x060, Last root directory copy</summary>
        public uint last_root_copy;
    }

#endregion
}