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

namespace QCRPG.TSL.BT.Packets;

public class LeSetupIsoDataPath : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _ConnectionHandle;

		private uint _DataPathDirection;

		private uint _DataPathId;

		private ulong _CodecId;

		private uint _ControllerDelay;

		private uint _CodecCfgLength;

		private List<uint> _CodecCfgList = new List<uint>();

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

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

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

		public ulong CodecId
		{
			get
			{
				return _CodecId;
			}
			set
			{
				_CodecId = value;
			}
		}

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

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

		public List<uint> CodecCfgList => _CodecCfgList;

		public IsoDataPathDirectionEnum GetDataPathDirectionAsIsoDataPathDirectionEnum()
		{
			return IsoDataPathDirectionInfo.ValueToIsoDataPathDirection(_DataPathDirection);
		}

		public void SetDataPathDirectionAsIsoDataPathDirectionEnum(IsoDataPathDirectionEnum enumItem)
		{
			DataPathDirection = IsoDataPathDirectionInfo.IsoDataPathDirectionToValue(enumItem);
		}

		public IsoDataPathIdEnum GetDataPathIdAsIsoDataPathIdEnum()
		{
			return IsoDataPathIdInfo.ValueToIsoDataPathId(_DataPathId);
		}

		public void SetDataPathIdAsIsoDataPathIdEnum(IsoDataPathIdEnum enumItem)
		{
			DataPathId = IsoDataPathIdInfo.IsoDataPathIdToValue(enumItem);
		}
	}

	private const uint _Opcode = 8302u;

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

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

	public uint Opcode => 8302u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	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.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.DataPathDirection = bitBuilder.RemoveBitsAsUint(8);
		UserFields.DataPathId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.CodecId = bitBuilder.RemoveBitsAsUlongLittleEndian(40);
		UserFields.ControllerDelay = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.CodecCfgLength = bitBuilder.RemoveBitsAsUint(8);
		for (int num = UserFields.CodecCfgList.Count - 1; num >= 0; num--)
		{
			UserFields.CodecCfgList[num] = bitBuilder.RemoveBitsAsUint(8);
		}
		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.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.DataPathDirection, 8);
		bitBuilder.AddBitsAsUint(UserFields.DataPathId, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.CodecId, 40);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ControllerDelay, 24);
		bitBuilder.AddBitsAsUint(UserFields.CodecCfgLength, 8);
		for (int num = UserFields.CodecCfgList.Count - 1; num >= 0; num--)
		{
			if (UserFields.CodecCfgList[num] > 255)
			{
				throw new Exception("UserFields.CodecCfgList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.CodecCfgList[num], 8);
		}
		_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");
		}
		if (UserFields.ControllerDelay < 0)
		{
			list.Add("Minimum allowed value for Controller_Delay is 0x0");
		}
		if (UserFields.ControllerDelay > 4000000)
		{
			list.Add("Maximum allowed value for Controller_Delay is 0x3D0900");
		}
		return list;
	}
}
