using System.IO;
using System.Text;
using SabreTools.Data.Extensions;
using SabreTools.Data.Models.ClrMamePro;
using SabreTools.IO.Extensions;
using SabreTools.Text.ClrMamePro;

#pragma warning disable CA1822 // Mark members as static
namespace SabreTools.Serialization.Writers
{
    public class ClrMamePro : BaseBinaryWriter<MetadataFile>
    {
        #region IByteWriter

        /// <inheritdoc/>
        public override byte[]? SerializeArray(MetadataFile? obj)
            => SerializeArray(obj, false);

        /// <inheritdoc/>
        public byte[]? SerializeArray(MetadataFile? obj, bool quotes)
        {
            using var stream = SerializeStream(obj, quotes);
            if (stream is null)
                return null;

            byte[] bytes = new byte[stream.Length];
            int read = stream.Read(bytes, 0, bytes.Length);
            return bytes;
        }

        #endregion

        #region IFileWriter

        /// <inheritdoc/>
        public override bool SerializeFile(MetadataFile? obj, string? path)
            => SerializeFile(obj, path, true);

        /// <inheritdoc cref="SerializeFile(MetadataFile, string)"/>
        public bool SerializeFile(MetadataFile? obj, string? path, bool quotes)
        {
            if (string.IsNullOrEmpty(path))
                return false;

            using var stream = SerializeStream(obj, quotes);
            if (stream is null)
                return false;

            using var fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);
            stream.BlockCopy(fs);
            fs.Flush();

            return true;
        }

        #endregion

        #region IStreamWriter

        /// <inheritdoc/>
        public override Stream? SerializeStream(MetadataFile? obj)
            => SerializeStream(obj, true);

        /// <inheritdoc cref="SerializeStream(MetadataFile)"/>
        public Stream? SerializeStream(MetadataFile? obj, bool quotes)
        {
            // If the metadata file is null
            if (obj is null)
                return null;

            // Setup the writer and output
            var stream = new MemoryStream();
            var writer = new Writer(stream, Encoding.UTF8) { Quotes = quotes };

            // Write the header, if it exists
            WriteHeader(obj.ClrMamePro, writer);

            // Write out the games, if they exist
            WriteGames(obj.Game, writer);

            // Write out the info, if it exists
            WriteInfo(obj.Info, writer);

            // Return the stream
            stream.SeekIfPossible(0, SeekOrigin.Begin);
            return stream;
        }

        /// <summary>
        /// Write header information to the current writer
        /// </summary>
        /// <param name="header">ClrMamePro representing the header information</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteHeader(Data.Models.ClrMamePro.ClrMamePro? header, Writer writer)
        {
            // If the header information is missing, we can't do anything
            if (header is null)
                return;

            writer.WriteStartElement("clrmamepro");

            writer.WriteOptionalStandalone("name", header.Name);
            writer.WriteOptionalStandalone("description", header.Description);
            writer.WriteOptionalStandalone("rootdir", header.RootDir);
            writer.WriteOptionalStandalone("category", header.Category);
            writer.WriteOptionalStandalone("version", header.Version);
            writer.WriteOptionalStandalone("date", header.Date);
            writer.WriteOptionalStandalone("author", header.Author);
            writer.WriteOptionalStandalone("homepage", header.Homepage);
            writer.WriteOptionalStandalone("url", header.Url);
            writer.WriteOptionalStandalone("comment", header.Comment);
            writer.WriteOptionalStandalone("header", header.Header);
            writer.WriteOptionalStandalone("type", header.Type);
            if (header.ForceMerging != Data.Models.Metadata.MergingFlag.None)
                writer.WriteOptionalStandalone("forcemerging", header.ForceMerging.AsStringValue());
            writer.WriteOptionalStandalone("forcezipping", header.ForceZipping.FromYesNo());
            if (header.ForcePacking != Data.Models.Metadata.PackingFlag.None)
                writer.WriteOptionalStandalone("forcepacking", header.ForcePacking.AsStringValue());

            writer.WriteEndElement(); // clrmamepro
            writer.Flush();
        }

        /// <summary>
        /// Write games information to the current writer
        /// </summary>
        /// <param name="games">Array of GameBase objects representing the games information</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteGames(GameBase[]? games, Writer writer)
        {
            // If the games information is missing, we can't do anything
            if (games is null || games.Length == 0)
                return;

            // Loop through and write out the games
            foreach (var game in games)
            {
                if (game is null)
                    continue;

                WriteGame(game, writer);
                writer.Flush();
            }
        }

        /// <summary>
        /// Write game information to the current writer
        /// </summary>
        /// <param name="game">GameBase object representing the game information</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteGame(GameBase game, Writer writer)
        {
            // If the game information is missing, we can't do anything
            if (game is null)
                return;

            switch (game)
            {
                case Game:
                    writer.WriteStartElement("game");
                    break;
                case Machine:
                    writer.WriteStartElement("machine");
                    break;
                case Resource:
                    writer.WriteStartElement("resource");
                    break;
                case Set:
                    writer.WriteStartElement(name: "set");
                    break;
                default:
                    // TODO: Log invalid values
                    break;
            }

            // Write the standalone values
            writer.WriteRequiredStandalone("name", game.Name, throwOnError: true);
            writer.WriteOptionalStandalone("description", game.Description);
            writer.WriteOptionalStandalone("driverstatus", game.DriverStatus);
            writer.WriteOptionalStandalone("year", game.Year);
            writer.WriteOptionalStandalone("manufacturer", game.Manufacturer);
            writer.WriteOptionalStandalone("category", game.Category);
            writer.WriteOptionalStandalone("cloneof", game.CloneOf);
            writer.WriteOptionalStandalone("romof", game.RomOf);
            writer.WriteOptionalStandalone("sampleof", game.SampleOf);

            // Write the item values
            WriteReleases(game.Release, writer);
            WriteBiosSets(game.BiosSet, writer);
            WriteRoms(game.Rom, writer);
            WriteDisks(game.Disk, writer);
            WriteMedia(game.Media, writer);
            WriteSamples(game.Sample, writer);
            WriteArchives(game.Archive, writer);
            WriteChips(game.Chip, writer);
            WriteVideos(game.Video, writer);
            WriteSound(game.Sound, writer);
            WriteInput(game.Input, writer);
            WriteDipSwitches(game.DipSwitch, writer);
            WriteDriver(game.Driver, writer);

            writer.WriteEndElement(); // game, machine, resource, set
        }

        /// <summary>
        /// Write releases information to the current writer
        /// </summary>
        /// <param name="releases">Array of Release objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteReleases(Release[]? releases, Writer writer)
        {
            // If the array is missing, we can't do anything
            if (releases is null)
                return;

            foreach (var release in releases)
            {
                writer.WriteStartElement("release");
                writer.WriteRequiredAttributeString("name", release.Name, throwOnError: true);
                writer.WriteRequiredAttributeString("region", release.Region, throwOnError: true);
                writer.WriteOptionalAttributeString("language", release.Language);
                writer.WriteOptionalAttributeString("date", release.Date);
                writer.WriteOptionalAttributeString("default", release.Default.FromYesNo());
                writer.WriteEndElement(); // release
            }
        }

        /// <summary>
        /// Write biossets information to the current writer
        /// </summary>
        /// <param name="biossets">Array of BiosSet objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteBiosSets(BiosSet[]? biossets, Writer writer)
        {
            // If the array is missing, we can't do anything
            if (biossets is null)
                return;

            foreach (var biosset in biossets)
            {
                writer.WriteStartElement("biosset");
                writer.WriteRequiredAttributeString("name", biosset.Name, throwOnError: true);
                writer.WriteRequiredAttributeString("description", biosset.Description, throwOnError: true);
                writer.WriteOptionalAttributeString("default", biosset.Default.FromYesNo());
                writer.WriteEndElement(); // release
            }
        }

        /// <summary>
        /// Write roms information to the current writer
        /// </summary>
        /// <param name="roms">Array of Rom objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteRoms(Rom[]? roms, Writer writer)
        {
            // If the array is missing, we can't do anything
            if (roms is null)
                return;

            foreach (var rom in roms)
            {
                writer.WriteStartElement("rom");
                writer.WriteRequiredAttributeString("name", rom.Name, throwOnError: true);
                writer.WriteRequiredAttributeString("size", rom.Size?.ToString(), throwOnError: true);
                writer.WriteOptionalAttributeString("crc16", rom.CRC16);
                writer.WriteOptionalAttributeString("crc", rom.CRC);
                writer.WriteOptionalAttributeString("crc64", rom.CRC64);
                writer.WriteOptionalAttributeString("md2", rom.MD2);
                writer.WriteOptionalAttributeString("md4", rom.MD4);
                writer.WriteOptionalAttributeString("md5", rom.MD5);
                writer.WriteOptionalAttributeString("ripemd128", rom.RIPEMD128);
                writer.WriteOptionalAttributeString("ripemd160", rom.RIPEMD160);
                writer.WriteOptionalAttributeString("sha1", rom.SHA1);
                writer.WriteOptionalAttributeString("sha256", rom.SHA256);
                writer.WriteOptionalAttributeString("sha384", rom.SHA384);
                writer.WriteOptionalAttributeString("sha512", rom.SHA512);
                writer.WriteOptionalAttributeString("spamsum", rom.SpamSum);
                writer.WriteOptionalAttributeString("xxh3_64", rom.xxHash364);
                writer.WriteOptionalAttributeString("xxh3_128", rom.xxHash3128);
                writer.WriteOptionalAttributeString("merge", rom.Merge);
                writer.WriteOptionalAttributeString("status", rom.Status?.AsStringValue());
                writer.WriteOptionalAttributeString("region", rom.Region);
                writer.WriteOptionalAttributeString("flags", rom.Flags);
                writer.WriteOptionalAttributeString("offs", rom.Offs);
                writer.WriteOptionalAttributeString("serial", rom.Serial);
                writer.WriteOptionalAttributeString("header", rom.Header);
                writer.WriteOptionalAttributeString("date", rom.Date);
                writer.WriteOptionalAttributeString("inverted", rom.Inverted.FromYesNo());
                writer.WriteOptionalAttributeString("mia", rom.MIA.FromYesNo());
                writer.WriteEndElement(); // rom
            }
        }

        /// <summary>
        /// Write disks information to the current writer
        /// </summary>
        /// <param name="disks">Array of Disk objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteDisks(Disk[]? disks, Writer writer)
        {
            // If the array is missing, we can't do anything
            if (disks is null)
                return;

            foreach (var disk in disks)
            {
                writer.WriteStartElement("disk");
                writer.WriteRequiredAttributeString("name", disk.Name, throwOnError: true);
                writer.WriteOptionalAttributeString("md5", disk.MD5);
                writer.WriteOptionalAttributeString("sha1", disk.SHA1);
                writer.WriteOptionalAttributeString("merge", disk.Merge);
                writer.WriteOptionalAttributeString("status", disk.Status?.AsStringValue());
                writer.WriteOptionalAttributeString("flags", disk.Flags);
                writer.WriteEndElement(); // disk
            }
        }

        /// <summary>
        /// Write medias information to the current writer
        /// </summary>
        /// <param name="medias">Array of Media objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteMedia(Media[]? medias, Writer writer)
        {
            // If the array is missing, we can't do anything
            if (medias is null)
                return;

            foreach (var media in medias)
            {
                writer.WriteStartElement("media");
                writer.WriteRequiredAttributeString("name", media.Name, throwOnError: true);
                writer.WriteOptionalAttributeString("md5", media.MD5);
                writer.WriteOptionalAttributeString("sha1", media.SHA1);
                writer.WriteOptionalAttributeString("sha256", media.SHA256);
                writer.WriteOptionalAttributeString("spamsum", media.SpamSum);
                writer.WriteEndElement(); // media
            }
        }

        /// <summary>
        /// Write samples information to the current writer
        /// </summary>
        /// <param name="samples">Array of Sample objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteSamples(Sample[]? samples, Writer writer)
        {
            // If the array is missing, we can't do anything
            if (samples is null)
                return;

            foreach (var sample in samples)
            {
                writer.WriteStartElement("sample");
                writer.WriteRequiredAttributeString("name", sample.Name, throwOnError: true);
                writer.WriteEndElement(); // sample
            }
        }

        /// <summary>
        /// Write archives information to the current writer
        /// </summary>
        /// <param name="archives">Array of Archive objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteArchives(Archive[]? archives, Writer writer)
        {
            // If the array is missing, we can't do anything
            if (archives is null)
                return;

            foreach (var archive in archives)
            {
                writer.WriteStartElement("archive");
                writer.WriteRequiredAttributeString("name", archive.Name, throwOnError: true);
                writer.WriteEndElement(); // archive
            }
        }

        /// <summary>
        /// Write chips information to the current writer
        /// </summary>
        /// <param name="chips">Array of Chip objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteChips(Chip[]? chips, Writer writer)
        {
            // If the array is missing, we can't do anything
            if (chips is null)
                return;

            foreach (var chip in chips)
            {
                writer.WriteStartElement("chip");
                writer.WriteRequiredAttributeString("type", chip.Type?.AsStringValue(), throwOnError: true);
                writer.WriteRequiredAttributeString("name", chip.Name, throwOnError: true);
                writer.WriteOptionalAttributeString("flags", chip.Flags);
                writer.WriteOptionalAttributeString("clock", chip.Clock?.ToString());
                writer.WriteEndElement(); // chip
            }
        }

        /// <summary>
        /// Write video information to the current writer
        /// </summary>
        /// <param name="videos">Array of Video objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteVideos(Video[]? videos, Writer writer)
        {
            // If the item is missing, we can't do anything
            if (videos is null)
                return;

            foreach (var video in videos)
            {
                writer.WriteStartElement("video");
                writer.WriteRequiredAttributeString("screen", video.Screen?.AsStringValue(), throwOnError: true);
                writer.WriteRequiredAttributeString("orientation", video.Orientation?.AsStringValue(useSecond: true), throwOnError: true);
                writer.WriteOptionalAttributeString("x", video.X?.ToString());
                writer.WriteOptionalAttributeString("y", video.Y.ToString());
                writer.WriteOptionalAttributeString("aspectx", video.AspectX.ToString());
                writer.WriteOptionalAttributeString("aspecty", video.AspectY.ToString());
                writer.WriteOptionalAttributeString("freq", video.Freq?.ToString("0.000000"));
                writer.WriteEndElement(); // video
            }
        }

        /// <summary>
        /// Write sound information to the current writer
        /// </summary>
        /// <param name="sound">Sound object to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteSound(Sound? sound, Writer writer)
        {
            // If the item is missing, we can't do anything
            if (sound is null)
                return;

            writer.WriteStartElement("sound");
            writer.WriteRequiredAttributeString("channels", sound.Channels?.ToString(), throwOnError: true);
            writer.WriteEndElement(); // sound
        }

        /// <summary>
        /// Write input information to the current writer
        /// </summary>
        /// <param name="input">Input object to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteInput(Input? input, Writer writer)
        {
            // If the item is missing, we can't do anything
            if (input is null)
                return;

            writer.WriteStartElement("input");
            writer.WriteRequiredAttributeString("players", input.Players?.ToString(), throwOnError: true);
            writer.WriteOptionalAttributeString("control", input.Control);
            writer.WriteRequiredAttributeString("buttons", input.Buttons?.ToString(), throwOnError: true);
            writer.WriteOptionalAttributeString("coins", input.Coins?.ToString());
            writer.WriteOptionalAttributeString("tilt", input.Tilt.FromYesNo());
            writer.WriteOptionalAttributeString("service", input.Service.FromYesNo());
            writer.WriteEndElement(); // input
        }

        /// <summary>
        /// Write dipswitches information to the current writer
        /// </summary>
        /// <param name="dipswitches">Array of DipSwitch objects to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteDipSwitches(DipSwitch[]? dipswitches, Writer writer)
        {
            // If the array is missing, we can't do anything
            if (dipswitches is null)
                return;

            foreach (var dipswitch in dipswitches)
            {
                writer.WriteStartElement("dipswitch");
                writer.WriteRequiredAttributeString("name", dipswitch.Name, throwOnError: true);
                foreach (var entry in dipswitch.Entry ?? [])
                {
                    writer.WriteRequiredAttributeString("entry", entry);
                }

                writer.WriteOptionalAttributeString("default", dipswitch.Default.FromYesNo());
                writer.WriteEndElement(); // dipswitch
            }
        }

        /// <summary>
        /// Write driver information to the current writer
        /// </summary>
        /// <param name="driver">Driver object to write</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteDriver(Driver? driver, Writer writer)
        {
            // If the item is missing, we can't do anything
            if (driver is null)
                return;

            writer.WriteStartElement("driver");
            writer.WriteRequiredAttributeString("status", driver.Status?.AsStringValue(), throwOnError: true);
            writer.WriteOptionalAttributeString("color", driver.Color?.AsStringValue()); // TODO: Probably actually required
            writer.WriteOptionalAttributeString("sound", driver.Sound?.AsStringValue()); // TODO: Probably actually required
            writer.WriteOptionalAttributeString("palettesize", driver.PaletteSize);
            writer.WriteOptionalAttributeString("blit", driver.Blit?.AsStringValue());
            writer.WriteEndElement(); // driver
        }

        /// <summary>
        /// Write info information to the current writer
        /// </summary>
        /// <param name="info">ClrMamePro representing the info information</param>
        /// <param name="writer">Writer representing the output</param>
        private static void WriteInfo(Info? info, Writer writer)
        {
            // If the info information is missing, we can't do anything
            if (info?.Source is null)
                return;

            writer.WriteStartElement("info");

            foreach (var source in info.Source)
            {
                writer.WriteOptionalStandalone("source", source);
            }

            writer.WriteEndElement(); // info
            writer.Flush();
        }

        #endregion
    }
}
