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

namespace QCRPG.TSL.BT.Packets;

public class WriteBestEffortFlushTimeout : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum BestEffortFlushTimeoutEnum
		{
			Best_Effort_Flush_Timeout_value_in_microseconds,
			No_Best_Effort_Flush_Timeout_used,
			InvalidBestEffortFlushTimeoutValue
		}

		public static class BestEffortFlushTimeoutInfo
		{
			public static BestEffortFlushTimeoutEnum ValueToBestEffortFlushTimeout(uint value)
			{
				if (value >= 0 && value <= 4294967294u)
				{
					return BestEffortFlushTimeoutEnum.Best_Effort_Flush_Timeout_value_in_microseconds;
				}
				if (value == uint.MaxValue)
				{
					return BestEffortFlushTimeoutEnum.No_Best_Effort_Flush_Timeout_used;
				}
				return BestEffortFlushTimeoutEnum.InvalidBestEffortFlushTimeoutValue;
			}

			public static uint BestEffortFlushTimeoutToValue(BestEffortFlushTimeoutEnum enumItem)
			{
				return enumItem switch
				{
					BestEffortFlushTimeoutEnum.Best_Effort_Flush_Timeout_value_in_microseconds => 0u, 
					BestEffortFlushTimeoutEnum.No_Best_Effort_Flush_Timeout_used => uint.MaxValue, 
					_ => uint.MaxValue, 
				};
			}

			public static string BestEffortFlushTimeoutToString(BestEffortFlushTimeoutEnum enumItem)
			{
				return enumItem switch
				{
					BestEffortFlushTimeoutEnum.Best_Effort_Flush_Timeout_value_in_microseconds => "Best Effort Flush Timeout value in microseconds", 
					BestEffortFlushTimeoutEnum.No_Best_Effort_Flush_Timeout_used => "No Best Effort Flush Timeout used (default)", 
					_ => "Invalid BestEffortFlushTimeout Value", 
				};
			}

			public static string BestEffortFlushTimeoutToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0-0xFFFFFFFE = Best Effort Flush Timeout value in microseconds");
				stringBuilder.AppendLine("0xFFFFFFFF = No Best Effort Flush Timeout used (default)");
				return stringBuilder.ToString();
			}
		}

		private uint _LogicalLinkHandle;

		private uint _BestEffortFlushTimeout;

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

		public uint BestEffortFlushTimeout
		{
			get
			{
				return _BestEffortFlushTimeout;
			}
			set
			{
				_BestEffortFlushTimeout = value;
			}
		}

		public BestEffortFlushTimeoutEnum GetBestEffortFlushTimeoutAsBestEffortFlushTimeoutEnum()
		{
			return BestEffortFlushTimeoutInfo.ValueToBestEffortFlushTimeout(_BestEffortFlushTimeout);
		}

		public void SetBestEffortFlushTimeoutAsBestEffortFlushTimeoutEnum(BestEffortFlushTimeoutEnum enumItem)
		{
			BestEffortFlushTimeout = BestEffortFlushTimeoutInfo.BestEffortFlushTimeoutToValue(enumItem);
		}
	}

	private const uint _Opcode = 3178u;

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

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

	public uint Opcode => 3178u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.LogicalLinkHandle = 0u;
		UserFields.BestEffortFlushTimeout = uint.MaxValue;
	}

	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.LogicalLinkHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.BestEffortFlushTimeout = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		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.LogicalLinkHandle, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.BestEffortFlushTimeout, 32);
		_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.LogicalLinkHandle < 0)
		{
			list.Add("Minimum allowed value for Logical_Link_Handle is 0x0");
		}
		if (UserFields.LogicalLinkHandle > 3839)
		{
			list.Add("Maximum allowed value for Logical_Link_Handle is 0xEFF");
		}
		return list;
	}
}
