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

namespace QCRPG.TSL.BT.Packets;

public class SetMwsScanFrequencyTable : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _NumScanFrequencies;

		private List<NumMwsScanFreqSubField> _NumMwsScanFreqList = new List<NumMwsScanFreqSubField>();

		public uint NumScanFrequencies
		{
			get
			{
				return _NumScanFrequencies;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumScanFrequencies is 0xFF");
				}
				_NumScanFrequencies = value;
				while (_NumMwsScanFreqList.Count < _NumScanFrequencies)
				{
					_NumMwsScanFreqList.Add(new NumMwsScanFreqSubField());
				}
				while (_NumMwsScanFreqList.Count > _NumScanFrequencies)
				{
					_NumMwsScanFreqList.RemoveAt(_NumMwsScanFreqList.Count - 1);
				}
			}
		}

		public List<NumMwsScanFreqSubField> NumMwsScanFreqList => _NumMwsScanFreqList;
	}

	public class NumMwsScanFreqSubField
	{
		private uint _ScanFrequencyLow;

		private uint _ScanFrequencyHigh;

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

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			ScanFrequencyLow = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			ScanFrequencyHigh = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(ScanFrequencyLow, 16);
			bitBuilder.AddBitsAsUintLittleEndian(ScanFrequencyHigh, 16);
			return bitBuilder.ToString();
		}
	}

	private const uint _Opcode = 3186u;

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

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

	public uint Opcode => 3186u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	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.NumScanFrequencies = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.NumMwsScanFreqList.Count; i++)
		{
			UserFields.NumMwsScanFreqList[i].FromBitBuilder(ref bitBuilder);
		}
		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.NumScanFrequencies, 8);
		for (int i = 0; i < UserFields.NumMwsScanFreqList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.NumMwsScanFreqList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
