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

namespace QCRPG.TSL.FMHCI.Packets;

public class EnableNotchControl : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum NotchEnableEnum
		{
			DISABLED,
			ENABLE_LOW_PASS_NOTCH_FILTER,
			ENABLE_HIGH_PASS_NOTCH_FITLER,
			InvalidNotchEnableValue
		}

		public static class NotchEnableInfo
		{
			public static NotchEnableEnum ValueToNotchEnable(uint value)
			{
				return value switch
				{
					0u => NotchEnableEnum.DISABLED, 
					1u => NotchEnableEnum.ENABLE_LOW_PASS_NOTCH_FILTER, 
					2u => NotchEnableEnum.ENABLE_HIGH_PASS_NOTCH_FITLER, 
					_ => NotchEnableEnum.InvalidNotchEnableValue, 
				};
			}

			public static uint NotchEnableToValue(NotchEnableEnum enumItem)
			{
				return enumItem switch
				{
					NotchEnableEnum.DISABLED => 0u, 
					NotchEnableEnum.ENABLE_LOW_PASS_NOTCH_FILTER => 1u, 
					NotchEnableEnum.ENABLE_HIGH_PASS_NOTCH_FITLER => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string NotchEnableToString(NotchEnableEnum enumItem)
			{
				return enumItem switch
				{
					NotchEnableEnum.DISABLED => "DISABLED", 
					NotchEnableEnum.ENABLE_LOW_PASS_NOTCH_FILTER => "T", 
					NotchEnableEnum.ENABLE_HIGH_PASS_NOTCH_FITLER => "ENABLE_HIGH_PASS_NOTCH_FITLER", 
					_ => "Invalid NotchEnable Value", 
				};
			}

			public static string NotchEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = DISABLED");
				stringBuilder.AppendLine("0x1 = T");
				stringBuilder.AppendLine("0x2 = ENABLE_HIGH_PASS_NOTCH_FITLER");
				return stringBuilder.ToString();
			}
		}

		private uint _NotchEnable;

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

		public NotchEnableEnum GetNotchEnableAsNotchEnableEnum()
		{
			return NotchEnableInfo.ValueToNotchEnable(_NotchEnable);
		}

		public void SetNotchEnableAsNotchEnableEnum(NotchEnableEnum enumItem)
		{
			NotchEnable = NotchEnableInfo.NotchEnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 19477u;

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

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

	public uint Opcode => 19477u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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.NotchEnable = 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.NotchEnable, 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.NotchEnable > 2)
		{
			list.Add("Maximum allowed value for Notch Enable is 0x2");
		}
		return list;
	}
}
