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

namespace QCRPG.TSL.BT.Packets;

public class QbceQleSetCigAltParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _Subopcode = 5u;

		private uint _CigId;

		private uint _CisCount;

		private List<CisParametersSubField> _CisParametersList = new List<CisParametersSubField>();

		public uint Subopcode => 5u;

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

		public uint CisCount
		{
			get
			{
				return _CisCount;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for CisCount is 0xFF");
				}
				_CisCount = value;
				while (_CisParametersList.Count < _CisCount)
				{
					_CisParametersList.Add(new CisParametersSubField());
				}
				while (_CisParametersList.Count > _CisCount)
				{
					_CisParametersList.RemoveAt(_CisParametersList.Count - 1);
				}
			}
		}

		public List<CisParametersSubField> CisParametersList => _CisParametersList;
	}

	public class CisParametersSubField
	{
		public enum CisEnableEnum
		{
			Disable,
			Enable,
			InvalidCisEnableValue
		}

		public static class CisEnableInfo
		{
			public static CisEnableEnum ValueToCisEnable(uint value)
			{
				return value switch
				{
					0u => CisEnableEnum.Disable, 
					1u => CisEnableEnum.Enable, 
					_ => CisEnableEnum.InvalidCisEnableValue, 
				};
			}

			public static uint CisEnableToValue(CisEnableEnum enumItem)
			{
				return enumItem switch
				{
					CisEnableEnum.Disable => 0u, 
					CisEnableEnum.Enable => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string CisEnableToString(CisEnableEnum enumItem)
			{
				return enumItem switch
				{
					CisEnableEnum.Disable => "The alternate parameters for this CIS are disabled", 
					CisEnableEnum.Enable => "The alternate parameters for this CIS are enabled", 
					_ => "Invalid CisEnable Value", 
				};
			}

			public static string CisEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The alternate parameters for this CIS are disabled");
				stringBuilder.AppendLine("0x1 = The alternate parameters for this CIS are enabled");
				return stringBuilder.ToString();
			}
		}

		private uint _CisId;

		private uint _CisEnable;

		private uint _PhyMToS;

		private uint _PhySToM;

		private uint _RtnMToS;

		private uint _RtnSToM;

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

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

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

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

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

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

		public void SetDefaultValues()
		{
			CisId = 0u;
			CisEnable = 0u;
			RtnMToS = 2u;
			RtnSToM = 2u;
		}

		public CisEnableEnum GetCisEnableAsCisEnableEnum()
		{
			return CisEnableInfo.ValueToCisEnable(_CisEnable);
		}

		public void SetCisEnableAsCisEnableEnum(CisEnableEnum enumItem)
		{
			CisEnable = CisEnableInfo.CisEnableToValue(enumItem);
		}

		public QhsTransmitterPhyPreferenceEnumCollection GetPhyMToSAsQhsTransmitterPhyPreferenceEnumCollection()
		{
			return QhsTransmitterPhyPreferenceInfo.ValueToQhsTransmitterPhyPreference(_PhyMToS);
		}

		public void SetPhyMToSAsQhsTransmitterPhyPreferenceEnumCollection(QhsTransmitterPhyPreferenceEnumCollection enumCollection)
		{
			PhyMToS = QhsTransmitterPhyPreferenceInfo.QhsTransmitterPhyPreferenceToValue(enumCollection);
		}

		public QhsTransmitterPhyPreferenceEnumCollection GetPhySToMAsQhsTransmitterPhyPreferenceEnumCollection()
		{
			return QhsTransmitterPhyPreferenceInfo.ValueToQhsTransmitterPhyPreference(_PhySToM);
		}

		public void SetPhySToMAsQhsTransmitterPhyPreferenceEnumCollection(QhsTransmitterPhyPreferenceEnumCollection enumCollection)
		{
			PhySToM = QhsTransmitterPhyPreferenceInfo.QhsTransmitterPhyPreferenceToValue(enumCollection);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			CisId = bitBuilder.RemoveBitsAsUint(8);
			CisEnable = bitBuilder.RemoveBitsAsUint(8);
			PhyMToS = bitBuilder.RemoveBitsAsUint(8);
			PhySToM = bitBuilder.RemoveBitsAsUint(8);
			RtnMToS = bitBuilder.RemoveBitsAsUint(8);
			RtnSToM = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(CisId, 8);
			bitBuilder.AddBitsAsUint(CisEnable, 8);
			bitBuilder.AddBitsAsUint(PhyMToS, 8);
			bitBuilder.AddBitsAsUint(PhySToM, 8);
			bitBuilder.AddBitsAsUint(RtnMToS, 8);
			bitBuilder.AddBitsAsUint(RtnSToM, 8);
			return bitBuilder.ToString();
		}

		public List<string> GetWarnings()
		{
			List<string> list = new List<string>();
			if (CisId < 0)
			{
				list.Add("Minimum allowed value for CIS_ID is 0x0");
			}
			if (CisId > 239)
			{
				list.Add("Maximum allowed value for CIS_ID is 0xEF");
			}
			if (CisEnable < 0)
			{
				list.Add("Minimum allowed value for CIS_Enable is 0x0");
			}
			if (CisEnable > 1)
			{
				list.Add("Maximum allowed value for CIS_Enable is 0x1");
			}
			if (RtnMToS < 0)
			{
				list.Add("Minimum allowed value for RTN_M_TO_S is 0x0");
			}
			if (RtnMToS > 15)
			{
				list.Add("Maximum allowed value for RTN_M_TO_S is 0xF");
			}
			if (RtnSToM < 0)
			{
				list.Add("Minimum allowed value for RTN_S_TO_M is 0x0");
			}
			if (RtnSToM > 15)
			{
				list.Add("Maximum allowed value for RTN_S_TO_M is 0xF");
			}
			return list;
		}
	}

	private const uint _Opcode = 64593u;

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

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

	public uint Opcode => 64593u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.CigId = 0u;
		UserFields.CisCount = 1u;
		for (int i = 0; i < UserFields.CisParametersList.Count; i++)
		{
			UserFields.CisParametersList[i].SetDefaultValues();
		}
	}

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Subopcode)
		{
			return false;
		}
		UserFields.CigId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.CisCount = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.CisParametersList.Count; i++)
		{
			UserFields.CisParametersList[i].FromBitBuilder(ref bitBuilder);
		}
		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.Subopcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.CigId, 8);
		bitBuilder.AddBitsAsUint(UserFields.CisCount, 8);
		for (int i = 0; i < UserFields.CisParametersList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.CisParametersList[i].ToBitString());
		}
		_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.CigId < 0)
		{
			list.Add("Minimum allowed value for CIG_ID is 0x0");
		}
		if (UserFields.CigId > 239)
		{
			list.Add("Maximum allowed value for CIG_ID is 0xEF");
		}
		if (UserFields.CisCount < 0)
		{
			list.Add("Minimum allowed value for CIS_Count is 0x0");
		}
		if (UserFields.CisCount > 16)
		{
			list.Add("Maximum allowed value for CIS_Count is 0x10");
		}
		for (int i = 0; i < UserFields.CisParametersList.Count; i++)
		{
			list.AddRange(UserFields.CisParametersList[i].GetWarnings());
		}
		return list;
	}
}
