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

namespace QCRPG.TSL.BT.Packets;

public class LeSetDefaultPeroidicAdvertisingSyncTransferParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ModeEnum
		{
			Mode_0,
			Mode_1,
			Mode_2,
			InvalidModeValue
		}

		public static class ModeInfo
		{
			public static ModeEnum ValueToMode(uint value)
			{
				return value switch
				{
					0u => ModeEnum.Mode_0, 
					1u => ModeEnum.Mode_1, 
					2u => ModeEnum.Mode_2, 
					_ => ModeEnum.InvalidModeValue, 
				};
			}

			public static uint ModeToValue(ModeEnum enumItem)
			{
				return enumItem switch
				{
					ModeEnum.Mode_0 => 0u, 
					ModeEnum.Mode_1 => 1u, 
					ModeEnum.Mode_2 => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ModeToString(ModeEnum enumItem)
			{
				return enumItem switch
				{
					ModeEnum.Mode_0 => "No attempt to sync with the periodic advertiser introduced by PAST proceudure", 
					ModeEnum.Mode_1 => "Attempt to sync with the periodic advertiser and disable periodic adv reporting", 
					ModeEnum.Mode_2 => "Attempt to sync with the periodic advertiser and enable periodic adv reporting", 
					_ => "Invalid Mode Value", 
				};
			}

			public static string ModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No attempt to sync with the periodic advertiser introduced by PAST proceudure");
				stringBuilder.AppendLine("0x1 = Attempt to sync with the periodic advertiser and disable periodic adv reporting");
				stringBuilder.AppendLine("0x2 = Attempt to sync with the periodic advertiser and enable periodic adv reporting");
				return stringBuilder.ToString();
			}
		}

		private uint _Mode;

		private uint _Skip;

		private uint _SyncTimeout = 10u;

		private uint _SupplementalType;

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

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

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

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

		public ModeEnum GetModeAsModeEnum()
		{
			return ModeInfo.ValueToMode(_Mode);
		}

		public void SetModeAsModeEnum(ModeEnum enumItem)
		{
			Mode = ModeInfo.ModeToValue(enumItem);
		}
	}

	private const uint _Opcode = 8285u;

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

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

	public uint Opcode => 8285u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Mode = 0u;
		UserFields.Skip = 0u;
		UserFields.SyncTimeout = 10u;
	}

	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.Mode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Skip = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.SyncTimeout = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.SupplementalType = 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.Mode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Skip, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SyncTimeout, 16);
		bitBuilder.AddBitsAsUint(UserFields.SupplementalType, 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.Mode < 0)
		{
			list.Add("Minimum allowed value for Mode is 0x0");
		}
		if (UserFields.Mode > 2)
		{
			list.Add("Maximum allowed value for Mode is 0x2");
		}
		if (UserFields.Skip < 0)
		{
			list.Add("Minimum allowed value for Skip is 0x0");
		}
		if (UserFields.Skip > 499)
		{
			list.Add("Maximum allowed value for Skip is 0x1F3");
		}
		if (UserFields.SyncTimeout < 10)
		{
			list.Add("Minimum allowed value for Sync_Timeout is 0xA");
		}
		if (UserFields.SyncTimeout > 1024)
		{
			list.Add("Maximum allowed value for Sync_Timeout is 0x400");
		}
		return list;
	}
}
