using System.Collections.Generic;

namespace QCRPG.Framework;

public class DUTSpecificAPI : CommonAPI
{
	private List<APIBase> apiList;

	public DUTSpecificAPI(PacketListBase packetList, Logger logger, PacketReceiveMode packetReceiveMode, List<APIBase> apiList)
		: base(packetList, logger, packetReceiveMode)
	{
		protocolCommon = new ProtocolCommon();
		this.apiList = apiList;
		connectionSpecificInfo = new ConnectionSpecificProtocolInfo();
	}

	protected override void SetupReceiveDataObservation()
	{
		protocolCommon.RegisterReceiveDataObserverPostProcessing(receiveDataObserver);
	}

	protected override void ShutdownReceiveDataObservation()
	{
		protocolCommon.UnregisterReceiveDataObserverPostProcessing(receiveDataObserver);
	}

	public void StartUp()
	{
		Startup();
		ChangeBottomProtocol(protocolCommon);
		SetupReceiveDataObservation();
	}

	public new void Shutdown()
	{
		base.Shutdown();
		ShutdownReceiveDataObservation();
	}

	public override void SetSleepMode(ConnectionSpecificProtocolInfo.SleepModeState sleepMode)
	{
		connectionSpecificInfo = new ConnectionSpecificProtocolInfo();
		connectionSpecificInfo.SleepMode = sleepMode;
		protocolCommon.SetSleepMode(sleepMode);
		foreach (APIBase api in apiList)
		{
			if (api is CommonAPI)
			{
				((CommonAPI)api).SetSleepMode(sleepMode);
			}
		}
	}

	public bool SaveSnooploginfile(string fileName)
	{
		bool result = false;
		foreach (APIBase api in apiList)
		{
			if (api is CommonAPI && ((CommonAPI)api).SaveBTSnoopLogs(fileName))
			{
				result = true;
			}
		}
		return result;
	}

	public void ClearSnoopLog()
	{
		foreach (APIBase api in apiList)
		{
			if (api is CommonAPI)
			{
				((CommonAPI)api).ClearBTSnoopLogs();
			}
		}
	}

	public void SetSnoopLogMode(bool enabled)
	{
		foreach (APIBase api in apiList)
		{
			if (api is CommonAPI)
			{
				((CommonAPI)api).EnableBTSnoopLogging(enabled);
			}
		}
	}

	public void SetSnoopLiveLogMode(bool enable, string BTSnoopLiveImportDllFileName, string BTSnoopLiveImportIniFileName)
	{
		if (enable)
		{
			snoopLiveLog.EnableBTLiveLogging(BTSnoopLiveImportDllFileName, BTSnoopLiveImportIniFileName);
		}
		else
		{
			snoopLiveLog.DisableBTLiveLogging();
		}
		foreach (APIBase api in apiList)
		{
			if (api is CommonAPI)
			{
				((CommonAPI)api).EnableBTSnoopLiveLogging(enable, BTSnoopLiveImportDllFileName, BTSnoopLiveImportIniFileName);
			}
		}
	}
}
