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

namespace QCRPG.TSL.BT.Packets;

public class SetMwsPatternConfiguration : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _MwsPatternIndex;

		private uint _MwsPatternNumintervals;

		private List<NumIntervalsSubField> _NumIntervalsList = new List<NumIntervalsSubField>();

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

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

		public List<NumIntervalsSubField> NumIntervalsList => _NumIntervalsList;
	}

	public class NumIntervalsSubField
	{
		private uint _MwsPatternIntervalduration;

		private uint _MwsPatternIntervaltype;

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

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			MwsPatternIntervalduration = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			MwsPatternIntervaltype = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(MwsPatternIntervalduration, 16);
			bitBuilder.AddBitsAsUint(MwsPatternIntervaltype, 8);
			return bitBuilder.ToString();
		}
	}

	private const uint _Opcode = 3187u;

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

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

	public uint Opcode => 3187u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.MwsPatternIndex = 0u;
		UserFields.MwsPatternNumintervals = 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.MwsPatternIndex = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MwsPatternNumintervals = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.NumIntervalsList.Count; i++)
		{
			UserFields.NumIntervalsList[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.MwsPatternIndex, 8);
		bitBuilder.AddBitsAsUint(UserFields.MwsPatternNumintervals, 8);
		for (int i = 0; i < UserFields.NumIntervalsList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.NumIntervalsList[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();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.MwsPatternIndex < 0)
		{
			list.Add("Minimum allowed value for MWS_PATTERN_Index is 0x0");
		}
		if (UserFields.MwsPatternIndex > 2)
		{
			list.Add("Maximum allowed value for MWS_PATTERN_Index is 0x2");
		}
		return list;
	}
}
