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

namespace QCRPG.TSL.BT.Packets;

public class LeMultiAdvtSetAdvtEnableMulti : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum AdvertisingEnableEnum
		{
			Advertising_is_disabled,
			Advertising_is_enabled,
			Reserved_for_future_use,
			InvalidAdvertisingEnableValue
		}

		public static class AdvertisingEnableInfo
		{
			public static AdvertisingEnableEnum ValueToAdvertisingEnable(uint value)
			{
				switch (value)
				{
				case 0u:
					return AdvertisingEnableEnum.Advertising_is_disabled;
				case 1u:
					return AdvertisingEnableEnum.Advertising_is_enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return AdvertisingEnableEnum.Reserved_for_future_use;
					}
					return AdvertisingEnableEnum.InvalidAdvertisingEnableValue;
				}
			}

			public static uint AdvertisingEnableToValue(AdvertisingEnableEnum enumItem)
			{
				return enumItem switch
				{
					AdvertisingEnableEnum.Advertising_is_disabled => 0u, 
					AdvertisingEnableEnum.Advertising_is_enabled => 1u, 
					AdvertisingEnableEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string AdvertisingEnableToString(AdvertisingEnableEnum enumItem)
			{
				return enumItem switch
				{
					AdvertisingEnableEnum.Advertising_is_disabled => "Advertising is disabled (default)", 
					AdvertisingEnableEnum.Advertising_is_enabled => "Advertising is enabled", 
					AdvertisingEnableEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid AdvertisingEnable Value", 
				};
			}

			public static string AdvertisingEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Advertising is disabled (default)");
				stringBuilder.AppendLine("0x1 = Advertising is enabled");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private const uint _MultiAdvtOpcode = 5u;

		private uint _AdvertisingEnable;

		private uint _AdvertisingInstance = 1u;

		public uint MultiAdvtOpcode => 5u;

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

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

		public AdvertisingEnableEnum GetAdvertisingEnableAsAdvertisingEnableEnum()
		{
			return AdvertisingEnableInfo.ValueToAdvertisingEnable(_AdvertisingEnable);
		}

		public void SetAdvertisingEnableAsAdvertisingEnableEnum(AdvertisingEnableEnum enumItem)
		{
			AdvertisingEnable = AdvertisingEnableInfo.AdvertisingEnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 64852u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLeMultiAdvtSetAdvtEnableMulti),
		typeof(CommandCompleteForNop)
	};

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

	public uint Opcode => 64852u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.AdvertisingInstance = 1u;
	}

	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.MultiAdvtOpcode)
		{
			return false;
		}
		UserFields.AdvertisingEnable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvertisingInstance = 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.MultiAdvtOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingEnable, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingInstance, 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.AdvertisingInstance < 1)
		{
			list.Add("Minimum allowed value for Advertising_Instance is 0x1");
		}
		return list;
	}
}
