using System;
using SabreTools.Data.Models.SoftwareList;

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

            var header = obj.Header;
            var metadataFile = header is not null ? ConvertHeaderFromInternalModel(header) : new Data.Models.SoftwareList.SoftwareList();

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

            return metadataFile;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Header"/> to <see cref="Models.SoftwareList.SoftwareList"/>
        /// </summary>
        private static Data.Models.SoftwareList.SoftwareList ConvertHeaderFromInternalModel(Data.Models.Metadata.Header item)
        {
            var softwareList = new Data.Models.SoftwareList.SoftwareList
            {
                Name = item.Name,
                Description = item.Description,
                Notes = item.Notes,
            };
            return softwareList;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Machine"/> to <see cref="SSoftware"/>
        /// </summary>
        private static Software ConvertMachineFromInternalModel(Data.Models.Metadata.Machine item)
        {
            var software = new Software
            {
                Name = item.Name,
                CloneOf = item.CloneOf,
                Supported = item.Supported,
                Description = item.Description,
                Year = item.Year,
                Publisher = item.Publisher,
                Notes = item.Notes,
            };

            var infos = item.Info;
            if (infos is not null && infos.Length > 0)
                software.Info = Array.ConvertAll(infos, ConvertFromInternalModel);

            var sharedFeats = item.SharedFeat;
            if (sharedFeats is not null && sharedFeats.Length > 0)
                software.SharedFeat = Array.ConvertAll(sharedFeats, ConvertFromInternalModel);

            var parts = item.Part;
            if (parts is not null && parts.Length > 0)
                software.Part = Array.ConvertAll(parts, ConvertFromInternalModel);

            return software;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.DataArea"/> to <see cref="SDataArea"/>
        /// </summary>
        private static DataArea ConvertFromInternalModel(Data.Models.Metadata.DataArea item)
        {
            var dataArea = new DataArea
            {
                Name = item.Name,
                Size = item.Size,
                Width = item.Width,
                Endianness = item.Endianness,
            };

            var roms = item.Rom;
            if (roms is not null && roms.Length > 0)
                dataArea.Rom = Array.ConvertAll(roms, ConvertFromInternalModel);

            return dataArea;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.DipSwitch"/> to <see cref="SDipSwitch"/>
        /// </summary>
        private static DipSwitch ConvertFromInternalModel(Data.Models.Metadata.DipSwitch item)
        {
            var dipSwitch = new DipSwitch
            {
                Name = item.Name,
                Tag = item.Tag,
                Mask = item.Mask,
            };

            var dipValues = item.DipValue;
            if (dipValues is not null && dipValues.Length > 0)
                dipSwitch.DipValue = Array.ConvertAll(dipValues, ConvertFromInternalModel);

            return dipSwitch;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.DipValue"/> to <see cref="SDipValue"/>
        /// </summary>
        private static DipValue ConvertFromInternalModel(Data.Models.Metadata.DipValue item)
        {
            var dipValue = new DipValue
            {
                Name = item.Name,
                Value = item.Value,
                Default = item.Default,
            };
            return dipValue;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Disk"/> to <see cref="SDisk"/>
        /// </summary>
        private static Disk ConvertFromInternalModel(Data.Models.Metadata.Disk item)
        {
            var disk = new Disk
            {
                Name = item.Name,
                MD5 = item.MD5,
                SHA1 = item.SHA1,
                Status = item.Status,
                Writeable = item.Writable,
            };
            return disk;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.DiskArea"/> to <see cref="SDiskArea"/>
        /// </summary>
        private static DiskArea ConvertFromInternalModel(Data.Models.Metadata.DiskArea item)
        {
            var diskArea = new DiskArea
            {
                Name = item.Name,
            };

            var disks = item.Disk;
            if (disks is not null && disks.Length > 0)
                diskArea.Disk = Array.ConvertAll(disks, ConvertFromInternalModel);

            return diskArea;
        }

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

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

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Part"/> to <see cref="SPart"/>
        /// </summary>
        private static Part ConvertFromInternalModel(Data.Models.Metadata.Part item)
        {
            var part = new Part
            {
                Name = item.Name,
                Interface = item.Interface,
            };

            var features = item.Feature;
            if (features is not null && features.Length > 0)
                part.Feature = Array.ConvertAll(features, ConvertFromInternalModel);

            var dataAreas = item.DataArea;
            if (dataAreas is not null && dataAreas.Length > 0)
                part.DataArea = Array.ConvertAll(dataAreas, ConvertFromInternalModel);

            var diskAreas = item.DiskArea;
            if (diskAreas is not null && diskAreas.Length > 0)
                part.DiskArea = Array.ConvertAll(diskAreas, ConvertFromInternalModel);

            var dipSwitches = item.DipSwitch;
            if (dipSwitches is not null && dipSwitches.Length > 0)
                part.DipSwitch = Array.ConvertAll(dipSwitches, ConvertFromInternalModel);

            return part;
        }

        /// <summary>
        /// Convert from <see cref="Models.Metadata.Rom"/> to <see cref="SRom"/>
        /// </summary>
        private static Rom ConvertFromInternalModel(Data.Models.Metadata.Rom item)
        {
            var rom = new Rom
            {
                Name = item.Name,
                Size = item.Size,
                Length = item.Length,
                CRC = item.CRC32,
                SHA1 = item.SHA1,
                Offset = item.Offset,
                Value = item.Value,
                Status = item.Status,
                LoadFlag = item.LoadFlag,
            };
            return rom;
        }

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