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

namespace QCRPG.TSL.ZigBee;

public class ZigBeeBuilder : BuilderBase
{
	private ZigBeeMainWorkerThread ZigBeeMainWorkerThread;

	private ZigBeeConfigSettings ZigBeeConfigSettings = new ZigBeeConfigSettings();

	private SpecificPacketNotifier specificPacketNotifier = new SpecificPacketNotifier();

	private PacketListBase packetListForGooier = new ZigBeePacketList();

	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 = "15.4";
		dutName = "DUT" + dutNumber;
		logger = new Logger(name, dutName);
		if (commandLineMode)
		{
			packetList = new ZigBeePacketList();
			api = new ZigBeeAPI(packetList, logger, packetReceiveMode, deviceEmulationMode);
			ZigBeeMainWorkerThread = new ZigBeeMainWorkerThread(logger, (ZigBeeAPI)api);
			((ZigBeeAPI)api).RegisterReceivePacketObserver(specificPacketNotifier);
			workerThreads.Add(ZigBeeMainWorkerThread);
			return;
		}
		configurer = new Configurer();
		packetList = new ZigBeePacketList();
		api = new ZigBeeAPI(packetList, logger, packetReceiveMode, deviceEmulationMode);
		ZigBeeMainWorkerThread = new ZigBeeMainWorkerThread(logger, (ZigBeeAPI)api);
		packetAutoResponder = new PacketAutoResponder(api, packetListForGooier);
		gooier = new ZigBeeGooier(name, logger, configurer, (ZigBeeAPI)api, packetListForGooier, ZigBeeConfigSettings, ZigBeeMainWorkerThread, deviceEmulationMode, deviceEmulationModeConfigSettings, packetAutoResponder, specificPacketNotifier);
		if (!automationMode)
		{
			((ZigBeeAPI)api).RegisterReceivePacketObserver(specificPacketNotifier);
			((ZigBeeAPI)api).RegisterSendPacketObserver(specificPacketNotifier);
		}
		workerThreads.Add(ZigBeeMainWorkerThread);
		scripter = new ZigBeeScripter((ZigBeeAPI)api, (ZigBeePacketList)packetList);
	}

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

	public override bool DeviceEmulationModeIsSupported()
	{
		return true;
	}

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