using System;
using System.Collections.Generic;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class FlowSpecificationComplete : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum FlagsEnum
		{
			Reserved_for_future_use,
			InvalidFlagsValue
		}

		public static class FlagsInfo
		{
			public static FlagsEnum ValueToFlags(uint value)
			{
				if (value >= 0 && value <= 255)
				{
					return FlagsEnum.Reserved_for_future_use;
				}
				return FlagsEnum.InvalidFlagsValue;
			}

			public static uint FlagsToValue(FlagsEnum enumItem)
			{
				if (enumItem == FlagsEnum.Reserved_for_future_use)
				{
					return 0u;
				}
				return uint.MaxValue;
			}

			public static string FlagsToString(FlagsEnum enumItem)
			{
				if (enumItem == FlagsEnum.Reserved_for_future_use)
				{
					return "Reserved for future use";
				}
				return "Invalid Flags Value";
			}

			public static string FlagsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		public enum FlowDirectionEnum
		{
			Outgoing_Flow_i_e_traffic_send_over_the_ACL_connection,
			Incoming_Flow_i_e_traffic_received_over_the_ACL_connection,
			Reserved_for_future_use,
			InvalidFlowDirectionValue
		}

		public static class FlowDirectionInfo
		{
			public static FlowDirectionEnum ValueToFlowDirection(uint value)
			{
				switch (value)
				{
				case 0u:
					return FlowDirectionEnum.Outgoing_Flow_i_e_traffic_send_over_the_ACL_connection;
				case 1u:
					return FlowDirectionEnum.Incoming_Flow_i_e_traffic_received_over_the_ACL_connection;
				default:
					if (value >= 2 && value <= 255)
					{
						return FlowDirectionEnum.Reserved_for_future_use;
					}
					return FlowDirectionEnum.InvalidFlowDirectionValue;
				}
			}

			public static uint FlowDirectionToValue(FlowDirectionEnum enumItem)
			{
				return enumItem switch
				{
					FlowDirectionEnum.Outgoing_Flow_i_e_traffic_send_over_the_ACL_connection => 0u, 
					FlowDirectionEnum.Incoming_Flow_i_e_traffic_received_over_the_ACL_connection => 1u, 
					FlowDirectionEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string FlowDirectionToString(FlowDirectionEnum enumItem)
			{
				return enumItem switch
				{
					FlowDirectionEnum.Outgoing_Flow_i_e_traffic_send_over_the_ACL_connection => "Outgoing Flow i.e. traffic send over the ACL connection", 
					FlowDirectionEnum.Incoming_Flow_i_e_traffic_received_over_the_ACL_connection => "Incoming Flow i.e. traffic received over the ACL connection", 
					FlowDirectionEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid FlowDirection Value", 
				};
			}

			public static string FlowDirectionToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Outgoing Flow i.e. traffic send over the ACL connection");
				stringBuilder.AppendLine("0x1 = Incoming Flow i.e. traffic received over the ACL connection");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		public enum ServiceTypeEnum
		{
			No_Traffic,
			Best_Effort,
			Guaranteed,
			Reserved_for_future_use,
			InvalidServiceTypeValue
		}

		public static class ServiceTypeInfo
		{
			public static ServiceTypeEnum ValueToServiceType(uint value)
			{
				switch (value)
				{
				case 0u:
					return ServiceTypeEnum.No_Traffic;
				case 1u:
					return ServiceTypeEnum.Best_Effort;
				case 2u:
					return ServiceTypeEnum.Guaranteed;
				default:
					if (value >= 3 && value <= 255)
					{
						return ServiceTypeEnum.Reserved_for_future_use;
					}
					return ServiceTypeEnum.InvalidServiceTypeValue;
				}
			}

			public static uint ServiceTypeToValue(ServiceTypeEnum enumItem)
			{
				return enumItem switch
				{
					ServiceTypeEnum.No_Traffic => 0u, 
					ServiceTypeEnum.Best_Effort => 1u, 
					ServiceTypeEnum.Guaranteed => 2u, 
					ServiceTypeEnum.Reserved_for_future_use => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ServiceTypeToString(ServiceTypeEnum enumItem)
			{
				return enumItem switch
				{
					ServiceTypeEnum.No_Traffic => "No Traffic", 
					ServiceTypeEnum.Best_Effort => "Best Effort", 
					ServiceTypeEnum.Guaranteed => "Guaranteed", 
					ServiceTypeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid ServiceType Value", 
				};
			}

			public static string ServiceTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No Traffic");
				stringBuilder.AppendLine("0x1 = Best Effort");
				stringBuilder.AppendLine("0x2 = Guaranteed");
				stringBuilder.AppendLine("0x3-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _Status;

		private uint _ConnectionHandle;

		private uint _Flags;

		private uint _FlowDirection;

		private uint _ServiceType;

		private uint _TokenRate;

		private uint _TokenBucketSize;

		private uint _PeakBandwidth;

		private uint _AccessLatency;

		public uint Status
		{
			get
			{
				return _Status;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Status is 0xFF");
				}
				_Status = value;
			}
		}

		public uint ConnectionHandle
		{
			get
			{
				return _ConnectionHandle;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for ConnectionHandle is 0xFFFF");
				}
				_ConnectionHandle = value;
			}
		}

		public uint Flags
		{
			get
			{
				return _Flags;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Flags is 0xFF");
				}
				_Flags = value;
			}
		}

		public uint FlowDirection
		{
			get
			{
				return _FlowDirection;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for FlowDirection is 0xFF");
				}
				_FlowDirection = value;
			}
		}

		public uint ServiceType
		{
			get
			{
				return _ServiceType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for ServiceType is 0xFF");
				}
				_ServiceType = value;
			}
		}

		public uint TokenRate
		{
			get
			{
				return _TokenRate;
			}
			set
			{
				_TokenRate = value;
			}
		}

		public uint TokenBucketSize
		{
			get
			{
				return _TokenBucketSize;
			}
			set
			{
				_TokenBucketSize = value;
			}
		}

		public uint PeakBandwidth
		{
			get
			{
				return _PeakBandwidth;
			}
			set
			{
				_PeakBandwidth = value;
			}
		}

		public uint AccessLatency
		{
			get
			{
				return _AccessLatency;
			}
			set
			{
				_AccessLatency = value;
			}
		}

		public ErrorCodeEnum GetStatusAsErrorCodeEnum()
		{
			return ErrorCodeInfo.ValueToErrorCode(_Status);
		}

		public void SetStatusAsErrorCodeEnum(ErrorCodeEnum enumItem)
		{
			Status = ErrorCodeInfo.ErrorCodeToValue(enumItem);
		}

		public FlagsEnum GetFlagsAsFlagsEnum()
		{
			return FlagsInfo.ValueToFlags(_Flags);
		}

		public void SetFlagsAsFlagsEnum(FlagsEnum enumItem)
		{
			Flags = FlagsInfo.FlagsToValue(enumItem);
		}

		public FlowDirectionEnum GetFlowDirectionAsFlowDirectionEnum()
		{
			return FlowDirectionInfo.ValueToFlowDirection(_FlowDirection);
		}

		public void SetFlowDirectionAsFlowDirectionEnum(FlowDirectionEnum enumItem)
		{
			FlowDirection = FlowDirectionInfo.FlowDirectionToValue(enumItem);
		}

		public ServiceTypeEnum GetServiceTypeAsServiceTypeEnum()
		{
			return ServiceTypeInfo.ValueToServiceType(_ServiceType);
		}

		public void SetServiceTypeAsServiceTypeEnum(ServiceTypeEnum enumItem)
		{
			ServiceType = ServiceTypeInfo.ServiceTypeToValue(enumItem);
		}
	}

	private const uint _EventCode = 33u;

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new FlowSpecificationCompleteInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 33u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Flags = bitBuilder.RemoveBitsAsUint(8);
		UserFields.FlowDirection = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ServiceType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TokenRate = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.TokenBucketSize = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.PeakBandwidth = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.AccessLatency = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.Flags, 8);
		bitBuilder.AddBitsAsUint(UserFields.FlowDirection, 8);
		bitBuilder.AddBitsAsUint(UserFields.ServiceType, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.TokenRate, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.TokenBucketSize, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PeakBandwidth, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.AccessLatency, 32);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.ConnectionHandle < 0)
		{
			list.Add("Minimum allowed value for Connection Handle is 0x0");
		}
		if (UserFields.ConnectionHandle > 3839)
		{
			list.Add("Maximum allowed value for Connection Handle is 0xEFF");
		}
		return list;
	}
}
