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

namespace QCRPG.TSL.BT.Packets;

public class ReceiveSynchronizationTrain : PacketBase
{
	public class UserFieldsWrapper
	{
		private ulong _BdAddress;

		private uint _SynchronizationScanTo = 34u;

		private uint _SyncScanWindow = 34u;

		private uint _SyncScanInterval = 2u;

		public ulong BdAddress
		{
			get
			{
				return _BdAddress;
			}
			set
			{
				_BdAddress = value;
			}
		}

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

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

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

	private const uint _Opcode = 1092u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForReceiveSynchronizationTrain),
		typeof(SynchronizationTrainRecieved)
	};

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

	public uint Opcode => 1092u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.SynchronizationScanTo = 34u;
		UserFields.SyncScanWindow = 34u;
		UserFields.SyncScanInterval = 2u;
	}

	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.BdAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.SynchronizationScanTo = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.SyncScanWindow = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.SyncScanInterval = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.AddBitsAsUlongLittleEndian(UserFields.BdAddress, 48);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SynchronizationScanTo, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SyncScanWindow, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SyncScanInterval, 16);
		_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.SynchronizationScanTo < 34)
		{
			list.Add("Minimum allowed value for Synchronization_scan_TO is 0x22");
		}
		if (UserFields.SynchronizationScanTo > 65534)
		{
			list.Add("Maximum allowed value for Synchronization_scan_TO is 0xFFFE");
		}
		if (UserFields.SyncScanWindow < 34)
		{
			list.Add("Minimum allowed value for Sync_Scan_Window is 0x22");
		}
		if (UserFields.SyncScanWindow > 65534)
		{
			list.Add("Maximum allowed value for Sync_Scan_Window is 0xFFFE");
		}
		if (UserFields.SyncScanInterval < 2)
		{
			list.Add("Minimum allowed value for Sync_Scan_Interval is 0x2");
		}
		if (UserFields.SyncScanInterval > 65534)
		{
			list.Add("Maximum allowed value for Sync_Scan_Interval is 0xFFFE");
		}
		return list;
	}
}
