using System.Text;
using SabreTools.Data.Models.Quantum;
using SabreTools.Text.Extensions;

namespace SabreTools.Wrappers
{
    public partial class Quantum : IPrintable
    {
#if NETCOREAPP
        /// <inheritdoc/>
        public string ExportJSON(bool recursive) => System.Text.Json.JsonSerializer.Serialize(Model, _jsonSerializerOptions);
#else
        /// <inheritdoc/>
        public string ExportJSON(bool recursive) => Newtonsoft.Json.JsonConvert.SerializeObject(Model, _jsonSerializerOptions);
#endif

        /// <inheritdoc/>
        public void PrintInformation(StringBuilder builder, bool recursive)
        {
            builder.AppendLine("Quantum Information:");
            builder.AppendLine("-------------------------");
            builder.AppendLine();

            Print(builder, Model.Header);
            Print(builder, Model.FileList);
            builder.AppendLine(Model.CompressedDataOffset, "  Compressed data offset");
            builder.AppendLine();
        }

        private static void Print(StringBuilder builder, Header header)
        {
            builder.AppendLine("  Header Information:");
            builder.AppendLine("  -------------------------");
            builder.AppendLine(header.Signature, "  Signature");
            builder.AppendLine(header.MajorVersion, "  Major version");
            builder.AppendLine(header.MinorVersion, "  Minor version");
            builder.AppendLine(header.FileCount, "  File count");
            builder.AppendLine(header.TableSize, "  Table size");
            builder.AppendLine(header.CompressionFlags, "  Compression flags");
            builder.AppendLine();
        }

        private static void Print(StringBuilder builder, FileDescriptor[] entries)
        {
            builder.AppendLine("  File List Information:");
            builder.AppendLine("  -------------------------");
            if (entries.Length == 0)
            {
                builder.AppendLine("  No file list items");
                builder.AppendLine();
                return;
            }

            for (int i = 0; i < entries.Length; i++)
            {
                var fileDescriptor = entries[i];

                builder.AppendLine($"  File Descriptor {i}");
                builder.AppendLine(fileDescriptor.FileNameSize, "    File name size");
                builder.AppendLine(fileDescriptor.FileName, "    File name");
                builder.AppendLine(fileDescriptor.CommentFieldSize, "    Comment field size");
                builder.AppendLine(fileDescriptor.CommentField, "    Comment field");
                builder.AppendLine(fileDescriptor.ExpandedFileSize, "    Expanded file size");
                builder.AppendLine(fileDescriptor.FileTime, "    File time");
                builder.AppendLine(fileDescriptor.FileDate, "    File date");
                if (fileDescriptor.Unknown is not null)
                    builder.AppendLine(fileDescriptor.Unknown.Value, "    Unknown (Checksum?)");
            }

            builder.AppendLine();
        }
    }
}
