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

namespace QCRPG.TSL.FMHCI.Packets;

public class SetSoftMute : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum TypeEnum
		{
			CONSIDER_SINR_ONLY,
			CONSIDER_BOTH_SINR_AND_RMSSI,
			InvalidTypeValue
		}

		public static class TypeInfo
		{
			public static TypeEnum ValueToType(uint value)
			{
				return value switch
				{
					0u => TypeEnum.CONSIDER_SINR_ONLY, 
					1u => TypeEnum.CONSIDER_BOTH_SINR_AND_RMSSI, 
					_ => TypeEnum.InvalidTypeValue, 
				};
			}

			public static uint TypeToValue(TypeEnum enumItem)
			{
				return enumItem switch
				{
					TypeEnum.CONSIDER_SINR_ONLY => 0u, 
					TypeEnum.CONSIDER_BOTH_SINR_AND_RMSSI => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TypeToString(TypeEnum enumItem)
			{
				return enumItem switch
				{
					TypeEnum.CONSIDER_SINR_ONLY => "CONSIDER_SINR_ONLY", 
					TypeEnum.CONSIDER_BOTH_SINR_AND_RMSSI => "CONSIDER_BOTH_SINR_AND_RMSSI", 
					_ => "Invalid Type Value", 
				};
			}

			public static string TypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = CONSIDER_SINR_ONLY");
				stringBuilder.AppendLine("0x1 = CONSIDER_BOTH_SINR_AND_RMSSI");
				return stringBuilder.ToString();
			}
		}

		private uint _Type;

		private uint _RampRate;

		private uint _SinrHigh;

		private uint _RmssiHigh;

		private uint _IndexHigh;

		private List<uint> _SoftMuteTableList = new List<uint>(new uint[50]);

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

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

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

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

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

		public List<uint> SoftMuteTableList => _SoftMuteTableList;

		public TypeEnum GetTypeAsTypeEnum()
		{
			return TypeInfo.ValueToType(_Type);
		}

		public void SetTypeAsTypeEnum(TypeEnum enumItem)
		{
			Type = TypeInfo.TypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 19481u;

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

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

	public uint Opcode => 19481u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.RampRate = 1u;
		UserFields.SinrHigh = 8u;
		UserFields.RmssiHigh = 168u;
		UserFields.IndexHigh = 8u;
		UserFields.SoftMuteTableList[0] = 32u;
		UserFields.SoftMuteTableList[1] = 41u;
		UserFields.SoftMuteTableList[2] = 53u;
		UserFields.SoftMuteTableList[3] = 69u;
		UserFields.SoftMuteTableList[4] = 89u;
		UserFields.SoftMuteTableList[5] = 115u;
		UserFields.SoftMuteTableList[6] = 149u;
		UserFields.SoftMuteTableList[7] = 193u;
		UserFields.SoftMuteTableList[8] = 250u;
	}

	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.Type = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RampRate = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SinrHigh = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RmssiHigh = bitBuilder.RemoveBitsAsUint(8);
		UserFields.IndexHigh = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.SoftMuteTableList.Count; i++)
		{
			UserFields.SoftMuteTableList[i] = 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.Type, 8);
		bitBuilder.AddBitsAsUint(UserFields.RampRate, 8);
		bitBuilder.AddBitsAsUint(UserFields.SinrHigh, 8);
		bitBuilder.AddBitsAsUint(UserFields.RmssiHigh, 8);
		bitBuilder.AddBitsAsUint(UserFields.IndexHigh, 8);
		for (int i = 0; i < UserFields.SoftMuteTableList.Count; i++)
		{
			if (UserFields.SoftMuteTableList[i] > 255)
			{
				throw new Exception("UserFields.SoftMuteTableList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.SoftMuteTableList[i], 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.Type > 1)
		{
			list.Add("Maximum allowed value for Type is 0x1");
		}
		if (UserFields.IndexHigh > 49)
		{
			list.Add("Maximum allowed value for Index High is 0x31");
		}
		return list;
	}
}
