using System;
using System.Collections;
using System.Collections.Generic;

namespace GeneralNode
{

    public class StringNode : ArbitraryNode, IComparable<StringNode>
    {
        string p;
        string sep;
        string th;
        string name;
        private StringNode(string s, string sep, List<StringNode> list) : base(null)
        {
           th = s + "";
           this.sep = sep;
           int n = s.LastIndexOf(sep);
           if (n < 0)
           {
               p = "";
           }
           else
           {
               p = th.Substring(0, n);
           }
           if (list.Contains(this))
           {
               throw new Exception();
           }
           if (n < 0)
           {
               name = s;
           }
           else
           {
               name = s.Substring(n + sep.Length);
           }
           list.Add(this);
        }
        public override object Id
        {
            get
            {
                return th;
            }
        }

        public override object ParentId
        {
            get
            {
                return p;
            }
        }

        #region IComparable<StringNode> Members

        public int CompareTo(StringNode other)
        {
            return th.Length - other.th.Length;
        }

        #endregion

        public override int GetHashCode()
        {
            return th.GetHashCode();
        }

 

        public override bool Equals(object obj)
        {
            StringNode n = obj as StringNode;
            return th.Equals(n.th);
        }

        #region Specific members

        public string Name
        {
            get
            {
                return name;
            }
        }

        static public StringNode GetRoot(List<string> list, string sep)
        {
            List<StringNode> l = new List<StringNode>();
            Dictionary<string, StringNode> table = new Dictionary<string,StringNode>();

            foreach (string str in list)
            {
                StringNode sn = new StringNode(str, sep, l);
                table[str] = sn;
            }
            l.Sort();
            List<StringNode> li = new List<StringNode>(l);
            foreach (StringNode node in li)
            {
                node.split(l, table);
            }

            StringNode root = new StringNode("", sep, l);
            table[""] = root;
            root.p = null;
            foreach (StringNode node in table.Values)
            {
                if (node.p != null)
                {
                    node.Parent = table[node.p];
                }
            }
            return root;
        }

        public string Path
        {
            get
            {
                return th;
            }
        }




        private void split(List<StringNode> list, Dictionary<string, StringNode> table)
        {
            int n = th.LastIndexOf(sep);
            if (n < 0)
            {
                return;
            }
            string str = th.Substring(0, n);
            try
            {
                StringNode sn = new StringNode(str, sep, list);
                table[str] = sn;
                sn.split(list, table);
            }
            catch (Exception ex)
            {
                
            }

        }
        #endregion
    }
}
