using System;
using System.Collections.Generic;
using System.Windows.Forms;
using gmRTL.Core;

namespace MSOutl
{
   /// <summary>
   /// This is a TreeView that can be populated using the MS Outline Control interface
   /// </summary>
   public class Outline : TreeView
   {

      private List<TreeNode> items = new List<TreeNode>();
      private List<int> indents = new List<int>();
      private string ind = "   ";

      public Outline() : base()
      {
      }

      private TreeNode GetNode(int Index)
      {
         if (Index < 0 || Index > items.Count)
         {
            System.Diagnostics.Debug.WriteLine("Invalid Node Index:" + Index);
            return null;
         }
         return items[Index];
      }

      public MSOutl.StyleConstants Style
      {
         set; get;
      }
      public System.Drawing.Image PicturePlus
      {
         set; get;
      }
      public System.Drawing.Image PictureMinus
      {
         set; get;
      }
      public System.Drawing.Image PictureLeaf
      {
         set; get;
      }
      public System.Drawing.Image PictureOpen
      {
         set; get;
      }
      public System.Drawing.Image PictureClosed
      {
         set; get;
      }
      private int _ListIndex = -1;
      public int ListIndex
      {
         set
         {
            _ListIndex = value;
         }
         get
         {
            return _ListIndex;
         }
      }
      protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e)
      {
         _ListIndex = items.IndexOf(e.Node);
         base.OnNodeMouseClick(e);
      }

      public int ListCount
      {
         get
         {
            return items.Count;
         }
      }
      public void Clear()
      {
         items.Clear();
         Nodes.Clear();
      }
      public void AddItem(string Item, int Index = 0) // using optional arg
      {
         TreeNode nod = new TreeNode();
         nod.Text = Item;
         nod.Name = Index.ToStr();
         items.Add(nod);
         indents.Add(1);
         Nodes.Add(nod);
      }
      public string List(int Index)
      {
         return items[Index].Text;
      }
      public void set_List(int Index, string value)
      {
         items[Index].Text = value;
         GetNode(Index).Text = value;
      }
      public int Indent(int Index)
      {
         return indents[Index];
      }
      public void set_Indent(int Index, int value)
      {
         indents[Index] = value;
         TreeNode nod = GetNode(Index);
         for (int i = Index - 1; i >= 0; i--)
         {
            if (indents[i] < value)
            {
               TreeNode parent = GetNode(i);
               nod.Remove();
               parent.Nodes.Add(nod);
               break;
            }
         }
      }
      public bool Expand(int Index)
      {
         return GetNode(Index).IsExpanded;
      }
      public void set_Expand(int Index, bool value)
      {
         if (value) GetNode(Index).Expand();
         else GetNode(Index).Collapse();
      }
      public int ItemData(int Index)
      {
         return GetNode(Index).Tag.ToInt();
      }
      public void set_ItemData(int Index, int value)
      {
         GetNode(Index).Tag = value.ToStr();
      }
      public string FullPath(int Index)
      {
         string fullPath = this.GetNode(Index).FullPath;
         return fullPath;
      }
   }
   public enum StyleConstants
   {
      outTextOnly = 0,
      outPictureText = 1,
      outPlusMinusText = 2,
      outPlusPictureText = 3,
      outTreelinesText = 4,
      outTreelinesPictureText = 5
   }
}
