using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.FM.Packets;

public class StartSearchUsingPi : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SearchControlEnum
		{
			STOP_SEARCH,
			START_SEARCH,
			InvalidSearchControlValue
		}

		public static class SearchControlInfo
		{
			public static SearchControlEnum ValueToSearchControl(uint value)
			{
				return value switch
				{
					0u => SearchControlEnum.STOP_SEARCH, 
					1u => SearchControlEnum.START_SEARCH, 
					_ => SearchControlEnum.InvalidSearchControlValue, 
				};
			}

			public static uint SearchControlToValue(SearchControlEnum enumItem)
			{
				return enumItem switch
				{
					SearchControlEnum.STOP_SEARCH => 0u, 
					SearchControlEnum.START_SEARCH => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SearchControlToString(SearchControlEnum enumItem)
			{
				return enumItem switch
				{
					SearchControlEnum.STOP_SEARCH => "STOP_SEARCH", 
					SearchControlEnum.START_SEARCH => "START_SEARCH", 
					_ => "Invalid SearchControl Value", 
				};
			}

			public static string SearchControlToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = STOP_SEARCH");
				stringBuilder.AppendLine("0x1 = START_SEARCH");
				return stringBuilder.ToString();
			}
		}

		public enum SearchDirectionEnum
		{
			SEARCH_UP,
			SEARCH_DOWN,
			InvalidSearchDirectionValue
		}

		public static class SearchDirectionInfo
		{
			public static SearchDirectionEnum ValueToSearchDirection(uint value)
			{
				return value switch
				{
					0u => SearchDirectionEnum.SEARCH_UP, 
					1u => SearchDirectionEnum.SEARCH_DOWN, 
					_ => SearchDirectionEnum.InvalidSearchDirectionValue, 
				};
			}

			public static uint SearchDirectionToValue(SearchDirectionEnum enumItem)
			{
				return enumItem switch
				{
					SearchDirectionEnum.SEARCH_UP => 0u, 
					SearchDirectionEnum.SEARCH_DOWN => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SearchDirectionToString(SearchDirectionEnum enumItem)
			{
				return enumItem switch
				{
					SearchDirectionEnum.SEARCH_UP => "SEARCH_UP", 
					SearchDirectionEnum.SEARCH_DOWN => "SEARCH_DOWN", 
					_ => "Invalid SearchDirection Value", 
				};
			}

			public static string SearchDirectionToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = SEARCH_UP");
				stringBuilder.AppendLine("0x1 = SEARCH_DOWN");
				return stringBuilder.ToString();
			}
		}

		private const uint _RegisterAddress = 6u;

		private const uint _SearchControl = 1u;

		private const uint _SearchMode = 6u;

		private uint _ProgramIdentification;

		private uint _ScanTime;

		private uint _SearchDirection;

		public uint RegisterAddress => 6u;

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

		public uint SearchControl => 1u;

		public uint ScanTime
		{
			get
			{
				return _ScanTime;
			}
			set
			{
				if (value > 7)
				{
					throw new Exception("Maximum possible value for ScanTime is 0x7");
				}
				_ScanTime = value;
			}
		}

		public uint SearchDirection
		{
			get
			{
				return _SearchDirection;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for SearchDirection is 0x1");
				}
				_SearchDirection = value;
			}
		}

		public uint SearchMode => 6u;

		public RegisterMapEnum GetRegisterAddressAsRegisterMapEnum()
		{
			return RegisterMapInfo.ValueToRegisterMap(6u);
		}

		public SearchControlEnum GetSearchControlAsSearchControlEnum()
		{
			return SearchControlInfo.ValueToSearchControl(1u);
		}

		public SearchDirectionEnum GetSearchDirectionAsSearchDirectionEnum()
		{
			return SearchDirectionInfo.ValueToSearchDirection(_SearchDirection);
		}

		public void SetSearchDirectionAsSearchDirectionEnum(SearchDirectionEnum enumItem)
		{
			SearchDirection = SearchDirectionInfo.SearchDirectionToValue(enumItem);
		}
	}

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

	private PacketInfoBase info;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.SearchDirection = 1u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.RegisterAddress)
		{
			return false;
		}
		UserFields.ProgramIdentification = bitBuilder.RemoveBitsAsUint(16);
		if (bitBuilder.RemoveBitsAsUint(1) != UserFields.SearchControl)
		{
			return false;
		}
		UserFields.ScanTime = bitBuilder.RemoveBitsAsUint(3);
		UserFields.SearchDirection = bitBuilder.RemoveBitsAsUint(1);
		if (bitBuilder.RemoveBitsAsUint(3) != UserFields.SearchMode)
		{
			return false;
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(UserFields.RegisterAddress, 8);
		bitBuilder.AddBitsAsUint(UserFields.ProgramIdentification, 16);
		bitBuilder.AddBitsAsUint(UserFields.SearchControl, 1);
		bitBuilder.AddBitsAsUint(UserFields.ScanTime, 3);
		bitBuilder.AddBitsAsUint(UserFields.SearchDirection, 1);
		bitBuilder.AddBitsAsUint(UserFields.SearchMode, 3);
		return bitBuilder.ToString();
	}
}
