using System.Windows.Forms;
using QCRPG.Framework;
using QCRPG.TSL.BT.Packets;

namespace QCRPG.TSL.BT;

public class BTBuilder : BuilderBase
{
	private BTMainWorkerThread btMainWorkerThread;

	private BTConfigSettings btConfigSettings = new BTConfigSettings();

	private PacketListBase packetListForGooier = new BTPacketList();

	private SpecificPacketNotifier specificPacketNotifier = new SpecificPacketNotifier();

	public SpecificPacketNotifier SpecificPacketNotifier => specificPacketNotifier;

	public override void BuildTSL(PacketReceiveMode packetReceiveMode, bool deviceEmulationMode = false, int dutNumber = 1, bool commandLineMode = false, bool automationMode = false)
	{
		base.deviceEmulationMode = deviceEmulationMode;
		name = "BT";
		dutName = "DUT" + dutNumber;
		logger = new Logger(name, dutName);
		if (commandLineMode)
		{
			packetList = new BTPacketList();
			api = new BTAPI(packetList, logger, packetReceiveMode, btConfigSettings);
			btMainWorkerThread = new BTMainWorkerThread(logger, (BTAPI)api, specificPacketNotifier);
			workerThreads.Add(btMainWorkerThread);
			return;
		}
		configurer = new Configurer();
		packetList = new BTPacketList();
		api = new BTAPI(packetList, logger, packetReceiveMode, btConfigSettings);
		btMainWorkerThread = new BTMainWorkerThread(logger, (BTAPI)api, specificPacketNotifier);
		packetAutoResponder = new PacketAutoResponder(api, packetListForGooier);
		scripter = new BTScripter((BTAPI)api, (BTPacketList)packetList);
		gooier = new BTGooier(name, logger, configurer, (BTAPI)api, packetListForGooier, btConfigSettings, btMainWorkerThread, deviceEmulationMode, deviceEmulationModeConfigSettings, packetAutoResponder, specificPacketNotifier, (BTScripter)scripter);
		if (!automationMode)
		{
			((BTAPI)api).RegisterReceivePacketObserver(specificPacketNotifier);
		}
		workerThreads.Add(btMainWorkerThread);
	}

	public override bool DeviceEmulationModeIsSupported()
	{
		return true;
	}

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

	public override void SetDeviceListView(ListView listView)
	{
		gooier.SetDeviceListView(listView);
	}

	public override void SetCombinedScripter(CombinedScripter combinedScripter)
	{
		((BTGooier)gooier).SetCombinedScripter(combinedScripter);
	}
}
