using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using QCRPG.Framework;

namespace QCRPG.TSL.BT;

public class DataTransferUserControlSelector : UserControlBase, QCRPG.Framework.IObserver<PacketBase>
{
	private Dictionary<int, DataTransferUserControl> linkAnDataTransferUserControls = new Dictionary<int, DataTransferUserControl>();

	private BTMainWorkerThread btMainWorkerThread;

	private Logger logger;

	private IContainer components;

	public DataTransferUserControlSelector(BTMainWorkerThread btMainWorkerThread, Logger logger)
		: base("Data Transfer")
	{
		InitializeComponent();
		this.btMainWorkerThread = btMainWorkerThread;
		this.logger = logger;
	}

	public bool IsConnectionAvailable(int connectionHandler)
	{
		return linkAnDataTransferUserControls.ContainsKey(connectionHandler);
	}

	public void CreateDataTransferControl(int connectionHandler, bool isBtConnection, string targetBdAddress)
	{
		if (base.InvokeRequired)
		{
			Invoke((Action)delegate
			{
				CreateDataTransferControl(connectionHandler, isBtConnection, targetBdAddress);
			});
		}
		else if (!linkAnDataTransferUserControls.ContainsKey(connectionHandler))
		{
			DataTransferUserControl dataTransferUserControl = new DataTransferUserControl(btMainWorkerThread, logger);
			dataTransferUserControl.ConnectionHandler = connectionHandler;
			dataTransferUserControl.IsBTConnection = isBtConnection;
			linkAnDataTransferUserControls[connectionHandler] = dataTransferUserControl;
			base.Controls.Add(linkAnDataTransferUserControls[connectionHandler]);
		}
	}

	public void RemoveDataTransferControl(int connectionHandler)
	{
		if (base.InvokeRequired)
		{
			Invoke((Action)delegate
			{
				RemoveDataTransferControl(connectionHandler);
			});
		}
		else if (linkAnDataTransferUserControls.ContainsKey(connectionHandler))
		{
			linkAnDataTransferUserControls[connectionHandler].StopTxTransferGUIUpdates();
			base.Controls.Remove(linkAnDataTransferUserControls[connectionHandler]);
			linkAnDataTransferUserControls.Remove(connectionHandler);
		}
	}

	public void SelectDataTransferControl(int connectionHandler, bool isBtConnection)
	{
		if (base.InvokeRequired)
		{
			Invoke((Action)delegate
			{
				SelectDataTransferControl(connectionHandler, isBtConnection);
			});
		}
		else
		{
			base.Controls.Clear();
			linkAnDataTransferUserControls[connectionHandler].IsBTConnection = isBtConnection;
			base.Controls.Add(linkAnDataTransferUserControls[connectionHandler]);
		}
	}

	public void ClearGui()
	{
		if (base.InvokeRequired)
		{
			Invoke((Action)delegate
			{
				ClearGui();
			});
		}
		else
		{
			base.Controls.Clear();
		}
	}

	public void NotifyObserver(PacketBase packetBase)
	{
		foreach (DataTransferUserControl value in linkAnDataTransferUserControls.Values)
		{
			value.NotifyObserver(packetBase);
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
	}
}
