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

namespace QCRPG.TSL.BT.Packets;

public class LeBigCreateSync : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum MseEnum
		{
			Any_NSE,
			Specified_NSE,
			InvalidMseValue
		}

		public static class MseInfo
		{
			public static MseEnum ValueToMse(uint value)
			{
				if (value == 0)
				{
					return MseEnum.Any_NSE;
				}
				if (value >= 1 && value <= 31)
				{
					return MseEnum.Specified_NSE;
				}
				return MseEnum.InvalidMseValue;
			}

			public static uint MseToValue(MseEnum enumItem)
			{
				return enumItem switch
				{
					MseEnum.Any_NSE => 0u, 
					MseEnum.Specified_NSE => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string MseToString(MseEnum enumItem)
			{
				return enumItem switch
				{
					MseEnum.Any_NSE => "The Controller can schedule reception of any number of subevents up to NSE.", 
					MseEnum.Specified_NSE => "Maximum number of subevents that should be used to receive data payloads", 
					_ => "Invalid Mse Value", 
				};
			}

			public static string MseToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The Controller can schedule reception of any number of subevents up to NSE.");
				stringBuilder.AppendLine("0x1-0x1F = Maximum number of subevents that should be used to receive data payloads");
				return stringBuilder.ToString();
			}
		}

		private uint _BigHandle;

		private uint _SyncHandle;

		private uint _Encryption;

		private List<uint> _BroadcastcodeList = new List<uint>(new uint[16]);

		private uint _Mse;

		private uint _BigSyncTimeout = 10u;

		private uint _NumBis = 1u;

		private List<BissSubField> _BissList = new List<BissSubField>();

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

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

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

		public List<uint> BroadcastcodeList => _BroadcastcodeList;

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

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

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

		public List<BissSubField> BissList => _BissList;

		public IsoEncryptionEnum GetEncryptionAsIsoEncryptionEnum()
		{
			return IsoEncryptionInfo.ValueToIsoEncryption(_Encryption);
		}

		public void SetEncryptionAsIsoEncryptionEnum(IsoEncryptionEnum enumItem)
		{
			Encryption = IsoEncryptionInfo.IsoEncryptionToValue(enumItem);
		}

		public MseEnum GetMseAsMseEnum()
		{
			return MseInfo.ValueToMse(_Mse);
		}

		public void SetMseAsMseEnum(MseEnum enumItem)
		{
			Mse = MseInfo.MseToValue(enumItem);
		}
	}

	public class BissSubField
	{
		private uint _Bis = 1u;

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

		public void SetDefaultValues()
		{
			Bis = 1u;
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			Bis = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(Bis, 8);
			return bitBuilder.ToString();
		}

		public List<string> GetWarnings()
		{
			List<string> list = new List<string>();
			if (Bis < 1)
			{
				list.Add("Minimum allowed value for BIS is 0x1");
			}
			if (Bis > 31)
			{
				list.Add("Maximum allowed value for BIS is 0x1F");
			}
			return list;
		}
	}

	private const uint _Opcode = 8299u;

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

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

	public uint Opcode => 8299u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.BigHandle = 0u;
		UserFields.SyncHandle = 0u;
		for (int num = UserFields.BroadcastcodeList.Count - 1; num >= 0; num--)
		{
		}
		UserFields.BigSyncTimeout = 10u;
		UserFields.NumBis = 1u;
		for (int i = 0; i < UserFields.BissList.Count; i++)
		{
			UserFields.BissList[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.BigHandle = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SyncHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Encryption = bitBuilder.RemoveBitsAsUint(8);
		for (int num = UserFields.BroadcastcodeList.Count - 1; num >= 0; num--)
		{
			UserFields.BroadcastcodeList[num] = bitBuilder.RemoveBitsAsUint(8);
		}
		UserFields.Mse = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BigSyncTimeout = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.NumBis = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.BissList.Count; i++)
		{
			UserFields.BissList[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.BigHandle, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SyncHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.Encryption, 8);
		for (int num = UserFields.BroadcastcodeList.Count - 1; num >= 0; num--)
		{
			if (UserFields.BroadcastcodeList[num] > 255)
			{
				throw new Exception("UserFields.BroadcastcodeList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.BroadcastcodeList[num], 8);
		}
		bitBuilder.AddBitsAsUint(UserFields.Mse, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.BigSyncTimeout, 16);
		bitBuilder.AddBitsAsUint(UserFields.NumBis, 8);
		for (int i = 0; i < UserFields.BissList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.BissList[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.BigHandle < 0)
		{
			list.Add("Minimum allowed value for BIG_Handle is 0x0");
		}
		if (UserFields.BigHandle > 239)
		{
			list.Add("Maximum allowed value for BIG_Handle is 0xEF");
		}
		if (UserFields.SyncHandle < 0)
		{
			list.Add("Minimum allowed value for Sync_Handle is 0x0");
		}
		if (UserFields.SyncHandle > 3839)
		{
			list.Add("Maximum allowed value for Sync_Handle is 0xEFF");
		}
		if (UserFields.BigSyncTimeout < 10)
		{
			list.Add("Minimum allowed value for BIG_Sync_Timeout is 0xA");
		}
		if (UserFields.BigSyncTimeout > 16384)
		{
			list.Add("Maximum allowed value for BIG_Sync_Timeout is 0x4000");
		}
		if (UserFields.NumBis < 1)
		{
			list.Add("Minimum allowed value for Num_BIS is 0x1");
		}
		if (UserFields.NumBis > 31)
		{
			list.Add("Maximum allowed value for Num_BIS is 0x1F");
		}
		for (int i = 0; i < UserFields.BissList.Count; i++)
		{
			list.AddRange(UserFields.BissList[i].GetWarnings());
		}
		return list;
	}
}
