using System;
using SabreTools.Data.Models.OpenMSX;

namespace SabreTools.Serialization.CrossModel
{
    public partial class OpenMSX : BaseMetadataSerializer<SoftwareDb>
    {
        /// <inheritdoc/>
        public override SoftwareDb? Deserialize(Data.Models.Metadata.MetadataFile? obj)
        {
            if (obj is null)
                return null;

            var header = obj.Header;
            var softwareDb = header is not null ? ConvertHeaderFromInternalModel(header) : new SoftwareDb();

            var machines = obj.Machine;
            if (machines is not null && machines.Length > 0)
                softwareDb.Software = Array.ConvertAll(machines, ConvertMachineFromInternalModel);

            return softwareDb;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Header"/> to <see cref="SoftwareDb"/>
        /// </summary>
        private static SoftwareDb ConvertHeaderFromInternalModel(Data.Models.Metadata.Header item)
        {
            var softwareDb = new SoftwareDb
            {
                Timestamp = item.Timestamp,
            };
            return softwareDb;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Machine"/> to <see cref="Software"/>
        /// </summary>
        private static Software ConvertMachineFromInternalModel(Data.Models.Metadata.Machine item)
        {
            var game = new Software
            {
                Title = item.Name,
                GenMSXID = item.GenMSXID,
                System = item.System,
                Company = item.Company,
                Year = item.Year,
                Country = item.Country,
            };

            var dumps = item.Dump;
            if (dumps is not null && dumps.Length > 0)
                game.Dump = Array.ConvertAll(dumps, ConvertFromInternalModel);

            return game;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Dump"/> to <see cref="Dump"/>
        /// </summary>
        private static Dump ConvertFromInternalModel(Data.Models.Metadata.Dump item)
        {
            var dump = new Dump();

            var original = item.Original;
            if (original is not null)
                dump.Original = ConvertFromInternalModel(original);

            dump.Boot = item.Boot;

            var rom = item.Rom;
            if (rom is not null)
                dump.Rom = ConvertRomFromInternalModel(rom);

            var megaRom = item.MegaRom;
            if (megaRom is not null)
                dump.Rom = ConvertMegaRomFromInternalModel(megaRom);

            var sccPlusCart = item.SCCPlusCart;
            if (sccPlusCart is not null)
                dump.Rom = ConvertSCCPlusCartFromInternalModel(sccPlusCart);

            return dump;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Rom"/> to <see cref="MegaRom"/>
        /// </summary>
        private static MegaRom ConvertMegaRomFromInternalModel(Data.Models.Metadata.Rom item)
        {
            var megaRom = new MegaRom
            {
                Start = item.Start,
                Type = item.OpenMSXType,
                Hash = item.SHA1,
                Remark = item.Remark,
            };
            return megaRom;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Original"/> to <see cref="Original"/>
        /// </summary>
        private static Original ConvertFromInternalModel(Data.Models.Metadata.Original item)
        {
            var original = new Original
            {
                Value = item.Value,
                Content = item.Content,
            };
            return original;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Rom"/> to <see cref="Rom"/>
        /// </summary>
        private static Rom ConvertRomFromInternalModel(Data.Models.Metadata.Rom item)
        {
            var rom = new Rom
            {
                Start = item.Start,
                Type = item.OpenMSXType,
                Hash = item.SHA1,
                Remark = item.Remark,
            };
            return rom;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Rom"/> to <see cref="SCCPlusCart"/>
        /// </summary>
        private static SCCPlusCart ConvertSCCPlusCartFromInternalModel(Data.Models.Metadata.Rom item)
        {
            var sccPlusCart = new SCCPlusCart
            {
                Start = item.Start,
                Type = item.OpenMSXType,
                Hash = item.SHA1,
                Remark = item.Remark,
            };
            return sccPlusCart;
        }
    }
}
