using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ionic.Crc;
using skyscraper5.Skyscraper.IO;

namespace skyscraper5.Dvb.SystemSoftwareUpdate.Model
{
    public class UpdateNotificationTarget
    {
        public UpdateNotificationTarget()
        {
            Compatibilities = new List<Compatibility>();
            Platforms = new List<Platform>();
        }

        public List<Compatibility> Compatibilities { get; private set; }

        public List<Platform> Platforms { get; private set; }

        private void HashDescriptor(BinaryWriter bw, Compatibility compatibility)
        {
            bw.Write(compatibility.DescriptorType);
            if (compatibility.DescriptorType >= 0x03)
            {
                bw.Write(compatibility.PrivateData);
                return;
            }
            else
            {
                bw.Write(compatibility.Model);
                bw.Write(compatibility.SpecifierData);
                bw.Write(compatibility.SpecifierType);
                bw.Write(compatibility.Version);
                foreach (Compatibility compatibilitySubDescriptor in compatibility.SubDescriptors)
                    HashDescriptor(bw, compatibilitySubDescriptor);
            }
        }
        public override int GetHashCode()
        {
            MemoryStream hccs = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(hccs);
            foreach (Compatibility compatibility in Compatibilities)
            {
                HashDescriptor(bw, compatibility);
            }

            hccs.Position = 0;
            
            CRC32 crc32 = new CRC32();
            return crc32.GetCrc32(hccs);
        }
    }
}
