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

namespace QCRPG.TSL.BT.Packets;

public class QbceQleSetCigParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _Subopcode = 4u;

		private uint _CigId;

		private uint _SduIntervalMToS = 255u;

		private uint _SduIntervalSToM = 255u;

		private uint _Sca;

		private uint _Packing;

		private uint _Framing;

		private uint _TransportLatencyMToS = 5u;

		private uint _TransportLatencySToM = 5u;

		private uint _CisCount;

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

		public uint Subopcode => 4u;

		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 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 TransportLatencyMToS
		{
			get
			{
				return _TransportLatencyMToS;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for TransportLatencyMToS is 0xFFFF");
				}
				_TransportLatencyMToS = value;
			}
		}

		public uint TransportLatencySToM
		{
			get
			{
				return _TransportLatencySToM;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for TransportLatencySToM is 0xFFFF");
				}
				_TransportLatencySToM = 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 _MaxSduSizeMToS;

		private uint _MaxSduSizeSToM;

		private uint _PhyMToS;

		private uint _PhySToM;

		private uint _RtnMToS;

		private uint _RtnSToM;

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

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

		public uint MaxSduSizeSToM
		{
			get
			{
				return _MaxSduSizeSToM;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for MaxSduSizeSToM is 0xFFFF");
				}
				_MaxSduSizeSToM = 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 RtnMToS
		{
			get
			{
				return _RtnMToS;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for RtnMToS is 0xFF");
				}
				_RtnMToS = value;
			}
		}

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

		public void SetDefaultValues()
		{
			CisId = 0u;
			MaxSduSizeMToS = 30u;
			MaxSduSizeSToM = 30u;
			RtnMToS = 2u;
			RtnSToM = 2u;
		}

		public QbceTransmitterPhyPreferenceEnumCollection GetPhyMToSAsQbceTransmitterPhyPreferenceEnumCollection()
		{
			return QbceTransmitterPhyPreferenceInfo.ValueToQbceTransmitterPhyPreference(_PhyMToS);
		}

		public void SetPhyMToSAsQbceTransmitterPhyPreferenceEnumCollection(QbceTransmitterPhyPreferenceEnumCollection enumCollection)
		{
			PhyMToS = QbceTransmitterPhyPreferenceInfo.QbceTransmitterPhyPreferenceToValue(enumCollection);
		}

		public QbceTransmitterPhyPreferenceEnumCollection GetPhySToMAsQbceTransmitterPhyPreferenceEnumCollection()
		{
			return QbceTransmitterPhyPreferenceInfo.ValueToQbceTransmitterPhyPreference(_PhySToM);
		}

		public void SetPhySToMAsQbceTransmitterPhyPreferenceEnumCollection(QbceTransmitterPhyPreferenceEnumCollection enumCollection)
		{
			PhySToM = QbceTransmitterPhyPreferenceInfo.QbceTransmitterPhyPreferenceToValue(enumCollection);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			CisId = bitBuilder.RemoveBitsAsUint(8);
			MaxSduSizeMToS = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			MaxSduSizeSToM = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			PhyMToS = bitBuilder.RemoveBitsAsUint(8);
			PhySToM = bitBuilder.RemoveBitsAsUint(8);
			RtnMToS = bitBuilder.RemoveBitsAsUint(8);
			RtnSToM = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(CisId, 8);
			bitBuilder.AddBitsAsUintLittleEndian(MaxSduSizeMToS, 16);
			bitBuilder.AddBitsAsUintLittleEndian(MaxSduSizeSToM, 16);
			bitBuilder.AddBitsAsUint(PhyMToS, 8);
			bitBuilder.AddBitsAsUint(PhySToM, 8);
			bitBuilder.AddBitsAsUint(RtnMToS, 8);
			bitBuilder.AddBitsAsUint(RtnSToM, 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 (MaxSduSizeMToS < 0)
			{
				list.Add("Minimum allowed value for Max_SDU_Size_M_TO_S is 0x0");
			}
			if (MaxSduSizeMToS > 4095)
			{
				list.Add("Maximum allowed value for Max_SDU_Size_M_TO_S is 0xFFF");
			}
			if (MaxSduSizeSToM < 0)
			{
				list.Add("Minimum allowed value for Max_SDU_Size_S_TO_M is 0x0");
			}
			if (MaxSduSizeSToM > 4095)
			{
				list.Add("Maximum allowed value for Max_SDU_Size_S_TO_M is 0xFFF");
			}
			if (RtnMToS < 0)
			{
				list.Add("Minimum allowed value for RTN_M_TO_S is 0x0");
			}
			if (RtnMToS > 15)
			{
				list.Add("Maximum allowed value for RTN_M_TO_S is 0xF");
			}
			if (RtnSToM < 0)
			{
				list.Add("Minimum allowed value for RTN_S_TO_M is 0x0");
			}
			if (RtnSToM > 15)
			{
				list.Add("Maximum allowed value for RTN_S_TO_M is 0xF");
			}
			return list;
		}
	}

	private const uint _Opcode = 64593u;

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

	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 QbceQleSetCigParametersInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64593u;

	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.TransportLatencyMToS = 5u;
		UserFields.TransportLatencySToM = 5u;
		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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Subopcode)
		{
			return false;
		}
		UserFields.CigId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SduIntervalMToS = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.SduIntervalSToM = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.Sca = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Packing = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Framing = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TransportLatencyMToS = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.TransportLatencySToM = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.Subopcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.CigId, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SduIntervalMToS, 24);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SduIntervalSToM, 24);
		bitBuilder.AddBitsAsUint(UserFields.Sca, 8);
		bitBuilder.AddBitsAsUint(UserFields.Packing, 8);
		bitBuilder.AddBitsAsUint(UserFields.Framing, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.TransportLatencyMToS, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.TransportLatencySToM, 16);
		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.TransportLatencyMToS < 5)
		{
			list.Add("Minimum allowed value for Transport_Latency_M_TO_S is 0x5");
		}
		if (UserFields.TransportLatencyMToS > 4000)
		{
			list.Add("Maximum allowed value for Transport_Latency_M_TO_S is 0xFA0");
		}
		if (UserFields.TransportLatencySToM < 5)
		{
			list.Add("Minimum allowed value for Transport_Latency_S_TO_M is 0x5");
		}
		if (UserFields.TransportLatencySToM > 4000)
		{
			list.Add("Maximum allowed value for Transport_Latency_S_TO_M is 0xFA0");
		}
		if (UserFields.CisCount < 0)
		{
			list.Add("Minimum allowed value for CIS_Count is 0x0");
		}
		if (UserFields.CisCount > 16)
		{
			list.Add("Maximum allowed value for CIS_Count is 0x10");
		}
		for (int i = 0; i < UserFields.CisParametersList.Count; i++)
		{
			list.AddRange(UserFields.CisParametersList[i].GetWarnings());
		}
		return list;
	}
}
