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

namespace QCRPG.TSL.BT.Packets;

public class LeRemoveIsoDataPath : PacketBase
{
	public class UserFieldsWrapper
	{
		public class DataPathDirectionEnumCollection
		{
			private AssertBitEnum _InputDataPath;

			private AssertBitEnum _OutputDataPath;

			public AssertBitEnum InputDataPath
			{
				get
				{
					return _InputDataPath;
				}
				set
				{
					_InputDataPath = value;
				}
			}

			public AssertBitEnum OutputDataPath
			{
				get
				{
					return _OutputDataPath;
				}
				set
				{
					_OutputDataPath = value;
				}
			}
		}

		public static class DataPathDirectionInfo
		{
			public static DataPathDirectionEnumCollection ValueToDataPathDirection(uint value)
			{
				DataPathDirectionEnumCollection dataPathDirectionEnumCollection = new DataPathDirectionEnumCollection();
				dataPathDirectionEnumCollection.InputDataPath = AssertBitInfo.ValueToAssertBit(value & 1u);
				dataPathDirectionEnumCollection.OutputDataPath = AssertBitInfo.ValueToAssertBit((value & 2) >> 1);
				return dataPathDirectionEnumCollection;
			}

			public static uint DataPathDirectionToValue(DataPathDirectionEnumCollection enumCollection)
			{
				uint num = 0u;
				num |= AssertBitInfo.AssertBitToValue(enumCollection.InputDataPath);
				return num | (AssertBitInfo.AssertBitToValue(enumCollection.OutputDataPath) << 1);
			}

			public static string DataPathDirectionToString(DataPathDirectionEnumCollection enumCollection)
			{
				string text = "";
				text = text + "Input data path: " + AssertBitInfo.AssertBitToString(enumCollection.InputDataPath) + "\r\n";
				text = text + "Output data path: " + AssertBitInfo.AssertBitToString(enumCollection.OutputDataPath) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string DataPathDirectionToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bit 0: Input data path");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				stringBuilder.AppendLine("Bit 1: Output data path");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle;

		private uint _DataPathDirection;

		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 DataPathDirectionEnumCollection GetDataPathDirectionAsDataPathDirectionEnumCollection()
		{
			return DataPathDirectionInfo.ValueToDataPathDirection(_DataPathDirection);
		}

		public void SetDataPathDirectionAsDataPathDirectionEnumCollection(DataPathDirectionEnumCollection enumCollection)
		{
			DataPathDirection = DataPathDirectionInfo.DataPathDirectionToValue(enumCollection);
		}
	}

	private const uint _Opcode = 8303u;

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

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

	public uint Opcode => 8303u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	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);
		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);
		_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");
		}
		return list;
	}
}
