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

namespace QCRPG.TSL.FMHCI.Packets;

public class StereoStatus : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum StereoIndicatorEnum
		{
			MONO_DEMODULATION,
			STEREO_DEMODULATION,
			InvalidStereoIndicatorValue
		}

		public static class StereoIndicatorInfo
		{
			public static StereoIndicatorEnum ValueToStereoIndicator(uint value)
			{
				return value switch
				{
					0u => StereoIndicatorEnum.MONO_DEMODULATION, 
					1u => StereoIndicatorEnum.STEREO_DEMODULATION, 
					_ => StereoIndicatorEnum.InvalidStereoIndicatorValue, 
				};
			}

			public static uint StereoIndicatorToValue(StereoIndicatorEnum enumItem)
			{
				return enumItem switch
				{
					StereoIndicatorEnum.MONO_DEMODULATION => 0u, 
					StereoIndicatorEnum.STEREO_DEMODULATION => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string StereoIndicatorToString(StereoIndicatorEnum enumItem)
			{
				return enumItem switch
				{
					StereoIndicatorEnum.MONO_DEMODULATION => "MONO_DEMODULATION", 
					StereoIndicatorEnum.STEREO_DEMODULATION => "STEREO_DEMODULATION", 
					_ => "Invalid StereoIndicator Value", 
				};
			}

			public static string StereoIndicatorToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = MONO_DEMODULATION");
				stringBuilder.AppendLine("0x1 = STEREO_DEMODULATION");
				return stringBuilder.ToString();
			}
		}

		private uint _PilotPllLockIndicator;

		private uint _HardMuteRightChannelIndicator;

		private uint _HardMuteLeftChannelIndicator;

		private uint _InterferenceMitigatedSoftMuteIndicator;

		private uint _InterferenceMitigatedBlendingIndicator;

		private uint _SoftMuteIndicator;

		private uint _BlendingOnIndicator;

		private uint _StereoIndicator;

		public uint PilotPllLockIndicator
		{
			get
			{
				return _PilotPllLockIndicator;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for PilotPllLockIndicator is 0x1");
				}
				_PilotPllLockIndicator = value;
			}
		}

		public uint HardMuteRightChannelIndicator
		{
			get
			{
				return _HardMuteRightChannelIndicator;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for HardMuteRightChannelIndicator is 0x1");
				}
				_HardMuteRightChannelIndicator = value;
			}
		}

		public uint HardMuteLeftChannelIndicator
		{
			get
			{
				return _HardMuteLeftChannelIndicator;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for HardMuteLeftChannelIndicator is 0x1");
				}
				_HardMuteLeftChannelIndicator = value;
			}
		}

		public uint InterferenceMitigatedSoftMuteIndicator
		{
			get
			{
				return _InterferenceMitigatedSoftMuteIndicator;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for InterferenceMitigatedSoftMuteIndicator is 0x1");
				}
				_InterferenceMitigatedSoftMuteIndicator = value;
			}
		}

		public uint InterferenceMitigatedBlendingIndicator
		{
			get
			{
				return _InterferenceMitigatedBlendingIndicator;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for InterferenceMitigatedBlendingIndicator is 0x1");
				}
				_InterferenceMitigatedBlendingIndicator = value;
			}
		}

		public uint SoftMuteIndicator
		{
			get
			{
				return _SoftMuteIndicator;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for SoftMuteIndicator is 0x1");
				}
				_SoftMuteIndicator = value;
			}
		}

		public uint BlendingOnIndicator
		{
			get
			{
				return _BlendingOnIndicator;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for BlendingOnIndicator is 0x1");
				}
				_BlendingOnIndicator = value;
			}
		}

		public uint StereoIndicator
		{
			get
			{
				return _StereoIndicator;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for StereoIndicator is 0x1");
				}
				_StereoIndicator = value;
			}
		}

		public StereoIndicatorEnum GetStereoIndicatorAsStereoIndicatorEnum()
		{
			return StereoIndicatorInfo.ValueToStereoIndicator(_StereoIndicator);
		}

		public void SetStereoIndicatorAsStereoIndicatorEnum(StereoIndicatorEnum enumItem)
		{
			StereoIndicator = StereoIndicatorInfo.StereoIndicatorToValue(enumItem);
		}
	}

	private const uint _EventCode = 3u;

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

	public uint EventCode => 3u;

	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.PilotPllLockIndicator = bitBuilder.RemoveBitsAsUint(1);
		UserFields.HardMuteRightChannelIndicator = bitBuilder.RemoveBitsAsUint(1);
		UserFields.HardMuteLeftChannelIndicator = bitBuilder.RemoveBitsAsUint(1);
		UserFields.InterferenceMitigatedSoftMuteIndicator = bitBuilder.RemoveBitsAsUint(1);
		UserFields.InterferenceMitigatedBlendingIndicator = bitBuilder.RemoveBitsAsUint(1);
		UserFields.SoftMuteIndicator = bitBuilder.RemoveBitsAsUint(1);
		UserFields.BlendingOnIndicator = bitBuilder.RemoveBitsAsUint(1);
		UserFields.StereoIndicator = bitBuilder.RemoveBitsAsUint(1);
		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.PilotPllLockIndicator, 1);
		bitBuilder.AddBitsAsUint(UserFields.HardMuteRightChannelIndicator, 1);
		bitBuilder.AddBitsAsUint(UserFields.HardMuteLeftChannelIndicator, 1);
		bitBuilder.AddBitsAsUint(UserFields.InterferenceMitigatedSoftMuteIndicator, 1);
		bitBuilder.AddBitsAsUint(UserFields.InterferenceMitigatedBlendingIndicator, 1);
		bitBuilder.AddBitsAsUint(UserFields.SoftMuteIndicator, 1);
		bitBuilder.AddBitsAsUint(UserFields.BlendingOnIndicator, 1);
		bitBuilder.AddBitsAsUint(UserFields.StereoIndicator, 1);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
