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

namespace QCRPG.TSL.BT.Packets;

public class HciModeChange : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum CurrentModeEnum
		{
			Active_Mode,
			Hold_Mode,
			Sniff_Mode,
			Park_State,
			Reserved,
			InvalidCurrentModeValue
		}

		public static class CurrentModeInfo
		{
			public static CurrentModeEnum ValueToCurrentMode(uint value)
			{
				switch (value)
				{
				case 0u:
					return CurrentModeEnum.Active_Mode;
				case 1u:
					return CurrentModeEnum.Hold_Mode;
				case 2u:
					return CurrentModeEnum.Sniff_Mode;
				case 3u:
					return CurrentModeEnum.Park_State;
				default:
					if (value >= 4 && value <= 255)
					{
						return CurrentModeEnum.Reserved;
					}
					return CurrentModeEnum.InvalidCurrentModeValue;
				}
			}

			public static uint CurrentModeToValue(CurrentModeEnum enumItem)
			{
				return enumItem switch
				{
					CurrentModeEnum.Active_Mode => 0u, 
					CurrentModeEnum.Hold_Mode => 1u, 
					CurrentModeEnum.Sniff_Mode => 2u, 
					CurrentModeEnum.Park_State => 3u, 
					CurrentModeEnum.Reserved => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string CurrentModeToString(CurrentModeEnum enumItem)
			{
				return enumItem switch
				{
					CurrentModeEnum.Active_Mode => "Active_Mode", 
					CurrentModeEnum.Hold_Mode => "Hold_Mode", 
					CurrentModeEnum.Sniff_Mode => "Sniff_Mode", 
					CurrentModeEnum.Park_State => "Park_State", 
					CurrentModeEnum.Reserved => "Reserved for future use", 
					_ => "Invalid CurrentMode Value", 
				};
			}

			public static string CurrentModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Active_Mode");
				stringBuilder.AppendLine("0x1 = Hold_Mode");
				stringBuilder.AppendLine("0x2 = Sniff_Mode");
				stringBuilder.AppendLine("0x3 = Park_State");
				stringBuilder.AppendLine("0x4-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _Status;

		private uint _ConnectionHandle;

		private uint _CurrentMode;

		private uint _Interval;

		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 CurrentMode
		{
			get
			{
				return _CurrentMode;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for CurrentMode is 0xFF");
				}
				_CurrentMode = value;
			}
		}

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

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

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

		public CurrentModeEnum GetCurrentModeAsCurrentModeEnum()
		{
			return CurrentModeInfo.ValueToCurrentMode(_CurrentMode);
		}

		public void SetCurrentModeAsCurrentModeEnum(CurrentModeEnum enumItem)
		{
			CurrentMode = CurrentModeInfo.CurrentModeToValue(enumItem);
		}
	}

	private const uint _EventCode = 20u;

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

	public uint EventCode => 20u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.CurrentMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Interval = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.Status, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.CurrentMode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Interval, 16);
		_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 > 3839)
		{
			list.Add("Maximum allowed value for Connection Handle is 0xEFF");
		}
		return list;
	}
}
