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

namespace QCRPG.TSL.BT.Packets;

public class LeCreateCis : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _CisCount = 1u;

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

		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 class ConnectionHandlesSubField
	{
		private uint _CisConnectionHandle;

		private uint _AclConnectionHandle;

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

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

		public void SetDefaultValues()
		{
			CisConnectionHandle = 0u;
			AclConnectionHandle = 0u;
		}

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

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

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

	private const uint _Opcode = 8292u;

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

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

	public uint Opcode => 8292u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.CisCount = 1u;
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			UserFields.ConnectionHandlesList[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.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.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 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.CisCount < 1)
		{
			list.Add("Minimum allowed value for CIS_Count is 0x1");
		}
		if (UserFields.CisCount > 31)
		{
			list.Add("Maximum allowed value for CIS_Count is 0x1F");
		}
		for (int i = 0; i < UserFields.ConnectionHandlesList.Count; i++)
		{
			list.AddRange(UserFields.ConnectionHandlesList[i].GetWarnings());
		}
		return list;
	}
}
