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

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeSync : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _Logicalchannel;

		private uint _Channelpage;

		private uint _Trackbeacon;

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

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

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

	private const uint _Packetcode = 44u;

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

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

	public uint Packetcode => 44u;

	public UserFieldsWrapper UserFields => _UserFields;

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

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(8) != Packetcode)
		{
			return false;
		}
		UserFields.Logicalchannel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Channelpage = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Trackbeacon = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Packetcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Logicalchannel, 8);
		bitBuilder.AddBitsAsUint(UserFields.Channelpage, 8);
		bitBuilder.AddBitsAsUint(UserFields.Trackbeacon, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Trackbeacon < 0)
		{
			list.Add("Minimum allowed value for TrackBeacon is 0x0");
		}
		if (UserFields.Trackbeacon > 1)
		{
			list.Add("Maximum allowed value for TrackBeacon is 0x1");
		}
		return list;
	}
}
