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

namespace QCRPG.TSL.FMHCI.Packets;

public class SetInternalToneGenerator : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ToneEnum
		{
			NO_TONE,
			TONE_1KHZ_LR_EQUAL_0DBFS,
			TONE_1KHZ_L_ONLY_0DBFS,
			TONE_1KHZ_R_ONLY_0DBFS,
			TONE_1KHZ_LR_EQUAL_MINUS18DBFS,
			TONE_15KHZ_LR_EQUAL_MINUS18DBFS,
			TONE_1KHZ_LR_EQUAL_MINUS24DBFS,
			TONE_15KHZ_LR_EQUAL_MINUS24DBFS,
			InvalidToneValue
		}

		public static class ToneInfo
		{
			public static ToneEnum ValueToTone(uint value)
			{
				return value switch
				{
					0u => ToneEnum.NO_TONE, 
					1u => ToneEnum.TONE_1KHZ_LR_EQUAL_0DBFS, 
					2u => ToneEnum.TONE_1KHZ_L_ONLY_0DBFS, 
					3u => ToneEnum.TONE_1KHZ_R_ONLY_0DBFS, 
					4u => ToneEnum.TONE_1KHZ_LR_EQUAL_MINUS18DBFS, 
					5u => ToneEnum.TONE_15KHZ_LR_EQUAL_MINUS18DBFS, 
					6u => ToneEnum.TONE_1KHZ_LR_EQUAL_MINUS24DBFS, 
					7u => ToneEnum.TONE_15KHZ_LR_EQUAL_MINUS24DBFS, 
					_ => ToneEnum.InvalidToneValue, 
				};
			}

			public static uint ToneToValue(ToneEnum enumItem)
			{
				return enumItem switch
				{
					ToneEnum.NO_TONE => 0u, 
					ToneEnum.TONE_1KHZ_LR_EQUAL_0DBFS => 1u, 
					ToneEnum.TONE_1KHZ_L_ONLY_0DBFS => 2u, 
					ToneEnum.TONE_1KHZ_R_ONLY_0DBFS => 3u, 
					ToneEnum.TONE_1KHZ_LR_EQUAL_MINUS18DBFS => 4u, 
					ToneEnum.TONE_15KHZ_LR_EQUAL_MINUS18DBFS => 5u, 
					ToneEnum.TONE_1KHZ_LR_EQUAL_MINUS24DBFS => 6u, 
					ToneEnum.TONE_15KHZ_LR_EQUAL_MINUS24DBFS => 7u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ToneToString(ToneEnum enumItem)
			{
				return enumItem switch
				{
					ToneEnum.NO_TONE => "No Tone", 
					ToneEnum.TONE_1KHZ_LR_EQUAL_0DBFS => "1 kHz L&R Equal, 0dBFS", 
					ToneEnum.TONE_1KHZ_L_ONLY_0DBFS => "1 kHz Left Only, 0dBFS", 
					ToneEnum.TONE_1KHZ_R_ONLY_0DBFS => "1 kHz Right Only, 0dBFS", 
					ToneEnum.TONE_1KHZ_LR_EQUAL_MINUS18DBFS => "1 kHz L&R Equal, -18dBFS", 
					ToneEnum.TONE_15KHZ_LR_EQUAL_MINUS18DBFS => "1 kHz L&R Equal, -18dBFS", 
					ToneEnum.TONE_1KHZ_LR_EQUAL_MINUS24DBFS => "1 kHz L&R Equal, -24dBFS", 
					ToneEnum.TONE_15KHZ_LR_EQUAL_MINUS24DBFS => "1 kHz L&R Equal, -24dBFS", 
					_ => "Invalid Tone Value", 
				};
			}

			public static string ToneToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No Tone");
				stringBuilder.AppendLine("0x1 = 1 kHz L&R Equal, 0dBFS");
				stringBuilder.AppendLine("0x2 = 1 kHz Left Only, 0dBFS");
				stringBuilder.AppendLine("0x3 = 1 kHz Right Only, 0dBFS");
				stringBuilder.AppendLine("0x4 = 1 kHz L&R Equal, -18dBFS");
				stringBuilder.AppendLine("0x5 = 1 kHz L&R Equal, -18dBFS");
				stringBuilder.AppendLine("0x6 = 1 kHz L&R Equal, -24dBFS");
				stringBuilder.AppendLine("0x7 = 1 kHz L&R Equal, -24dBFS");
				return stringBuilder.ToString();
			}
		}

		private uint _Tone;

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

		public ToneEnum GetToneAsToneEnum()
		{
			return ToneInfo.ValueToTone(_Tone);
		}

		public void SetToneAsToneEnum(ToneEnum enumItem)
		{
			Tone = ToneInfo.ToneToValue(enumItem);
		}
	}

	private const uint _Opcode = 64520u;

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

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

	public uint Opcode => 64520u;

	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.Tone = 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.Tone, 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.Tone > 7)
		{
			list.Add("Maximum allowed value for Tone is 0x7");
		}
		return list;
	}
}
