﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

// essentially we strive to mold the treelist control of devx into the treeview control of microsoft
namespace Rvs.KbCheats.Shared.UIUtilities.CustomControls
{
	[ToolboxItem(true)]
	public sealed class CCDXTreeView : TreeList
	{
		private bool _useBuiltInMechanismForSettingNodeImages;

		[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
		public bool UseBuiltInMechanismForSettingNodeImages
		{
			get { return _useBuiltInMechanismForSettingNodeImages; }
			set
			{
				if (_useBuiltInMechanismForSettingNodeImages == value) return;

				_useBuiltInMechanismForSettingNodeImages = value;

                GetStateImage -= OnGetStateImage;
                if (value) GetStateImage += OnGetStateImage;
			}
		}

		[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)] public int StateImageIndexForFocusedNode { get; set; }
		[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)] public int StateImageIndexForUnfocusedNode { get; set; }

		public CCDXTreeView()
		{
		    BorderStyle = BorderStyles.NoBorder;
		    TreeLineStyle = LineStyle.None;

		    OptionsBehavior.Editable = false;

			OptionsView.ShowColumns = false;
			OptionsView.ShowHorzLines = false;
			OptionsView.ShowIndicator = false;
			OptionsView.ShowVertLines = false;
			OptionsView.FocusRectStyle = DrawFocusRectStyle.None;
			OptionsSelection.EnableAppearanceFocusedCell = false; //0
		}
		//0 this causes the background of the selected nodes to be transparent blue or some other color to that effect depending on theme

		private void OnGetStateImage(object o, GetStateImageEventArgs e)
		{
			e.NodeImageIndex = e.Node.Focused ? StateImageIndexForFocusedNode : StateImageIndexForUnfocusedNode;
		}

	    public void CollapseAllNodesExceptSpecificOnes(List<TreeListNode> nodeList = null)
	    {
            var focusedNodeSnapshot = FocusedNode;
            try
	        {
	            SuspendLayout();
	            BeginUpdate();
	            CollapseAll();

	            (nodeList ?? new List<TreeListNode> {focusedNodeSnapshot}).ForEach(node =>
	            {
	                for (var n = node?.ParentNode; n != null; n = n.ParentNode)
	                {
	                    n.Expanded = true;
	                }
	            });

	            if (nodeList?.FirstOrDefault() != null)
	            {
	                MakeNodeVisible(nodeList.FirstOrDefault());
	            }
	        }
	        finally
	        {
                FocusedNode = focusedNodeSnapshot;

                EndUpdate();
	            ResumeLayout(performLayout: true);
	        }
	    }

	    protected override void OnKeyDown(KeyEventArgs e)
		{
			try
			{
				var currentlyFocusedNode = FocusedNode;
				if (e.Control || (e.KeyCode != Keys.Left && e.KeyCode != Keys.Right) || currentlyFocusedNode == null) return;

				if (e.KeyCode == Keys.Left)
				{
					if (currentlyFocusedNode.HasChildren && currentlyFocusedNode.Expanded)
					{
						currentlyFocusedNode.Expanded = false;
					}
					else
					{
						var parentNode = FocusedNode.ParentNode;
						if (parentNode != null) FocusedNode = parentNode;
					}
				}
				else if (e.KeyCode == Keys.Right)
				{
					if (currentlyFocusedNode.HasChildren)
					{
						if (!currentlyFocusedNode.Expanded)
						{
							currentlyFocusedNode.Expanded = true;
						}
						else
						{
							FocusedNode = currentlyFocusedNode.Nodes[0];
						}
					}
				}
			}
			finally
			{
				base.OnKeyDown(e);
			}
		}
	}
}
