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

namespace QCRPG.TSL.BT.Packets;

public class GenericEnhancedLog : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum LogIdEnum
		{
			SLC_LOG,
			DEBUG_LOG,
			InvalidLogIdValue
		}

		public static class LogIdInfo
		{
			public static LogIdEnum ValueToLogId(uint value)
			{
				if (value == 0)
				{
					return LogIdEnum.SLC_LOG;
				}
				if (value >= 1 && value <= 65535)
				{
					return LogIdEnum.DEBUG_LOG;
				}
				return LogIdEnum.InvalidLogIdValue;
			}

			public static uint LogIdToValue(LogIdEnum enumItem)
			{
				return enumItem switch
				{
					LogIdEnum.SLC_LOG => 0u, 
					LogIdEnum.DEBUG_LOG => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LogIdToString(LogIdEnum enumItem)
			{
				return enumItem switch
				{
					LogIdEnum.SLC_LOG => "SLC_LOG", 
					LogIdEnum.DEBUG_LOG => "DEBUG_LOG", 
					_ => "Invalid LogId Value", 
				};
			}

			public static string LogIdToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = SLC_LOG");
				stringBuilder.AppendLine("0x1-0xFFFF = DEBUG_LOG");
				return stringBuilder.ToString();
			}
		}

		private uint _SeqNum;

		private uint _LogsLostDrop;

		private uint _ProdId;

		private uint _LogId;

		private uint _LogFormatLevel;

		private uint _Timestamp;

		private uint _PayloadLength;

		private List<uint> _PduList = new List<uint>();

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

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

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

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

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

		public uint Timestamp
		{
			get
			{
				return _Timestamp;
			}
			set
			{
				_Timestamp = value;
			}
		}

		public uint PayloadLength
		{
			get
			{
				return _PayloadLength;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for PayloadLength is 0xFFFF");
				}
				_PayloadLength = value;
				while (_PduList.Count < _PayloadLength)
				{
					_PduList.Add(0u);
				}
				while (_PduList.Count > _PayloadLength)
				{
					_PduList.RemoveAt(_PduList.Count - 1);
				}
			}
		}

		public List<uint> PduList => _PduList;

		public LogIdEnum GetLogIdAsLogIdEnum()
		{
			return LogIdInfo.ValueToLogId(_LogId);
		}

		public void SetLogIdAsLogIdEnum(LogIdEnum enumItem)
		{
			LogId = LogIdInfo.LogIdToValue(enumItem);
		}
	}

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _AclheaderSeqnumPadding;

	private uint _LogformatlevelTimestampPadding;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.EnhancedLog;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new GenericEnhancedLogInfo(this);
			}
			return info;
		}
	}

	public uint AclheaderSeqnumPadding
	{
		get
		{
			return _AclheaderSeqnumPadding;
		}
		set
		{
			if (value > 16777215)
			{
				throw new Exception("Maximum possible value for AclheaderSeqnumPadding is 0xFFFFFF");
			}
			_AclheaderSeqnumPadding = value;
		}
	}

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		AclheaderSeqnumPadding = bitBuilder.RemoveBitsAsUint(24);
		UserFields.SeqNum = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LogsLostDrop = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ProdId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LogId = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.LogFormatLevel = bitBuilder.RemoveBitsAsUint(8);
		LogformatlevelTimestampPadding = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Timestamp = bitBuilder.RemoveBitsAsUint(32);
		UserFields.PayloadLength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		for (int i = 0; i < UserFields.PduList.Count; i++)
		{
			UserFields.PduList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(AclheaderSeqnumPadding, 24);
		bitBuilder.AddBitsAsUint(UserFields.SeqNum, 8);
		bitBuilder.AddBitsAsUint(UserFields.LogsLostDrop, 8);
		bitBuilder.AddBitsAsUint(UserFields.ProdId, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.LogId, 16);
		bitBuilder.AddBitsAsUint(UserFields.LogFormatLevel, 8);
		bitBuilder.AddBitsAsUint(LogformatlevelTimestampPadding, 8);
		bitBuilder.AddBitsAsUint(UserFields.Timestamp, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PayloadLength, 16);
		for (int i = 0; i < UserFields.PduList.Count; i++)
		{
			if (UserFields.PduList[i] > 255)
			{
				throw new Exception("UserFields.PduList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.PduList[i], 8);
		}
		return bitBuilder.ToString();
	}
}
