using System.Collections.Generic;
using System.Linq;
using QCRPG.TSL.ZigBee.Packets;

namespace QCRPG.TSL.ZigBee;

public class ZigBeeRemoteDeviceInfo
{
	public struct ZigBeeRemoteInfo
	{
		public ulong shortAddress;

		public ulong extAddress;

		public DutaddressmodeEnum addressMode;

		public uint channelId;

		public uint panId;

		public bool isConnected;
	}

	public List<ZigBeeRemoteInfo> zigBeeRemoteDevices = new List<ZigBeeRemoteInfo>();

	public ZigBeeRemoteDeviceInfo()
	{
		zigBeeRemoteDevices.Clear();
	}

	public void UpdateZigBeeRemoteDeviceList(ZigBeeRemoteInfo zigBeeRemoteDevice)
	{
		int num = -1;
		foreach (ZigBeeRemoteInfo zigBeeRemoteDevice2 in zigBeeRemoteDevices)
		{
			if (zigBeeRemoteDevice.addressMode == DutaddressmodeEnum.ShortAddress_16_BIT && zigBeeRemoteDevice2.shortAddress == zigBeeRemoteDevice.shortAddress)
			{
				num = zigBeeRemoteDevices.IndexOf(zigBeeRemoteDevice2);
				break;
			}
			if (zigBeeRemoteDevice.addressMode == DutaddressmodeEnum.ExtendedAddress_64_BIT && zigBeeRemoteDevice2.extAddress == zigBeeRemoteDevice.extAddress)
			{
				num = zigBeeRemoteDevices.IndexOf(zigBeeRemoteDevice2);
				break;
			}
		}
		if (num != -1)
		{
			zigBeeRemoteDevices.RemoveAt(num);
			zigBeeRemoteDevices.Insert(num, zigBeeRemoteDevice);
		}
		else
		{
			zigBeeRemoteDevices.Add(zigBeeRemoteDevice);
		}
	}

	public void EmptyZigBeeRemoteDeviceList()
	{
		zigBeeRemoteDevices.Clear();
	}

	public void UpdateZigBeeRemoteDeviceList(DutaddressmodeEnum addressMode, ulong shortAddr, ulong extAddr, uint panId, uint channelId, bool isConnected = false)
	{
		ZigBeeRemoteInfo item = default(ZigBeeRemoteInfo);
		item.addressMode = addressMode;
		item.shortAddress = shortAddr;
		item.extAddress = extAddr;
		item.panId = panId;
		item.channelId = channelId;
		item.isConnected = isConnected;
		int num = -1;
		foreach (ZigBeeRemoteInfo zigBeeRemoteDevice in zigBeeRemoteDevices)
		{
			if ((zigBeeRemoteDevice.extAddress != 0 && zigBeeRemoteDevice.extAddress == item.extAddress) || (zigBeeRemoteDevice.shortAddress != 0 && zigBeeRemoteDevice.shortAddress == item.shortAddress))
			{
				num = zigBeeRemoteDevices.IndexOf(zigBeeRemoteDevice);
				break;
			}
		}
		if (num != -1)
		{
			item = zigBeeRemoteDevices.ElementAt(num);
			if (addressMode == DutaddressmodeEnum.ShortAddress_16_BIT)
			{
				item.shortAddress = shortAddr;
			}
			else
			{
				item.extAddress = extAddr;
			}
			item.addressMode = addressMode;
			if (channelId != 0)
			{
				item.channelId = channelId;
			}
			if (panId != 0)
			{
				item.panId = panId;
			}
			item.isConnected = isConnected;
			zigBeeRemoteDevices.RemoveAt(num);
			zigBeeRemoteDevices.Insert(num, item);
		}
		else
		{
			if (addressMode == DutaddressmodeEnum.ShortAddress_16_BIT)
			{
				item.shortAddress = shortAddr;
			}
			else
			{
				item.extAddress = extAddr;
			}
			item.addressMode = addressMode;
			item.channelId = channelId;
			item.panId = panId;
			item.isConnected = isConnected;
			zigBeeRemoteDevices.Add(item);
		}
	}

	public bool CheckAndGetZigBeeRemoteDeviceInfoByAddress(ulong address, out DutaddressmodeEnum addrMode, out ulong altAddress, out uint panId, out uint channelId)
	{
		addrMode = DutaddressmodeEnum.ShortAddress_16_BIT;
		panId = 0u;
		channelId = 0u;
		altAddress = 0uL;
		foreach (ZigBeeRemoteInfo zigBeeRemoteDevice in zigBeeRemoteDevices)
		{
			if (zigBeeRemoteDevice.shortAddress != 0 && zigBeeRemoteDevice.shortAddress == address)
			{
				addrMode = DutaddressmodeEnum.ShortAddress_16_BIT;
				if (zigBeeRemoteDevice.extAddress != 0)
				{
					altAddress = zigBeeRemoteDevice.extAddress;
				}
				panId = zigBeeRemoteDevice.panId;
				channelId = zigBeeRemoteDevice.channelId;
				return true;
			}
			if (zigBeeRemoteDevice.extAddress != 0 && zigBeeRemoteDevice.extAddress == address)
			{
				addrMode = DutaddressmodeEnum.ExtendedAddress_64_BIT;
				if (zigBeeRemoteDevice.shortAddress != 0)
				{
					altAddress = zigBeeRemoteDevice.shortAddress;
				}
				panId = zigBeeRemoteDevice.panId;
				channelId = zigBeeRemoteDevice.channelId;
				return true;
			}
		}
		return false;
	}

	public bool GetAssociatedDevice(out ulong address, out DutaddressmodeEnum addrMode, out uint panId, out uint channelId)
	{
		address = 0uL;
		addrMode = DutaddressmodeEnum.ShortAddress_16_BIT;
		panId = 0u;
		channelId = 0u;
		foreach (ZigBeeRemoteInfo zigBeeRemoteDevice in zigBeeRemoteDevices)
		{
			if (zigBeeRemoteDevice.isConnected)
			{
				if (zigBeeRemoteDevice.addressMode == DutaddressmodeEnum.ExtendedAddress_64_BIT)
				{
					address = zigBeeRemoteDevice.extAddress;
				}
				else
				{
					address = zigBeeRemoteDevice.shortAddress;
				}
				addrMode = zigBeeRemoteDevice.addressMode;
				panId = zigBeeRemoteDevice.panId;
				channelId = zigBeeRemoteDevice.channelId;
				return true;
			}
		}
		return false;
	}
}
