using System;
using System.Collections.Generic;
using QCRPG.Framework;

namespace QCRPG.TSL.ZigBee;

public class ZigBeeReceivePacketHandler : ReceivePacketHandlerBase
{
	private List<PacketType> dataTypeList = new List<PacketType>();

	private List<PacketType> eventTypeList = new List<PacketType>();

	private List<PacketType> eventTypeListIndication = new List<PacketType>();

	private List<PacketType> commandTypeList = new List<PacketType>();

	public ZigBeeReceivePacketHandler(PacketListBase packetList)
		: base(packetList)
	{
		dataTypeList.Add(PacketType.Data);
		eventTypeList.Add(PacketType.Notification);
		eventTypeList.Add(PacketType.Response);
		eventTypeList.Add(PacketType.Tabi20Event);
		eventTypeListIndication.Add(PacketType.Indication);
		commandTypeList.Add(PacketType.ResponseAsCommand);
		commandTypeList.Add(PacketType.Command);
	}

	protected override PacketBase ConvertBytesToPacket(PacketData packetData)
	{
		if (packetData is ZigBeeIncomingPacketData)
		{
			if (((ZigBeeIncomingPacketData)packetData).ZigBeePacketType == ZigBeePacketDataType.Event)
			{
				return packetList.BytesToPacketOfType(packetData.bytes, eventTypeList);
			}
			if (((ZigBeeIncomingPacketData)packetData).ZigBeePacketType == ZigBeePacketDataType.Indication)
			{
				return packetList.BytesToPacketOfType(packetData.bytes, eventTypeListIndication);
			}
			if (((ZigBeeIncomingPacketData)packetData).ZigBeePacketType == ZigBeePacketDataType.Command)
			{
				return packetList.BytesToPacketOfType(packetData.bytes, commandTypeList);
			}
			if (((ZigBeeIncomingPacketData)packetData).ZigBeePacketType == ZigBeePacketDataType.Tabi20Event)
			{
				return packetList.BytesToPacketOfType(packetData.bytes, eventTypeList);
			}
			throw new Exception("Unsupported 15.4 packet type: " + ((ZigBeeOutgoingPacketData)packetData).ZigBeePacketType);
		}
		return packetList.BytesToPacket(packetData.bytes);
	}
}
