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

namespace QCRPG.TSL.BT.Packets;

public class LeSetCigParametersTest : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _CigId;

		private uint _SduIntervalMToS = 255u;

		private uint _SduIntervalSToM = 255u;

		private uint _FtMToS = 1u;

		private uint _FtSToM = 1u;

		private uint _IsoInterval = 4u;

		private uint _Sca;

		private uint _Packing;

		private uint _Framing;

		private uint _CisCount;

		private List<CisParametersSubField> _CisParametersList = new List<CisParametersSubField>();

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

		public uint SduIntervalMToS
		{
			get
			{
				return _SduIntervalMToS;
			}
			set
			{
				if (value > 16777215)
				{
					throw new Exception("Maximum possible value for SduIntervalMToS is 0xFFFFFF");
				}
				_SduIntervalMToS = value;
			}
		}

		public uint SduIntervalSToM
		{
			get
			{
				return _SduIntervalSToM;
			}
			set
			{
				if (value > 16777215)
				{
					throw new Exception("Maximum possible value for SduIntervalSToM is 0xFFFFFF");
				}
				_SduIntervalSToM = value;
			}
		}

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

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

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

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

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

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

		public uint CisCount
		{
			get
			{
				return _CisCount;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for CisCount is 0xFF");
				}
				_CisCount = value;
				while (_CisParametersList.Count < _CisCount)
				{
					_CisParametersList.Add(new CisParametersSubField());
				}
				while (_CisParametersList.Count > _CisCount)
				{
					_CisParametersList.RemoveAt(_CisParametersList.Count - 1);
				}
			}
		}

		public List<CisParametersSubField> CisParametersList => _CisParametersList;

		public ClockAccuracyEnum GetScaAsClockAccuracyEnum()
		{
			return ClockAccuracyInfo.ValueToClockAccuracy(_Sca);
		}

		public void SetScaAsClockAccuracyEnum(ClockAccuracyEnum enumItem)
		{
			Sca = ClockAccuracyInfo.ClockAccuracyToValue(enumItem);
		}

		public IsoPackingEnum GetPackingAsIsoPackingEnum()
		{
			return IsoPackingInfo.ValueToIsoPacking(_Packing);
		}

		public void SetPackingAsIsoPackingEnum(IsoPackingEnum enumItem)
		{
			Packing = IsoPackingInfo.IsoPackingToValue(enumItem);
		}

		public IsoFramingEnum GetFramingAsIsoFramingEnum()
		{
			return IsoFramingInfo.ValueToIsoFraming(_Framing);
		}

		public void SetFramingAsIsoFramingEnum(IsoFramingEnum enumItem)
		{
			Framing = IsoFramingInfo.IsoFramingToValue(enumItem);
		}
	}

	public class CisParametersSubField
	{
		private uint _CisId;

		private uint _Nse = 1u;

		private uint _MaxSduMToS;

		private uint _MaxSduSToM;

		private uint _MaxPduMToS;

		private uint _MaxPduSToM;

		private uint _PhyMToS;

		private uint _PhySToM;

		private uint _BnMToS;

		private uint _BnSToM;

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

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

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

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

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

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

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

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

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

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

		public void SetDefaultValues()
		{
			CisId = 0u;
			Nse = 2u;
			MaxSduMToS = 30u;
			MaxSduSToM = 30u;
			MaxPduMToS = 30u;
			MaxPduSToM = 30u;
			BnMToS = 1u;
			BnSToM = 1u;
		}

		public TransmitterPhyPreferenceEnumCollection GetPhyMToSAsTransmitterPhyPreferenceEnumCollection()
		{
			return TransmitterPhyPreferenceInfo.ValueToTransmitterPhyPreference(_PhyMToS);
		}

		public void SetPhyMToSAsTransmitterPhyPreferenceEnumCollection(TransmitterPhyPreferenceEnumCollection enumCollection)
		{
			PhyMToS = TransmitterPhyPreferenceInfo.TransmitterPhyPreferenceToValue(enumCollection);
		}

		public TransmitterPhyPreferenceEnumCollection GetPhySToMAsTransmitterPhyPreferenceEnumCollection()
		{
			return TransmitterPhyPreferenceInfo.ValueToTransmitterPhyPreference(_PhySToM);
		}

		public void SetPhySToMAsTransmitterPhyPreferenceEnumCollection(TransmitterPhyPreferenceEnumCollection enumCollection)
		{
			PhySToM = TransmitterPhyPreferenceInfo.TransmitterPhyPreferenceToValue(enumCollection);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			CisId = bitBuilder.RemoveBitsAsUint(8);
			Nse = bitBuilder.RemoveBitsAsUint(8);
			MaxSduMToS = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			MaxSduSToM = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			MaxPduMToS = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			MaxPduSToM = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			PhyMToS = bitBuilder.RemoveBitsAsUint(8);
			PhySToM = bitBuilder.RemoveBitsAsUint(8);
			BnMToS = bitBuilder.RemoveBitsAsUint(8);
			BnSToM = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(CisId, 8);
			bitBuilder.AddBitsAsUint(Nse, 8);
			bitBuilder.AddBitsAsUintLittleEndian(MaxSduMToS, 16);
			bitBuilder.AddBitsAsUintLittleEndian(MaxSduSToM, 16);
			bitBuilder.AddBitsAsUintLittleEndian(MaxPduMToS, 16);
			bitBuilder.AddBitsAsUintLittleEndian(MaxPduSToM, 16);
			bitBuilder.AddBitsAsUint(PhyMToS, 8);
			bitBuilder.AddBitsAsUint(PhySToM, 8);
			bitBuilder.AddBitsAsUint(BnMToS, 8);
			bitBuilder.AddBitsAsUint(BnSToM, 8);
			return bitBuilder.ToString();
		}

		public List<string> GetWarnings()
		{
			List<string> list = new List<string>();
			if (CisId < 0)
			{
				list.Add("Minimum allowed value for CIS_ID is 0x0");
			}
			if (CisId > 239)
			{
				list.Add("Maximum allowed value for CIS_ID is 0xEF");
			}
			if (Nse < 1)
			{
				list.Add("Minimum allowed value for NSE is 0x1");
			}
			if (Nse > 31)
			{
				list.Add("Maximum allowed value for NSE is 0x1F");
			}
			if (MaxSduMToS < 0)
			{
				list.Add("Minimum allowed value for Max_SDU_M_TO_S is 0x0");
			}
			if (MaxSduMToS > 4095)
			{
				list.Add("Maximum allowed value for Max_SDU_M_TO_S is 0xFFF");
			}
			if (MaxSduSToM < 0)
			{
				list.Add("Minimum allowed value for Max_SDU_S_TO_M is 0x0");
			}
			if (MaxSduSToM > 4095)
			{
				list.Add("Maximum allowed value for Max_SDU_S_TO_M is 0xFFF");
			}
			if (MaxPduMToS < 0)
			{
				list.Add("Minimum allowed value for Max_PDU_M_TO_S is 0x0");
			}
			if (MaxPduMToS > 251)
			{
				list.Add("Maximum allowed value for Max_PDU_M_TO_S is 0xFB");
			}
			if (MaxPduSToM < 0)
			{
				list.Add("Minimum allowed value for Max_PDU_S_TO_M is 0x0");
			}
			if (MaxPduSToM > 251)
			{
				list.Add("Maximum allowed value for Max_PDU_S_TO_M is 0xFB");
			}
			if (BnMToS < 0)
			{
				list.Add("Minimum allowed value for BN_M_TO_S is 0x0");
			}
			if (BnMToS > 15)
			{
				list.Add("Maximum allowed value for BN_M_TO_S is 0xF");
			}
			if (BnSToM < 0)
			{
				list.Add("Minimum allowed value for BN_S_TO_M is 0x0");
			}
			if (BnSToM > 15)
			{
				list.Add("Maximum allowed value for BN_S_TO_M is 0xF");
			}
			return list;
		}
	}

	private const uint _Opcode = 8291u;

	private Type[] expectedResponseTypes = new Type[1] { typeof(CommandCompleteForLeSetCigParametersTest) };

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Command;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public uint Opcode => 8291u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.CigId = 0u;
		UserFields.SduIntervalMToS = 10000u;
		UserFields.SduIntervalSToM = 10000u;
		UserFields.FtMToS = 1u;
		UserFields.FtSToM = 1u;
		UserFields.IsoInterval = 8u;
		UserFields.CisCount = 1u;
		for (int i = 0; i < UserFields.CisParametersList.Count; i++)
		{
			UserFields.CisParametersList[i].SetDefaultValues();
		}
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != Opcode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.CigId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SduIntervalMToS = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.SduIntervalSToM = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.FtMToS = bitBuilder.RemoveBitsAsUint(8);
		UserFields.FtSToM = bitBuilder.RemoveBitsAsUint(8);
		UserFields.IsoInterval = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Sca = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Packing = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Framing = bitBuilder.RemoveBitsAsUint(8);
		UserFields.CisCount = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.CisParametersList.Count; i++)
		{
			UserFields.CisParametersList[i].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.CigId, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SduIntervalMToS, 24);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SduIntervalSToM, 24);
		bitBuilder.AddBitsAsUint(UserFields.FtMToS, 8);
		bitBuilder.AddBitsAsUint(UserFields.FtSToM, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.IsoInterval, 16);
		bitBuilder.AddBitsAsUint(UserFields.Sca, 8);
		bitBuilder.AddBitsAsUint(UserFields.Packing, 8);
		bitBuilder.AddBitsAsUint(UserFields.Framing, 8);
		bitBuilder.AddBitsAsUint(UserFields.CisCount, 8);
		for (int i = 0; i < UserFields.CisParametersList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.CisParametersList[i].ToBitString());
		}
		_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.CigId < 0)
		{
			list.Add("Minimum allowed value for CIG_ID is 0x0");
		}
		if (UserFields.CigId > 239)
		{
			list.Add("Maximum allowed value for CIG_ID is 0xEF");
		}
		if (UserFields.SduIntervalMToS < 255)
		{
			list.Add("Minimum allowed value for SDU_Interval_M_TO_S is 0xFF");
		}
		if (UserFields.SduIntervalMToS > 1048575)
		{
			list.Add("Maximum allowed value for SDU_Interval_M_TO_S is 0xFFFFF");
		}
		if (UserFields.SduIntervalSToM < 255)
		{
			list.Add("Minimum allowed value for SDU_Interval_S_TO_M is 0xFF");
		}
		if (UserFields.SduIntervalSToM > 1048575)
		{
			list.Add("Maximum allowed value for SDU_Interval_S_TO_M is 0xFFFFF");
		}
		if (UserFields.FtMToS < 1)
		{
			list.Add("Minimum allowed value for FT_M_TO_S is 0x1");
		}
		if (UserFields.FtMToS > 255)
		{
			list.Add("Maximum allowed value for FT_M_TO_S is 0xFF");
		}
		if (UserFields.FtSToM < 1)
		{
			list.Add("Minimum allowed value for FT_S_TO_M is 0x1");
		}
		if (UserFields.FtSToM > 255)
		{
			list.Add("Maximum allowed value for FT_S_TO_M is 0xFF");
		}
		if (UserFields.IsoInterval < 4)
		{
			list.Add("Minimum allowed value for ISO_Interval is 0x4");
		}
		if (UserFields.IsoInterval > 3200)
		{
			list.Add("Maximum allowed value for ISO_Interval is 0xC80");
		}
		if (UserFields.CisCount < 0)
		{
			list.Add("Minimum allowed value for CIS_Count is 0x0");
		}
		if (UserFields.CisCount > 31)
		{
			list.Add("Maximum allowed value for CIS_Count is 0x1F");
		}
		for (int i = 0; i < UserFields.CisParametersList.Count; i++)
		{
			list.AddRange(UserFields.CisParametersList[i].GetWarnings());
		}
		return list;
	}
}
