using System;
using System.Text;

namespace skyscraper5.Dvb.SystemSoftwareUpdate.Model
{
    public class Platform
    {
        //descriptor 0x02 update descriptor
        public int? UpdateFlag { get; set; }
        public int? UpdateMethod { get; set; }
        public int? UpdatePriority { get; set; }
        
        //descriptor 0x03 ssu location descriptor
        public ushort? DataBroadcastId { get; set; }
        public ushort? AssociationTag { get; set; }
        public byte[] PrivateData { get; set; }

        //descriptor 0x06 target smartcard descriptor
        public string SmartCardNumber { get; set; }
        public uint? SuperCaSystemId { get; set; }

        //descriptor 0x08 target serial number descriptor
        public byte[] SerialData { get; set; }

        //descriptor 0x07 target mac address descriptor
        public string MacAddressMask { get; set; }
        public string[] MacAddressMatches { get; set; }

        //descriptor 0x0b ssu subgroup association descriptor
        public byte[] SubgroupTag { get; set; }

        public override string ToString()
        {
            if (AssociationTag.HasValue)
                return AssociationTag.Value.ToString();
            else if (!string.IsNullOrEmpty(MacAddressMask))
                return MacAddressMask;
            else
                throw new NotImplementedException();
        }

        /// <summary>
        /// Use this convenience function to build an identifying column.
        /// </summary>
        /// <returns>Depends on what descriptors are present</returns>
        public int GetFlagsForSqlKey()
        {
	        int result = 0;
	        if (UpdateFlag.HasValue)
		        result |= 0x02;
	        if (DataBroadcastId.HasValue)
		        result |= 0x04;
	        if (SuperCaSystemId.HasValue)
		        result |= 0x20;
	        if (SerialData != null)
		        result |= 0x80;
	        if (MacAddressMatches != null)
		        result |= 0x40;
	        if (SubgroupTag != null)
		        result |= 0x400;
	        return result;
        }
	}
}