using QCRPG.Framework;
using QCRPG.TSL.FMHCI.Packets;

namespace QCRPG.TSL.FMHCI;

public class FMHCIBuilder : BuilderBase
{
	private FMHCIMainWorkerThread fmhciMainWorkerThread;

	private FMHCIConfigSettings fmhciConfigSettings = new FMHCIConfigSettings();

	private PacketListBase packetListForGooier = new FMHCIPacketList();

	public override void BuildTSL(PacketReceiveMode packetReceiveMode, bool deviceEmulationMode = false, int dutNumber = 1, bool commandLineMode = false, bool automationMode = false)
	{
		base.deviceEmulationMode = deviceEmulationMode;
		name = "FM HCI";
		dutName = "DUT" + dutNumber;
		logger = new Logger(name, dutName);
		if (commandLineMode)
		{
			packetList = new FMHCIPacketList();
			api = new FMHCIAPI(packetList, logger, packetReceiveMode, deviceEmulationMode);
			fmhciMainWorkerThread = new FMHCIMainWorkerThread(logger, (FMHCIAPI)api);
			workerThreads.Add(fmhciMainWorkerThread);
			return;
		}
		configurer = new Configurer();
		packetList = new FMHCIPacketList();
		api = new FMHCIAPI(packetList, logger, packetReceiveMode, deviceEmulationMode);
		fmhciMainWorkerThread = new FMHCIMainWorkerThread(logger, (FMHCIAPI)api);
		packetAutoResponder = new PacketAutoResponder(api, packetListForGooier);
		gooier = new FMHCIGooier(name, logger, configurer, (FMHCIAPI)api, packetListForGooier, fmhciConfigSettings, fmhciMainWorkerThread, deviceEmulationMode, deviceEmulationModeConfigSettings, packetAutoResponder);
		scripter = new FMHCIScripter((FMHCIAPI)api, (FMHCIPacketList)packetList);
		workerThreads.Add(fmhciMainWorkerThread);
	}

	public override bool DeviceEmulationModeIsSupported()
	{
		return true;
	}

	public override void ApplicationReady()
	{
		((FMHCIGooier)gooier).ApplicationReady();
		api.RegisterReceivePacketObserver(packetAutoResponder);
	}
}
