using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Global
{
    public class Project
    {
        public string ProjectFile, Directory, Name;
        public List<string> Tags = new List<string>();

        public Project(string ProjectFile)
        {
            this.ProjectFile = ProjectFile;
            Directory = ProjectFile.Substring(0, ProjectFile.LastIndexOf("\\") + 1);
            Name = ProjectFile.Substring(ProjectFile.LastIndexOf("\\") + 1, (ProjectFile.LastIndexOf('.') - 1 - ProjectFile.LastIndexOf("\\")));
        }

        public void Read()
        {
            // Project File
            StreamReader pio = new StreamReader(ProjectFile);
            string TagsList = pio.ReadLine();
            pio.Close();

            // Tags List
            StreamReader sr = new StreamReader(TagsList);
            while (!sr.EndOfStream)
            {
                Tags.Add(sr.ReadLine());
            }
            sr.Close();
        }

        public void IntoTreeView(TreeView tv)
        {
            for (int i = 0; i < Tags.Count; i++)
            {
                string[] Paths = Tags[i].Split(new string[] { "\\" }, StringSplitOptions.None);
                int[] Indexes = new int[Paths.Length];

                // Find the Nodes
                TreeNode n = null;
                for (int x = 0; x < Paths.Length; x++)
                {
                    Indexes[x] = FindNode(tv, Paths[x], Indexes, x);
                    if (Indexes[x] == -1 && x == 0)
                    {
                        tv.Nodes.Add(Paths[x]);
                        Indexes[x] = tv.Nodes.Count - 1;
                        n = tv.Nodes[tv.Nodes.Count - 1];

                        // Image Index Code
                        if (Paths[x].Contains('.'))
                        {
                            n.ImageIndex = 2;
                            n.SelectedImageIndex = 2;
                        }
                        else
                            n.ImageIndex = 0;

                    }
                    else if (Indexes[x] == -1 && x != 0)
                    {
                        n = tv.Nodes[Indexes[0]];
                        for (int z = 1; z < x; z++)
                        {
                            n = n.Nodes[Indexes[z]];
                        }
                        n.Nodes.Add(Paths[x]);
                        Indexes[x] = n.Nodes.Count - 1;
                        n = n.Nodes[n.Nodes.Count - 1];

                        // Image Index Code
                        if (Paths[x].Contains('.'))
                        {
                            n.ImageIndex = 2;
                            n.SelectedImageIndex = 2;
                        }
                        else
                            n.ImageIndex = 0;
                    }
                }
            }

            tv.Sort();
        }

        private int FindNode(TreeView tv, string Path, int[] Indexes, int x)
        {
            if (x == 0)
            {
                for (int i = 0; i < tv.Nodes.Count; i++)
                {
                    if (tv.Nodes[i].Text == Path)
                        return i;
                }
            }
            else
            {
                TreeNode n = tv.Nodes[Indexes[0]];
                for (int i = 1; i < x; i++)
                {
                    n = n.Nodes[Indexes[i]];
                }

                for (int i = 0; i < n.Nodes.Count; i++)
                {
                    if (n.Nodes[i].Text == Path)
                        return i;
                }
            }
            return -1;
        }
    }
}
