using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class EnableAmpReceiverReports : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EnableEnum
		{
			Disabled,
			Enabled,
			Reserved,
			InvalidEnableValue
		}

		public static class EnableInfo
		{
			public static EnableEnum ValueToEnable(uint value)
			{
				switch (value)
				{
				case 0u:
					return EnableEnum.Disabled;
				case 1u:
					return EnableEnum.Enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return EnableEnum.Reserved;
					}
					return EnableEnum.InvalidEnableValue;
				}
			}

			public static uint EnableToValue(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disabled => 0u, 
					EnableEnum.Enabled => 1u, 
					EnableEnum.Reserved => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EnableToString(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disabled => "Disabled", 
					EnableEnum.Enabled => "Enabled", 
					EnableEnum.Reserved => "Reserved", 
					_ => "Invalid Enable Value", 
				};
			}

			public static string EnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disabled");
				stringBuilder.AppendLine("0x1 = Enabled");
				stringBuilder.AppendLine("0x2-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		public enum IntervalEnum
		{
			Reserved,
			Number_of_seconds_between_event_reporting,
			InvalidIntervalValue
		}

		public static class IntervalInfo
		{
			public static IntervalEnum ValueToInterval(uint value)
			{
				if (value == 0)
				{
					return IntervalEnum.Reserved;
				}
				if (value >= 1 && value <= 255)
				{
					return IntervalEnum.Number_of_seconds_between_event_reporting;
				}
				return IntervalEnum.InvalidIntervalValue;
			}

			public static uint IntervalToValue(IntervalEnum enumItem)
			{
				return enumItem switch
				{
					IntervalEnum.Reserved => 0u, 
					IntervalEnum.Number_of_seconds_between_event_reporting => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string IntervalToString(IntervalEnum enumItem)
			{
				return enumItem switch
				{
					IntervalEnum.Reserved => "Reserved", 
					IntervalEnum.Number_of_seconds_between_event_reporting => "Number of seconds between event reporting", 
					_ => "Invalid Interval Value", 
				};
			}

			public static string IntervalToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Reserved");
				stringBuilder.AppendLine("0x1-0xFF = Number of seconds between event reporting");
				return stringBuilder.ToString();
			}
		}

		private uint _Enable;

		private uint _Interval;

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

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

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

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

		public IntervalEnum GetIntervalAsIntervalEnum()
		{
			return IntervalInfo.ValueToInterval(_Interval);
		}

		public void SetIntervalAsIntervalEnum(IntervalEnum enumItem)
		{
			Interval = IntervalInfo.IntervalToValue(enumItem);
		}
	}

	private const uint _Opcode = 6151u;

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

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

	public uint Opcode => 6151u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Enable = 0u;
		UserFields.Interval = 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;
		}
		UserFields.Enable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Interval = 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.Enable, 8);
		bitBuilder.AddBitsAsUint(UserFields.Interval, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
