using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace QDART_MFG_CfgEditor.QualCtrl;

public class QualTreeView : TreeView
{
	public delegate void AutoCheckEventHandler(object sender, TreeViewEventArgs e);

	private ImageList _ilStateImages;

	private bool _bPreventCheckEvent;

	private bool _bFirstInit;

	private bool _bEditableSubNodes;

	public event AutoCheckEventHandler AutoCheck;

	public QualTreeView()
	{
		_ilStateImages = new ImageList();
		CheckBoxState checkBoxState = CheckBoxState.UncheckedNormal;
		for (int i = 0; i <= 2; i++)
		{
			Bitmap bitmap = new Bitmap(16, 16);
			Graphics graphics = Graphics.FromImage(bitmap);
			switch (i)
			{
			case 0:
				checkBoxState = CheckBoxState.UncheckedNormal;
				break;
			case 1:
				checkBoxState = CheckBoxState.CheckedNormal;
				break;
			case 2:
				checkBoxState = CheckBoxState.MixedNormal;
				break;
			}
			CheckBoxRenderer.DrawCheckBox(graphics, new Point(2, 2), checkBoxState);
			graphics.Save();
			_ilStateImages.Images.Add(bitmap);
		}
		base.CheckBoxes = true;
		base.StateImageList = _ilStateImages;
		_bEditableSubNodes = true;
	}

	protected void SetParentState(TreeNode tNode)
	{
		TreeNode treeNode = tNode.Parent;
		if (treeNode == null)
		{
			return;
		}
		try
		{
			if (tNode.StateImageIndex == 2)
			{
				treeNode.Checked = false;
				treeNode.StateImageIndex = 2;
				return;
			}
			int num = 0;
			int num2 = 0;
			foreach (TreeNode node in treeNode.Nodes)
			{
				if (node.StateImageIndex <= 0)
				{
					num2++;
				}
				else if (node.StateImageIndex == 1)
				{
					num++;
				}
				if (node.StateImageIndex == 2 || (num > 0 && num2 > 0))
				{
					treeNode.Checked = false;
					treeNode.StateImageIndex = 2;
					return;
				}
			}
			if (num2 > 0)
			{
				treeNode.Checked = false;
				treeNode.StateImageIndex = 0;
			}
			else if (num > 0)
			{
				treeNode.Checked = true;
				treeNode.StateImageIndex = 1;
			}
		}
		finally
		{
			SetParentState(treeNode);
		}
	}

	protected void SetChildrenState(TreeNode tNode, bool RootNode)
	{
		if (!RootNode)
		{
			tNode.Checked = tNode.Parent.StateImageIndex == 1;
			tNode.StateImageIndex = tNode.Parent.StateImageIndex;
		}
		foreach (TreeNode node in tNode.Nodes)
		{
			SetChildrenState(node, RootNode: false);
		}
	}

	public void SetState(TreeNode tNode, int NewState)
	{
		if (NewState < 0 || NewState > 2)
		{
			NewState = 0;
		}
		tNode.Checked = NewState == 1;
		if (tNode.Checked == (NewState == 1))
		{
			tNode.StateImageIndex = NewState;
			_bPreventCheckEvent = true;
			SetParentState(tNode);
			SetChildrenState(tNode, RootNode: true);
			_bPreventCheckEvent = false;
		}
	}

	public void NoEventWhenInit(bool bfirstInit)
	{
		_bFirstInit = bfirstInit;
	}

	public void EditableSubNodes(bool editable)
	{
		_bEditableSubNodes = editable;
	}

	public void InitializeStates(TreeNodeCollection tNodes)
	{
		foreach (TreeNode tNode in tNodes)
		{
			if (tNode.StateImageIndex == -1)
			{
				_bPreventCheckEvent = true;
				if (tNode.Parent != null)
				{
					tNode.Checked = tNode.Parent.Checked;
					tNode.StateImageIndex = tNode.Parent.StateImageIndex;
				}
				else
				{
					tNode.StateImageIndex = (tNode.Checked ? 1 : 0);
				}
				_bPreventCheckEvent = false;
			}
			InitializeStates(tNode.Nodes);
		}
	}

	public void InitializeCBImages()
	{
		if (base.CheckBoxes)
		{
			base.CheckBoxes = false;
			InitializeStates(base.Nodes);
		}
	}

	public override void Refresh()
	{
		base.Refresh();
		InitializeCBImages();
	}

	protected override void OnLayout(LayoutEventArgs levent)
	{
		base.OnLayout(levent);
		InitializeCBImages();
	}

	protected override void OnAfterExpand(TreeViewEventArgs e)
	{
		if (base.CheckBoxes)
		{
			InitializeStates(e.Node.Nodes);
		}
		if (!_bEditableSubNodes)
		{
			foreach (TreeNode node in e.Node.Nodes)
			{
				node.BackColor = Color.LightGray;
			}
		}
		else
		{
			foreach (TreeNode node2 in e.Node.Nodes)
			{
				node2.BackColor = Color.Empty;
			}
		}
		base.OnAfterExpand(e);
	}

	protected override void OnBeforeCheck(TreeViewCancelEventArgs e)
	{
		if (e.Node.BackColor == Color.LightGray)
		{
			e.Cancel = true;
			base.OnBeforeCheck(e);
		}
		if (!_bPreventCheckEvent)
		{
			base.OnBeforeCheck(e);
		}
	}

	protected override void OnAfterCheck(TreeViewEventArgs e)
	{
		if (_bPreventCheckEvent)
		{
			if (this.AutoCheck != null)
			{
				this.AutoCheck(this, e);
			}
		}
		else
		{
			base.OnAfterCheck(e);
		}
	}

	private void MessageForNodeStates(string strwhen)
	{
		string text = strwhen;
		foreach (TreeNode node in base.Nodes)
		{
			string text2 = text;
			text = text2 + "\r\nName: " + node.Name + "; Checked: " + node.Checked + " ; ImgState: " + node.StateImageIndex;
			foreach (TreeNode node2 in node.Nodes)
			{
				string text3 = text;
				text = text3 + "\r\nName: " + node2.Name + "; Checked: " + node2.Checked + " ; ImgState: " + node2.StateImageIndex;
			}
		}
		MessageBox.Show(text);
	}

	private void UpdateForLteWcdmaLinkAfterCheckStatus(TreeViewEventArgs e)
	{
		if (e.Node.Parent == null || !(e.Node.Parent.Name == "LTE") || !e.Node.Checked)
		{
			return;
		}
		foreach (TreeNode node in base.Nodes)
		{
			if (node.Name == "WCDMA" && node.StateImageIndex == 0)
			{
				SetState(node, 1);
				if (!_bFirstInit)
				{
					MessageBox.Show("Auto select WCDMA due to LTE path loss data linked to WCDMA.");
				}
			}
		}
	}

	private void LteWcdmaLinkBeforeCheckStatus(TreeViewCancelEventArgs e)
	{
		if (!(e.Node.Name == "LTE") || e.Node.StateImageIndex != 0)
		{
			return;
		}
		foreach (TreeNode node in base.Nodes)
		{
			if (node.Name == "WCDMA" && node.StateImageIndex == 0)
			{
				SetState(node, 1);
				if (!_bFirstInit)
				{
					MessageBox.Show("Auto select WCDMA due to LTE path loss data linked to WCDMA.");
				}
			}
		}
	}

	private bool TrigerNotToUncheckWcdamWhenLteSelect(TreeViewCancelEventArgs e)
	{
		if (e.Node.Parent != null)
		{
			if (e.Node.Parent.Name == "WCDMA" && e.Node.Checked)
			{
				foreach (TreeNode node in base.Nodes)
				{
					if (!(node.Name == "LTE") || node.StateImageIndex == 0)
					{
						continue;
					}
					int num = 0;
					foreach (TreeNode node2 in e.Node.Parent.Nodes)
					{
						if (node2.Checked)
						{
							num++;
						}
					}
					if (num == 1)
					{
						if (!_bFirstInit && _bEditableSubNodes)
						{
							MessageBox.Show("Deselecting WCDMA is not allowed when LTE is selected.");
						}
						return true;
					}
				}
			}
		}
		else if (e.Node.Name == "WCDMA" && e.Node.StateImageIndex == 1)
		{
			foreach (TreeNode node3 in base.Nodes)
			{
				if (node3.Name == "LTE" && node3.StateImageIndex != 0)
				{
					if (!_bFirstInit)
					{
						MessageBox.Show("Deselecting WCDMA is not allowed when LTE is selected.");
					}
					return true;
				}
			}
		}
		return false;
	}

	protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e)
	{
		base.OnNodeMouseClick(e);
		int num = ((base.ImageList != null) ? 20 : 0);
		if (e.X <= e.Node.Bounds.Left - num && e.X >= e.Node.Bounds.Left - (num + 14) && e.Button == MouseButtons.Left)
		{
			SetState(e.Node, (!e.Node.Checked) ? 1 : 0);
		}
	}
}
