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

namespace QCRPG.TSL.ZigBee.Packets;

public class VsDutTxRaw : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum FlagsEnum
		{
			PHR_NO_CRC,
			NO_PHR_NO_CRC,
			PHR_CRC,
			CRC_NO_PHR,
			InvalidFlagsValue
		}

		public static class FlagsInfo
		{
			public static FlagsEnum ValueToFlags(uint value)
			{
				return value switch
				{
					0u => FlagsEnum.PHR_NO_CRC, 
					1u => FlagsEnum.NO_PHR_NO_CRC, 
					2u => FlagsEnum.PHR_CRC, 
					3u => FlagsEnum.CRC_NO_PHR, 
					_ => FlagsEnum.InvalidFlagsValue, 
				};
			}

			public static uint FlagsToValue(FlagsEnum enumItem)
			{
				return enumItem switch
				{
					FlagsEnum.PHR_NO_CRC => 0u, 
					FlagsEnum.NO_PHR_NO_CRC => 1u, 
					FlagsEnum.PHR_CRC => 2u, 
					FlagsEnum.CRC_NO_PHR => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string FlagsToString(FlagsEnum enumItem)
			{
				return enumItem switch
				{
					FlagsEnum.PHR_NO_CRC => "PHR_NO_CRC", 
					FlagsEnum.NO_PHR_NO_CRC => "NO_PHR_NO_CRC", 
					FlagsEnum.PHR_CRC => "PHR_CRC", 
					FlagsEnum.CRC_NO_PHR => "CRC_NO_PHR", 
					_ => "Invalid Flags Value", 
				};
			}

			public static string FlagsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = PHR_NO_CRC");
				stringBuilder.AppendLine("0x1 = NO_PHR_NO_CRC");
				stringBuilder.AppendLine("0x2 = PHR_CRC");
				stringBuilder.AppendLine("0x3 = CRC_NO_PHR");
				return stringBuilder.ToString();
			}
		}

		private uint _Flags;

		private uint _Page;

		private uint _Channel = 11u;

		private uint _Power;

		private uint _Payloadlength;

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

		public uint Flags
		{
			get
			{
				return _Flags;
			}
			set
			{
				_Flags = value;
			}
		}

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

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

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

		public uint Payloadlength
		{
			get
			{
				return _Payloadlength;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Payloadlength is 0xFFFF");
				}
				_Payloadlength = value;
				while (_PayloadList.Count < _Payloadlength)
				{
					_PayloadList.Add(0u);
				}
				while (_PayloadList.Count > _Payloadlength)
				{
					_PayloadList.RemoveAt(_PayloadList.Count - 1);
				}
			}
		}

		public List<uint> PayloadList => _PayloadList;

		public FlagsEnum GetFlagsAsFlagsEnum()
		{
			return FlagsInfo.ValueToFlags(_Flags);
		}

		public void SetFlagsAsFlagsEnum(FlagsEnum enumItem)
		{
			Flags = FlagsInfo.FlagsToValue(enumItem);
		}
	}

	private const uint _Packetcode = 139u;

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

	private PacketInfoBase info;

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

	public uint Packetcode => 139u;

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Flags = 2u;
		UserFields.Channel = 11u;
		UserFields.Power = 15u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(8) != Packetcode)
		{
			return false;
		}
		UserFields.Flags = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.Page = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Channel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Power = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Payloadlength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		for (int num = UserFields.PayloadList.Count - 1; num >= 0; num--)
		{
			UserFields.PayloadList[num] = bitBuilder.RemoveBitsAsUint(8);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Packetcode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Flags, 32);
		bitBuilder.AddBitsAsUint(UserFields.Page, 8);
		bitBuilder.AddBitsAsUint(UserFields.Channel, 8);
		bitBuilder.AddBitsAsUint(UserFields.Power, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Payloadlength, 16);
		for (int num = UserFields.PayloadList.Count - 1; num >= 0; num--)
		{
			if (UserFields.PayloadList[num] > 255)
			{
				throw new Exception("UserFields.PayloadList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.PayloadList[num], 8);
		}
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Flags < 0)
		{
			list.Add("Minimum allowed value for Flags is 0x0");
		}
		if (UserFields.Flags > 3)
		{
			list.Add("Maximum allowed value for Flags is 0x3");
		}
		if (UserFields.Channel < 11)
		{
			list.Add("Minimum allowed value for Channel is 0xB");
		}
		if (UserFields.Channel > 26)
		{
			list.Add("Maximum allowed value for Channel is 0x1A");
		}
		if (UserFields.Power < 0)
		{
			list.Add("Minimum allowed value for Power is 0x0");
		}
		if (UserFields.Power > 15)
		{
			list.Add("Maximum allowed value for Power is 0xF");
		}
		return list;
	}
}
