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

namespace QCRPG.TSL.BT.Packets;

public class DebugSimulateCxmMwsRx : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum MwsTypeEnum
		{
			REAL_TIME_SIGNAL_MESSAGE,
			TRANSPORT_CONTROL_MESSAGE,
			TRANSPARENT_DATA_MESSAGE,
			MWS_INACTIVITY_DURATION_MESSAGE,
			MWS_SCAN_FREQUENCY_MESSAGE,
			RFU,
			VENDOR_SPECIFIC,
			InvalidMwsTypeValue
		}

		public static class MwsTypeInfo
		{
			public static MwsTypeEnum ValueToMwsType(uint value)
			{
				switch (value)
				{
				case 0u:
					return MwsTypeEnum.REAL_TIME_SIGNAL_MESSAGE;
				case 1u:
					return MwsTypeEnum.TRANSPORT_CONTROL_MESSAGE;
				case 2u:
					return MwsTypeEnum.TRANSPARENT_DATA_MESSAGE;
				case 3u:
					return MwsTypeEnum.MWS_INACTIVITY_DURATION_MESSAGE;
				case 4u:
					return MwsTypeEnum.MWS_SCAN_FREQUENCY_MESSAGE;
				case 5u:
					return MwsTypeEnum.RFU;
				default:
					if (value >= 6 && value <= 7)
					{
						return MwsTypeEnum.VENDOR_SPECIFIC;
					}
					return MwsTypeEnum.InvalidMwsTypeValue;
				}
			}

			public static uint MwsTypeToValue(MwsTypeEnum enumItem)
			{
				return enumItem switch
				{
					MwsTypeEnum.REAL_TIME_SIGNAL_MESSAGE => 0u, 
					MwsTypeEnum.TRANSPORT_CONTROL_MESSAGE => 1u, 
					MwsTypeEnum.TRANSPARENT_DATA_MESSAGE => 2u, 
					MwsTypeEnum.MWS_INACTIVITY_DURATION_MESSAGE => 3u, 
					MwsTypeEnum.MWS_SCAN_FREQUENCY_MESSAGE => 4u, 
					MwsTypeEnum.RFU => 5u, 
					MwsTypeEnum.VENDOR_SPECIFIC => 6u, 
					_ => uint.MaxValue, 
				};
			}

			public static string MwsTypeToString(MwsTypeEnum enumItem)
			{
				return enumItem switch
				{
					MwsTypeEnum.REAL_TIME_SIGNAL_MESSAGE => "Real Time Signal Message", 
					MwsTypeEnum.TRANSPORT_CONTROL_MESSAGE => "Transport Control Message", 
					MwsTypeEnum.TRANSPARENT_DATA_MESSAGE => "Transparent Data Message", 
					MwsTypeEnum.MWS_INACTIVITY_DURATION_MESSAGE => "MWS Inactivity Duration Message", 
					MwsTypeEnum.MWS_SCAN_FREQUENCY_MESSAGE => "MWS Scan Frequency Message", 
					MwsTypeEnum.RFU => "RFU", 
					MwsTypeEnum.VENDOR_SPECIFIC => "Vendor Specific", 
					_ => "Invalid MwsType Value", 
				};
			}

			public static string MwsTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Real Time Signal Message");
				stringBuilder.AppendLine("0x1 = Transport Control Message");
				stringBuilder.AppendLine("0x2 = Transparent Data Message");
				stringBuilder.AppendLine("0x3 = MWS Inactivity Duration Message");
				stringBuilder.AppendLine("0x4 = MWS Scan Frequency Message");
				stringBuilder.AppendLine("0x5 = RFU");
				stringBuilder.AppendLine("0x6-0x7 = Vendor Specific");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 55u;

		private uint _MwsType;

		private uint _MwsBodyLength;

		private List<uint> _MwsBodyList = new List<uint>();

		public uint SubOpcode => 55u;

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

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

		public List<uint> MwsBodyList => _MwsBodyList;

		public MwsTypeEnum GetMwsTypeAsMwsTypeEnum()
		{
			return MwsTypeInfo.ValueToMwsType(_MwsType);
		}

		public void SetMwsTypeAsMwsTypeEnum(MwsTypeEnum enumItem)
		{
			MwsType = MwsTypeInfo.MwsTypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 64524u;

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

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

	public uint Opcode => 64524u;

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubOpcode)
		{
			return false;
		}
		UserFields.MwsType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MwsBodyLength = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.MwsBodyList.Count; i++)
		{
			UserFields.MwsBodyList[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.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.MwsType, 8);
		bitBuilder.AddBitsAsUint(UserFields.MwsBodyLength, 8);
		for (int i = 0; i < UserFields.MwsBodyList.Count; i++)
		{
			if (UserFields.MwsBodyList[i] > 255)
			{
				throw new Exception("UserFields.MwsBodyList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.MwsBodyList[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();
	}
}
