using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Mhp.Descriptors;
using skyscraper5.Mhp.Descriptors.InteractionTransportSelectors;

namespace skyscraper5.Mhp.Si.Model
{
    public class AitApplication
    {
        public ApplicationIdentifier ApplicationIdentifier { get; }
        public byte ApplicationControlCode { get; }

        //Transport Protocol Descriptors
        public List<TransportProtocolDescriptor> TransportProtocols { get; }

        //Application Descriptor
        public ApplicationDescriptor.ApplicationProfileEncoding[] ApplicationProfiles { get; set; }
        public byte? ApplicationPriority { get; set; }
        public bool? ServiceBoundFlag { get; set; }
        public byte[] TransportProtocolLabel { get; set; }
        public int? Visibility { get; set; }

        //Application Name Descriptor
        public ReadOnlyDictionary<string, string> ApplicationName { get; set; }

        //Simple Application Location Descriptor
        public string InitialPath { get; set; }

        //Simple Application Boundary Descriptor
        public string[] BoundaryExtensions { get; set; }

        //Application Usage Descriptor
        public byte? ApplicationUsageCode { get; set; }

        //0x03 dvb-j application descriptor
        public string[] JvmArguments { get; set; }

        //0x04 dvb-j application location descriptor
        public string JvmBaseDirectory { get; set; }
        public string JvmClassPathExtension { get; set; }
        public string JvmInitialClass { get; set; }

        //0x10 application storage descriptor
        public uint? Version { get; set; }
        public bool? IsLaunchableFromOlderVersion { get; set; }
        public bool? LaunchableCompletelyFromCache { get; set; }
        public bool? NotLaunchableFromBroadcast { get; set; }
        public byte? ApplicationStoragePriority { get; set; }
        public byte? StorageProperty { get; set; }

        //0x05 external application authorization descriptor
        public ExternalApplicationAuthorisationDescriptor.ExternalAuthorization[] ExternalAuthorizations { get; set; }

        //0x66 abstract service descriptor
        public bool? AutoSelect { get; set; }
        public uint? ServiceId { get; set; }
        public string ServiceName { get; set; }

        //0x68 unbound application descriptor
        public uint? VersionNumber { get; set; }

        //0x69 ocap application storage descriptor
        public byte? LaunchOrder { get; set; }
        public ushort? StoragePriority { get; set; }

        //0x72 connection requirement descriptor
        public bool? IpConnectionRequired { get; set; }

        public AitApplication(ApplicationIdentifier applicationIdentifier, byte applicationControlCode)
        {
            ApplicationIdentifier = applicationIdentifier;
            ApplicationControlCode = applicationControlCode;
            TransportProtocols = new List<TransportProtocolDescriptor>();
        }

        public string TryGetName()
        {
            if (ApplicationName != null)
            {
                foreach (KeyValuePair<string, string> keyValuePair in ApplicationName)
                {
                    return keyValuePair.Value;
                }
            }

            return string.Format("{0} / {1}", ApplicationIdentifier.OrganisationId, ApplicationIdentifier.ApplicationId);
        }

        public string ConcatJvmArguments()
        {
	        if (JvmArguments == null)
		        return null;

	        if (JvmArguments.Length == 0)
		        return null;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < JvmArguments.Length; i++)
            {
	            sb.Append(JvmArguments[i]);
	            sb.Append(" ");
            }
            return sb.ToString();
        }
    }
}
