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

namespace QCRPG.TSL.BT.Packets;

public class SetMwsChannelParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum MwsChannelEnableEnum
		{
			MWS_channel_is_disabled,
			MWS_channel_is_enabled,
			Reserved,
			InvalidMwsChannelEnableValue
		}

		public static class MwsChannelEnableInfo
		{
			public static MwsChannelEnableEnum ValueToMwsChannelEnable(uint value)
			{
				switch (value)
				{
				case 0u:
					return MwsChannelEnableEnum.MWS_channel_is_disabled;
				case 1u:
					return MwsChannelEnableEnum.MWS_channel_is_enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return MwsChannelEnableEnum.Reserved;
					}
					return MwsChannelEnableEnum.InvalidMwsChannelEnableValue;
				}
			}

			public static uint MwsChannelEnableToValue(MwsChannelEnableEnum enumItem)
			{
				return enumItem switch
				{
					MwsChannelEnableEnum.MWS_channel_is_disabled => 0u, 
					MwsChannelEnableEnum.MWS_channel_is_enabled => 1u, 
					MwsChannelEnableEnum.Reserved => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string MwsChannelEnableToString(MwsChannelEnableEnum enumItem)
			{
				return enumItem switch
				{
					MwsChannelEnableEnum.MWS_channel_is_disabled => "MWS channel is disabled", 
					MwsChannelEnableEnum.MWS_channel_is_enabled => "MWS channel is enabled", 
					MwsChannelEnableEnum.Reserved => "Reserved", 
					_ => "Invalid MwsChannelEnable Value", 
				};
			}

			public static string MwsChannelEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = MWS channel is disabled");
				stringBuilder.AppendLine("0x1 = MWS channel is enabled");
				stringBuilder.AppendLine("0x2-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		private uint _MwsChannelEnable;

		private uint _MwsRxCenterFrequency;

		private uint _MwsTxCenterFrequency;

		private uint _MwsRxChannelBandwidth;

		private uint _MwsTxChannelBandwidth;

		private uint _MwsChannelType;

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

		public uint MwsRxCenterFrequency
		{
			get
			{
				return _MwsRxCenterFrequency;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for MwsRxCenterFrequency is 0xFFFF");
				}
				_MwsRxCenterFrequency = value;
			}
		}

		public uint MwsTxCenterFrequency
		{
			get
			{
				return _MwsTxCenterFrequency;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for MwsTxCenterFrequency is 0xFFFF");
				}
				_MwsTxCenterFrequency = value;
			}
		}

		public uint MwsRxChannelBandwidth
		{
			get
			{
				return _MwsRxChannelBandwidth;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for MwsRxChannelBandwidth is 0xFFFF");
				}
				_MwsRxChannelBandwidth = value;
			}
		}

		public uint MwsTxChannelBandwidth
		{
			get
			{
				return _MwsTxChannelBandwidth;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for MwsTxChannelBandwidth is 0xFFFF");
				}
				_MwsTxChannelBandwidth = value;
			}
		}

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

		public MwsChannelEnableEnum GetMwsChannelEnableAsMwsChannelEnableEnum()
		{
			return MwsChannelEnableInfo.ValueToMwsChannelEnable(_MwsChannelEnable);
		}

		public void SetMwsChannelEnableAsMwsChannelEnableEnum(MwsChannelEnableEnum enumItem)
		{
			MwsChannelEnable = MwsChannelEnableInfo.MwsChannelEnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 3182u;

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

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

	public uint Opcode => 3182u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.MwsChannelEnable = 0u;
	}

	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.MwsChannelEnable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MwsRxCenterFrequency = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsTxCenterFrequency = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsRxChannelBandwidth = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsTxChannelBandwidth = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsChannelType = 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.MwsChannelEnable, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsRxCenterFrequency, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsTxCenterFrequency, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsRxChannelBandwidth, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsTxChannelBandwidth, 16);
		bitBuilder.AddBitsAsUint(UserFields.MwsChannelType, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
