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

namespace QCRPG.TSL.BT.Packets;

public class SetExternalFrameConfiguration : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _ExtFrameDuration;

		private uint _ExtFrameSyncAssertOffset;

		private uint _ExtFrameSyncAssertJitter;

		private uint _ExtNumPeriods;

		private List<NumPeriodSubField> _NumPeriodList = new List<NumPeriodSubField>();

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

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

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

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

		public List<NumPeriodSubField> NumPeriodList => _NumPeriodList;
	}

	public class NumPeriodSubField
	{
		public enum PeriodTypeEnum
		{
			Downlink,
			Uplink,
			Bi_Directional,
			Guard_Period,
			Reserved,
			InvalidPeriodTypeValue
		}

		public static class PeriodTypeInfo
		{
			public static PeriodTypeEnum ValueToPeriodType(uint value)
			{
				switch (value)
				{
				case 0u:
					return PeriodTypeEnum.Downlink;
				case 1u:
					return PeriodTypeEnum.Uplink;
				case 2u:
					return PeriodTypeEnum.Bi_Directional;
				case 3u:
					return PeriodTypeEnum.Guard_Period;
				default:
					if (value >= 4 && value <= 255)
					{
						return PeriodTypeEnum.Reserved;
					}
					return PeriodTypeEnum.InvalidPeriodTypeValue;
				}
			}

			public static uint PeriodTypeToValue(PeriodTypeEnum enumItem)
			{
				return enumItem switch
				{
					PeriodTypeEnum.Downlink => 0u, 
					PeriodTypeEnum.Uplink => 1u, 
					PeriodTypeEnum.Bi_Directional => 2u, 
					PeriodTypeEnum.Guard_Period => 3u, 
					PeriodTypeEnum.Reserved => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PeriodTypeToString(PeriodTypeEnum enumItem)
			{
				return enumItem switch
				{
					PeriodTypeEnum.Downlink => "Downlink", 
					PeriodTypeEnum.Uplink => "Uplink", 
					PeriodTypeEnum.Bi_Directional => "Bi_Directional", 
					PeriodTypeEnum.Guard_Period => "Guard_Period", 
					PeriodTypeEnum.Reserved => "Reserved", 
					_ => "Invalid PeriodType Value", 
				};
			}

			public static string PeriodTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Downlink");
				stringBuilder.AppendLine("0x1 = Uplink");
				stringBuilder.AppendLine("0x2 = Bi_Directional");
				stringBuilder.AppendLine("0x3 = Guard_Period");
				stringBuilder.AppendLine("0x4-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		private uint _PeriodDuration;

		private uint _PeriodType;

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

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

		public void SetDefaultValues()
		{
			PeriodType = 0u;
		}

		public PeriodTypeEnum GetPeriodTypeAsPeriodTypeEnum()
		{
			return PeriodTypeInfo.ValueToPeriodType(_PeriodType);
		}

		public void SetPeriodTypeAsPeriodTypeEnum(PeriodTypeEnum enumItem)
		{
			PeriodType = PeriodTypeInfo.PeriodTypeToValue(enumItem);
		}

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

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

	private const uint _Opcode = 3183u;

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

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

	public uint Opcode => 3183u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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.ExtFrameDuration = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ExtFrameSyncAssertOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ExtFrameSyncAssertJitter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ExtNumPeriods = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.NumPeriodList.Count; i++)
		{
			UserFields.NumPeriodList[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.AddBitsAsUintLittleEndian(UserFields.ExtFrameDuration, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ExtFrameSyncAssertOffset, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ExtFrameSyncAssertJitter, 16);
		bitBuilder.AddBitsAsUint(UserFields.ExtNumPeriods, 8);
		for (int i = 0; i < UserFields.NumPeriodList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.NumPeriodList[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();
	}
}
