using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace QCRPG.Framework;

public abstract class GooierBase
{
	protected readonly string technologyName;

	protected readonly int mainWindowHandle;

	private string titleBarMessage;

	protected readonly Logger logger;

	protected readonly List<UserControlBase> userControls = new List<UserControlBase>();

	protected readonly ToolStripMenuItem dropDownMenu = new ToolStripMenuItem();

	protected readonly List<ToolStripItem> customToolStripItems = new List<ToolStripItem>();

	protected readonly List<ToolStripStatusLabel> customToolStripStatusLabels = new List<ToolStripStatusLabel>();

	protected Control invokeControl;

	private bool running;

	private bool ranOnce;

	public string TechnologyName => technologyName;

	public string TitleBarMessage
	{
		get
		{
			return titleBarMessage;
		}
		set
		{
			titleBarMessage = value;
		}
	}

	public List<UserControlBase> UserControls => userControls;

	public ToolStripMenuItem DropDownMenu => dropDownMenu;

	public List<ToolStripItem> CustomToolStripItems => customToolStripItems;

	public List<ToolStripStatusLabel> CustomToolStripStatusLabels => customToolStripStatusLabels;

	public Control InvokeControl
	{
		set
		{
			invokeControl = value;
		}
	}

	[DllImport("user32.dll")]
	public static extern bool SetWindowText(IntPtr hWnd, string lpString);

	[DllImport("user32.dll")]
	public static extern int GetActiveWindow();

	public GooierBase(string technologyName, Logger logger)
	{
		this.technologyName = technologyName;
		this.logger = logger;
		mainWindowHandle = GetActiveWindow();
	}

	public void Startup()
	{
		if (!running)
		{
			if (!ranOnce)
			{
				AddUserControls();
				BuildDropDownMenu();
				BuildCustomToolStrip();
				BuildCustomStatusStrip();
				StartupComplete();
				ranOnce = true;
			}
			else
			{
				SetControlState(enabled: true);
			}
			running = true;
		}
	}

	protected virtual void AddUserControls()
	{
	}

	protected virtual void BuildDropDownMenu()
	{
	}

	protected virtual void BuildCustomToolStrip()
	{
	}

	protected virtual void BuildCustomStatusStrip()
	{
	}

	protected virtual void BuildCustomDeviceList(ListView deviceListView)
	{
	}

	protected virtual void StartupComplete()
	{
	}

	public virtual void UpdateStatusBar(PacketBase packet)
	{
	}

	public virtual void UpdateStatusBar(IncomingPacketData incomingPacketData)
	{
	}

	public virtual void UpdateStatusBar(OutgoingPacketData outgoingPacketData)
	{
	}

	public virtual void UpdateTitleBar()
	{
	}

	public void Shutdown()
	{
		if (running)
		{
			SetControlState(enabled: false);
			running = false;
		}
	}

	private void SetControlState(bool enabled)
	{
		foreach (UserControlBase userControl in userControls)
		{
			userControl.Enabled = enabled;
		}
		if (dropDownMenu != null)
		{
			dropDownMenu.Enabled = enabled;
		}
		foreach (ToolStripItem customToolStripItem in customToolStripItems)
		{
			customToolStripItem.Enabled = enabled;
		}
		foreach (ToolStripStatusLabel customToolStripStatusLabel in customToolStripStatusLabels)
		{
			customToolStripStatusLabel.Enabled = enabled;
		}
	}

	public virtual void SetDeviceListView(ListView listView)
	{
	}
}
