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

namespace QCRPG.TSL.BT.Packets;

public class LtPhyTestMode : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EnableEnum
		{
			Disable,
			Enable,
			InvalidEnableValue
		}

		public static class EnableInfo
		{
			public static EnableEnum ValueToEnable(uint value)
			{
				return value switch
				{
					0u => EnableEnum.Disable, 
					1u => EnableEnum.Enable, 
					_ => EnableEnum.InvalidEnableValue, 
				};
			}

			public static uint EnableToValue(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disable => 0u, 
					EnableEnum.Enable => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EnableToString(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disable => "Lower Test Mode disabled", 
					EnableEnum.Enable => "Lower Test Mode enabled", 
					_ => "Invalid Enable Value", 
				};
			}

			public static string EnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Lower Test Mode disabled");
				stringBuilder.AppendLine("0x1 = Lower Test Mode enabled");
				return stringBuilder.ToString();
			}
		}

		public class PhyTestModeEnumCollection
		{
			private AssertBitEnum _SendLlUnknownRspToLlPhyReq;

			private AssertBitEnum _DoNotRespondToLlPhyReq;

			private AssertBitEnum _BlockInitiatingPhyExchange;

			private AssertBitEnum _SendPhyUpdateWithInstantInThePast;

			private AssertBitEnum _ForceLlPhyUpdateIndPhyParams;

			public AssertBitEnum SendLlUnknownRspToLlPhyReq
			{
				get
				{
					return _SendLlUnknownRspToLlPhyReq;
				}
				set
				{
					_SendLlUnknownRspToLlPhyReq = value;
				}
			}

			public AssertBitEnum DoNotRespondToLlPhyReq
			{
				get
				{
					return _DoNotRespondToLlPhyReq;
				}
				set
				{
					_DoNotRespondToLlPhyReq = value;
				}
			}

			public AssertBitEnum BlockInitiatingPhyExchange
			{
				get
				{
					return _BlockInitiatingPhyExchange;
				}
				set
				{
					_BlockInitiatingPhyExchange = value;
				}
			}

			public AssertBitEnum SendPhyUpdateWithInstantInThePast
			{
				get
				{
					return _SendPhyUpdateWithInstantInThePast;
				}
				set
				{
					_SendPhyUpdateWithInstantInThePast = value;
				}
			}

			public AssertBitEnum ForceLlPhyUpdateIndPhyParams
			{
				get
				{
					return _ForceLlPhyUpdateIndPhyParams;
				}
				set
				{
					_ForceLlPhyUpdateIndPhyParams = value;
				}
			}
		}

		public static class PhyTestModeInfo
		{
			public static PhyTestModeEnumCollection ValueToPhyTestMode(uint value)
			{
				PhyTestModeEnumCollection phyTestModeEnumCollection = new PhyTestModeEnumCollection();
				phyTestModeEnumCollection.SendLlUnknownRspToLlPhyReq = AssertBitInfo.ValueToAssertBit(value & 1u);
				phyTestModeEnumCollection.DoNotRespondToLlPhyReq = AssertBitInfo.ValueToAssertBit((value & 2) >> 1);
				phyTestModeEnumCollection.BlockInitiatingPhyExchange = AssertBitInfo.ValueToAssertBit((value & 4) >> 2);
				phyTestModeEnumCollection.SendPhyUpdateWithInstantInThePast = AssertBitInfo.ValueToAssertBit((value & 8) >> 3);
				phyTestModeEnumCollection.ForceLlPhyUpdateIndPhyParams = AssertBitInfo.ValueToAssertBit((value & 0x10) >> 4);
				return phyTestModeEnumCollection;
			}

			public static uint PhyTestModeToValue(PhyTestModeEnumCollection enumCollection)
			{
				uint num = 0u;
				num |= AssertBitInfo.AssertBitToValue(enumCollection.SendLlUnknownRspToLlPhyReq);
				num |= AssertBitInfo.AssertBitToValue(enumCollection.DoNotRespondToLlPhyReq) << 1;
				num |= AssertBitInfo.AssertBitToValue(enumCollection.BlockInitiatingPhyExchange) << 2;
				num |= AssertBitInfo.AssertBitToValue(enumCollection.SendPhyUpdateWithInstantInThePast) << 3;
				return num | (AssertBitInfo.AssertBitToValue(enumCollection.ForceLlPhyUpdateIndPhyParams) << 4);
			}

			public static string PhyTestModeToString(PhyTestModeEnumCollection enumCollection)
			{
				string text = "";
				text = text + "Send LL_UNKNOWN_RSP to LL_PHY_REQ: " + AssertBitInfo.AssertBitToString(enumCollection.SendLlUnknownRspToLlPhyReq) + "\r\n";
				text = text + "Do not respond to LL_PHY_REQ: " + AssertBitInfo.AssertBitToString(enumCollection.DoNotRespondToLlPhyReq) + "\r\n";
				text = text + "Block initiating PHY exchange: " + AssertBitInfo.AssertBitToString(enumCollection.BlockInitiatingPhyExchange) + "\r\n";
				text = text + "Send PHY update with instant in the past: " + AssertBitInfo.AssertBitToString(enumCollection.SendPhyUpdateWithInstantInThePast) + "\r\n";
				text = text + "Force LL_PHY_UPDATE_IND PHY Params: " + AssertBitInfo.AssertBitToString(enumCollection.ForceLlPhyUpdateIndPhyParams) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string PhyTestModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bit 0: Send LL_UNKNOWN_RSP to LL_PHY_REQ");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				stringBuilder.AppendLine("Bit 1: Do not respond to LL_PHY_REQ");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				stringBuilder.AppendLine("Bit 2: Block initiating PHY exchange");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				stringBuilder.AppendLine("Bit 3: Send PHY update with instant in the past");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				stringBuilder.AppendLine("Bit 4: Force LL_PHY_UPDATE_IND PHY Params");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 12u;

		private uint _Enable;

		private uint _PhyTestMode;

		private uint _PhyM2s = 1u;

		private uint _PhyS2m = 1u;

		public uint SubOpcode => 12u;

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

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

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

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

		public EnableEnum GetEnableAsEnableEnum()
		{
			return EnableInfo.ValueToEnable(_Enable);
		}

		public void SetEnableAsEnableEnum(EnableEnum enumItem)
		{
			Enable = EnableInfo.EnableToValue(enumItem);
		}

		public PhyTestModeEnumCollection GetPhyTestModeAsPhyTestModeEnumCollection()
		{
			return PhyTestModeInfo.ValueToPhyTestMode(_PhyTestMode);
		}

		public void SetPhyTestModeAsPhyTestModeEnumCollection(PhyTestModeEnumCollection enumCollection)
		{
			PhyTestMode = PhyTestModeInfo.PhyTestModeToValue(enumCollection);
		}
	}

	private const uint _Opcode = 64536u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLowerTester),
		typeof(CommandCompleteForNop)
	};

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

	public uint Opcode => 64536u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.PhyM2s = 1u;
		UserFields.PhyS2m = 1u;
	}

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubOpcode)
		{
			return false;
		}
		UserFields.Enable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PhyTestMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PhyM2s = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PhyS2m = 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.AddBitsAsUint(UserFields.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Enable, 8);
		bitBuilder.AddBitsAsUint(UserFields.PhyTestMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.PhyM2s, 8);
		bitBuilder.AddBitsAsUint(UserFields.PhyS2m, 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.PhyM2s < 1)
		{
			list.Add("Minimum allowed value for PHY_M2S is 0x1");
		}
		if (UserFields.PhyM2s > 4)
		{
			list.Add("Maximum allowed value for PHY_M2S is 0x4");
		}
		if (UserFields.PhyS2m < 1)
		{
			list.Add("Minimum allowed value for PHY_S2M is 0x1");
		}
		if (UserFields.PhyS2m > 4)
		{
			list.Add("Maximum allowed value for PHY_S2M is 0x4");
		}
		return list;
	}
}
