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

namespace QCRPG.TSL.BT.Packets;

public class CommandCompleteForLeSetCigParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _CommandOpcode = 8290u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _CigId;

		private uint _CisCount;

		private List<ConnectionHandlesSubField> _ConnectionHandlesList = new List<ConnectionHandlesSubField>();

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

		public uint CommandOpcode => 8290u;

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

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

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

		public List<ConnectionHandlesSubField> ConnectionHandlesList => _ConnectionHandlesList;

		public CommandOpcodeEnum GetCommandOpcodeAsCommandOpcodeEnum()
		{
			return CommandOpcodeInfo.ValueToCommandOpcode(8290u);
		}

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

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

	public class ConnectionHandlesSubField
	{
		private uint _ConnectionHandle;

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

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(ConnectionHandle, 16);
			return bitBuilder.ToString();
		}

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

	private const uint _EventCode = 14u;

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

	public uint EventCode => 14u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.NumHciCommandPackets = 1u;
		UserFields.CigId = 0u;
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			UserFields.ConnectionHandlesList[i].SetDefaultValues();
		}
	}

	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.NumHciCommandPackets = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.CommandOpcode)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.CigId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.CisCount = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			UserFields.ConnectionHandlesList[i].FromBitBuilder(ref bitBuilder);
		}
		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.NumHciCommandPackets, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.CommandOpcode, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUint(UserFields.CigId, 8);
		bitBuilder.AddBitsAsUint(UserFields.CisCount, 8);
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.ConnectionHandlesList[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");
		}
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			list.AddRange(UserFields.ConnectionHandlesList[i].GetWarnings());
		}
		return list;
	}
}
