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

namespace QCRPG.TSL.FMHCI.Packets;

public class SetLogConfiguration : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum LogEnum
		{
			DISABLED,
			ENABLED,
			InvalidLogValue
		}

		public static class LogInfo
		{
			public static LogEnum ValueToLog(uint value)
			{
				return value switch
				{
					0u => LogEnum.DISABLED, 
					1u => LogEnum.ENABLED, 
					_ => LogEnum.InvalidLogValue, 
				};
			}

			public static uint LogToValue(LogEnum enumItem)
			{
				return enumItem switch
				{
					LogEnum.DISABLED => 0u, 
					LogEnum.ENABLED => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LogToString(LogEnum enumItem)
			{
				return enumItem switch
				{
					LogEnum.DISABLED => "DISABLED", 
					LogEnum.ENABLED => "ENABLED", 
					_ => "Invalid Log Value", 
				};
			}

			public static string LogToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = DISABLED");
				stringBuilder.AppendLine("0x1 = ENABLED");
				return stringBuilder.ToString();
			}
		}

		public enum LogAudioEnum
		{
			DISABLED,
			ENABLED,
			InvalidLogAudioValue
		}

		public static class LogAudioInfo
		{
			public static LogAudioEnum ValueToLogAudio(uint value)
			{
				return value switch
				{
					0u => LogAudioEnum.DISABLED, 
					1u => LogAudioEnum.ENABLED, 
					_ => LogAudioEnum.InvalidLogAudioValue, 
				};
			}

			public static uint LogAudioToValue(LogAudioEnum enumItem)
			{
				return enumItem switch
				{
					LogAudioEnum.DISABLED => 0u, 
					LogAudioEnum.ENABLED => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LogAudioToString(LogAudioEnum enumItem)
			{
				return enumItem switch
				{
					LogAudioEnum.DISABLED => "DISABLED", 
					LogAudioEnum.ENABLED => "ENABLED", 
					_ => "Invalid LogAudio Value", 
				};
			}

			public static string LogAudioToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = DISABLED");
				stringBuilder.AppendLine("0x1 = ENABLED");
				return stringBuilder.ToString();
			}
		}

		private uint _Log;

		private uint _LogAudio;

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

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

		public LogEnum GetLogAsLogEnum()
		{
			return LogInfo.ValueToLog(_Log);
		}

		public void SetLogAsLogEnum(LogEnum enumItem)
		{
			Log = LogInfo.LogToValue(enumItem);
		}

		public LogAudioEnum GetLogAudioAsLogAudioEnum()
		{
			return LogAudioInfo.ValueToLogAudio(_LogAudio);
		}

		public void SetLogAudioAsLogAudioEnum(LogAudioEnum enumItem)
		{
			LogAudio = LogAudioInfo.LogAudioToValue(enumItem);
		}
	}

	private const uint _Opcode = 64522u;

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

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

	public uint Opcode => 64522u;

	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.Log = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LogAudio = bitBuilder.RemoveBitsAsUint(8);
		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.Log, 8);
		bitBuilder.AddBitsAsUint(UserFields.LogAudio, 8);
		_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.Log > 1)
		{
			list.Add("Maximum allowed value for Log is 0x1");
		}
		if (UserFields.LogAudio > 1)
		{
			list.Add("Maximum allowed value for Log Audio is 0x1");
		}
		return list;
	}
}
