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

namespace QCRPG.TSL.FMHCI.Packets;

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

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

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

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

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

		private const uint _CommandOpcode = 19484u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _Type;

		private uint _RampRateUp;

		private uint _DebounceNumberOfSamplesUp;

		private uint _DebounceIndexUp;

		private uint _SinrIndexSkipStep;

		private uint _SinrHigh;

		private uint _RmssiHigh;

		private uint _IndexHigh;

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

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

		public uint CommandOpcode => 19484u;

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

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

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

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

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

		public uint SinrIndexSkipStep
		{
			get
			{
				return _SinrIndexSkipStep;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for SinrIndexSkipStep is 0xFF");
				}
				_SinrIndexSkipStep = 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> BlendTableList => _BlendTableList;

		public CommandOpcodeEnum GetCommandOpcodeAsCommandOpcodeEnum()
		{
			return CommandOpcodeInfo.ValueToCommandOpcode(19484u);
		}

		public ErrorCodeEnum GetStatusAsErrorCodeEnum()
		{
			return ErrorCodeInfo.ValueToErrorCode(_Status);
		}

		public void SetStatusAsErrorCodeEnum(ErrorCodeEnum enumItem)
		{
			Status = ErrorCodeInfo.ErrorCodeToValue(enumItem);
		}

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

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

	private const uint _EventCode = 15u;

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Response;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new CommandCompleteForGetBlendInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 15u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.NumHciCommandPackets = 1u;
		UserFields.RampRateUp = 2u;
		UserFields.DebounceIndexUp = 9u;
		UserFields.SinrIndexSkipStep = 1u;
		UserFields.SinrHigh = 25u;
		UserFields.RmssiHigh = 179u;
		UserFields.IndexHigh = 19u;
		UserFields.BlendTableList[1] = 1u;
		UserFields.BlendTableList[2] = 4u;
		UserFields.BlendTableList[3] = 7u;
		UserFields.BlendTableList[4] = 11u;
		UserFields.BlendTableList[5] = 16u;
		UserFields.BlendTableList[6] = 23u;
		UserFields.BlendTableList[7] = 30u;
		UserFields.BlendTableList[8] = 38u;
		UserFields.BlendTableList[9] = 47u;
		UserFields.BlendTableList[10] = 56u;
		UserFields.BlendTableList[11] = 67u;
		UserFields.BlendTableList[12] = 79u;
		UserFields.BlendTableList[13] = 92u;
		UserFields.BlendTableList[14] = 105u;
		UserFields.BlendTableList[15] = 120u;
		UserFields.BlendTableList[16] = 135u;
		UserFields.BlendTableList[17] = 152u;
		UserFields.BlendTableList[18] = 169u;
		UserFields.BlendTableList[19] = 188u;
	}

	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.NumHciCommandPackets = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.CommandOpcode)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Type = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RampRateUp = bitBuilder.RemoveBitsAsUint(8);
		UserFields.DebounceNumberOfSamplesUp = bitBuilder.RemoveBitsAsUint(8);
		UserFields.DebounceIndexUp = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SinrIndexSkipStep = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SinrHigh = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RmssiHigh = bitBuilder.RemoveBitsAsUint(8);
		UserFields.IndexHigh = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.BlendTableList.Count; i++)
		{
			UserFields.BlendTableList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		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.NumHciCommandPackets, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.CommandOpcode, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUint(UserFields.Type, 8);
		bitBuilder.AddBitsAsUint(UserFields.RampRateUp, 8);
		bitBuilder.AddBitsAsUint(UserFields.DebounceNumberOfSamplesUp, 8);
		bitBuilder.AddBitsAsUint(UserFields.DebounceIndexUp, 8);
		bitBuilder.AddBitsAsUint(UserFields.SinrIndexSkipStep, 8);
		bitBuilder.AddBitsAsUint(UserFields.SinrHigh, 8);
		bitBuilder.AddBitsAsUint(UserFields.RmssiHigh, 8);
		bitBuilder.AddBitsAsUint(UserFields.IndexHigh, 8);
		for (int i = 0; i < UserFields.BlendTableList.Count; i++)
		{
			if (UserFields.BlendTableList[i] > 255)
			{
				throw new Exception("UserFields.BlendTableList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.BlendTableList[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 > 2)
		{
			list.Add("Maximum allowed value for Type is 0x2");
		}
		if (UserFields.RampRateUp > 49)
		{
			list.Add("Maximum allowed value for Ramp Rate Up is 0x31");
		}
		if (UserFields.DebounceIndexUp > 49)
		{
			list.Add("Maximum allowed value for Debounce Index Up is 0x31");
		}
		if (UserFields.SinrIndexSkipStep > 48)
		{
			list.Add("Maximum allowed value for SINR Index Skip Step is 0x30");
		}
		if (UserFields.IndexHigh > 49)
		{
			list.Add("Maximum allowed value for Index High is 0x31");
		}
		return list;
	}
}
