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

namespace QCRPG.TSL.FMHCI.Packets;

public class SetGetResetAgc : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum UcctrlEnum
		{
			SET_AGC_GAINSTATE,
			GET_AGC_GAINSTATE,
			RESET_AGC,
			InvalidUcctrlValue
		}

		public static class UcctrlInfo
		{
			public static UcctrlEnum ValueToUcctrl(uint value)
			{
				return value switch
				{
					0u => UcctrlEnum.SET_AGC_GAINSTATE, 
					1u => UcctrlEnum.GET_AGC_GAINSTATE, 
					2u => UcctrlEnum.RESET_AGC, 
					_ => UcctrlEnum.InvalidUcctrlValue, 
				};
			}

			public static uint UcctrlToValue(UcctrlEnum enumItem)
			{
				return enumItem switch
				{
					UcctrlEnum.SET_AGC_GAINSTATE => 0u, 
					UcctrlEnum.GET_AGC_GAINSTATE => 1u, 
					UcctrlEnum.RESET_AGC => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string UcctrlToString(UcctrlEnum enumItem)
			{
				return enumItem switch
				{
					UcctrlEnum.SET_AGC_GAINSTATE => "Set AGC Gain State with ucGainState value", 
					UcctrlEnum.GET_AGC_GAINSTATE => "Get Current AGC gain State and past 3 gain state changes", 
					UcctrlEnum.RESET_AGC => "Reset AGC", 
					_ => "Invalid Ucctrl Value", 
				};
			}

			public static string UcctrlToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Set AGC Gain State with ucGainState value");
				stringBuilder.AppendLine("0x1 = Get Current AGC gain State and past 3 gain state changes");
				stringBuilder.AppendLine("0x2 = Reset AGC");
				return stringBuilder.ToString();
			}
		}

		public enum UcgainstateEnum
		{
			G0_HIGH,
			G1_MIDPLUS,
			G2_MID,
			G3_MIDMIN,
			G4_LOW,
			AGC_FREERUN,
			InvalidUcgainstateValue
		}

		public static class UcgainstateInfo
		{
			public static UcgainstateEnum ValueToUcgainstate(uint value)
			{
				return value switch
				{
					0u => UcgainstateEnum.G0_HIGH, 
					1u => UcgainstateEnum.G1_MIDPLUS, 
					2u => UcgainstateEnum.G2_MID, 
					3u => UcgainstateEnum.G3_MIDMIN, 
					4u => UcgainstateEnum.G4_LOW, 
					5u => UcgainstateEnum.AGC_FREERUN, 
					_ => UcgainstateEnum.InvalidUcgainstateValue, 
				};
			}

			public static uint UcgainstateToValue(UcgainstateEnum enumItem)
			{
				return enumItem switch
				{
					UcgainstateEnum.G0_HIGH => 0u, 
					UcgainstateEnum.G1_MIDPLUS => 1u, 
					UcgainstateEnum.G2_MID => 2u, 
					UcgainstateEnum.G3_MIDMIN => 3u, 
					UcgainstateEnum.G4_LOW => 4u, 
					UcgainstateEnum.AGC_FREERUN => 5u, 
					_ => uint.MaxValue, 
				};
			}

			public static string UcgainstateToString(UcgainstateEnum enumItem)
			{
				return enumItem switch
				{
					UcgainstateEnum.G0_HIGH => "G0 state (HIGH)", 
					UcgainstateEnum.G1_MIDPLUS => "G1 state (MIDPLUS)", 
					UcgainstateEnum.G2_MID => "G2 state (MID)", 
					UcgainstateEnum.G3_MIDMIN => "G3 state (MIDMIN)", 
					UcgainstateEnum.G4_LOW => "G4 state (LOW)", 
					UcgainstateEnum.AGC_FREERUN => "AGC Free Run", 
					_ => "Invalid Ucgainstate Value", 
				};
			}

			public static string UcgainstateToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = G0 state (HIGH)");
				stringBuilder.AppendLine("0x1 = G1 state (MIDPLUS)");
				stringBuilder.AppendLine("0x2 = G2 state (MID)");
				stringBuilder.AppendLine("0x3 = G3 state (MIDMIN)");
				stringBuilder.AppendLine("0x4 = G4 state (LOW)");
				stringBuilder.AppendLine("0x5 = AGC Free Run");
				return stringBuilder.ToString();
			}
		}

		private uint _Ucctrl;

		private uint _Ucgainstate;

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

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

		public UcctrlEnum GetUcctrlAsUcctrlEnum()
		{
			return UcctrlInfo.ValueToUcctrl(_Ucctrl);
		}

		public void SetUcctrlAsUcctrlEnum(UcctrlEnum enumItem)
		{
			Ucctrl = UcctrlInfo.UcctrlToValue(enumItem);
		}

		public UcgainstateEnum GetUcgainstateAsUcgainstateEnum()
		{
			return UcgainstateInfo.ValueToUcgainstate(_Ucgainstate);
		}

		public void SetUcgainstateAsUcgainstateEnum(UcgainstateEnum enumItem)
		{
			Ucgainstate = UcgainstateInfo.UcgainstateToValue(enumItem);
		}
	}

	private const uint _Opcode = 64525u;

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

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

	public uint Opcode => 64525u;

	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.Ucctrl = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Ucgainstate = 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.Ucctrl, 8);
		bitBuilder.AddBitsAsUint(UserFields.Ucgainstate, 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.Ucctrl > 2)
		{
			list.Add("Maximum allowed value for ucCtrl is 0x2");
		}
		if (UserFields.Ucgainstate > 5)
		{
			list.Add("Maximum allowed value for ucGainState is 0x5");
		}
		return list;
	}
}
