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

namespace QCRPG.TSL.BT.Packets;

public class QcmPhyChanged : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PhyEnum
		{
			BR_EDR,
			QHS,
			InvalidPhyValue
		}

		public static class PhyInfo
		{
			public static PhyEnum ValueToPhy(uint value)
			{
				return value switch
				{
					0u => PhyEnum.BR_EDR, 
					1u => PhyEnum.QHS, 
					_ => PhyEnum.InvalidPhyValue, 
				};
			}

			public static uint PhyToValue(PhyEnum enumItem)
			{
				return enumItem switch
				{
					PhyEnum.BR_EDR => 0u, 
					PhyEnum.QHS => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PhyToString(PhyEnum enumItem)
			{
				return enumItem switch
				{
					PhyEnum.BR_EDR => "Using BR/EDR Phy mode", 
					PhyEnum.QHS => "Using QHS Phy mode", 
					_ => "Invalid Phy Value", 
				};
			}

			public static string PhyToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Using BR/EDR Phy mode");
				stringBuilder.AppendLine("0x1 = Using QHS Phy mode");
				return stringBuilder.ToString();
			}
		}

		public enum SourceEnum
		{
			Local,
			Remote,
			InvalidSourceValue
		}

		public static class SourceInfo
		{
			public static SourceEnum ValueToSource(uint value)
			{
				return value switch
				{
					0u => SourceEnum.Local, 
					1u => SourceEnum.Remote, 
					_ => SourceEnum.InvalidSourceValue, 
				};
			}

			public static uint SourceToValue(SourceEnum enumItem)
			{
				return enumItem switch
				{
					SourceEnum.Local => 0u, 
					SourceEnum.Remote => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SourceToString(SourceEnum enumItem)
			{
				return enumItem switch
				{
					SourceEnum.Local => "Local initialized", 
					SourceEnum.Remote => "Remote initialized", 
					_ => "Invalid Source Value", 
				};
			}

			public static string SourceToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Local initialized");
				stringBuilder.AppendLine("0x1 = Remote initialized");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubeventCode = 81u;

		private const uint _MessageType = 1u;

		private uint _Status;

		private uint _ConnectionHandle;

		private uint _Phy;

		private uint _Source;

		public uint SubeventCode => 81u;

		public uint MessageType => 1u;

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

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

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

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

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

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

		public PhyEnum GetPhyAsPhyEnum()
		{
			return PhyInfo.ValueToPhy(_Phy);
		}

		public void SetPhyAsPhyEnum(PhyEnum enumItem)
		{
			Phy = PhyInfo.PhyToValue(enumItem);
		}

		public SourceEnum GetSourceAsSourceEnum()
		{
			return SourceInfo.ValueToSource(_Source);
		}

		public void SetSourceAsSourceEnum(SourceEnum enumItem)
		{
			Source = SourceInfo.SourceToValue(enumItem);
		}
	}

	private const uint _EventCode = 255u;

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

	public uint EventCode => 255u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
		UserFields.Phy = 0u;
		UserFields.Source = 0u;
	}

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubeventCode)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.MessageType)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Phy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Source = bitBuilder.RemoveBitsAsUint(8);
		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.SubeventCode, 8);
		bitBuilder.AddBitsAsUint(UserFields.MessageType, 8);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.Phy, 8);
		bitBuilder.AddBitsAsUint(UserFields.Source, 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");
		}
		if (UserFields.Phy < 0)
		{
			list.Add("Minimum allowed value for PHY is 0x0");
		}
		if (UserFields.Phy > 1)
		{
			list.Add("Maximum allowed value for PHY is 0x1");
		}
		if (UserFields.Source < 0)
		{
			list.Add("Minimum allowed value for Source is 0x0");
		}
		if (UserFields.Source > 1)
		{
			list.Add("Maximum allowed value for Source is 0x1");
		}
		return list;
	}
}
