using System;
using System.Threading;

namespace QCRPG.Framework;

public class PacketAutoResponder : IObserver<PacketBase>
{
	private APIBase api;

	private PacketListBase packetList;

	private bool enabled;

	private int delayMs;

	public PacketAutoResponder(APIBase api, PacketListBase packetList)
	{
		this.api = api;
		this.packetList = packetList;
	}

	public void SetState(bool enabled)
	{
		this.enabled = enabled;
	}

	public void SetDelayMs(int delayMs)
	{
		this.delayMs = delayMs;
	}

	public void NotifyObserver(PacketBase packet)
	{
		if (enabled && packet.ExpectedResponseTypes != null)
		{
			Type[] expectedResponseTypes = packet.ExpectedResponseTypes;
			foreach (Type type in expectedResponseTypes)
			{
				Thread.Sleep(delayMs);
				PacketBase packetTypeFromList = PacketHelper.GetPacketTypeFromList(type, packetList.Packets);
				api.Send(packetTypeFromList);
			}
		}
	}
}
