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

namespace Global
{
    public class Project
    {
        // Project Files
        public string Directory { get; set; }
        public string Name { get; set; }
        public string Engine { get; set; }
        public string Build { get; set; }
        public string TagFolder { get; set; }
        public string BinFolder { get; set; }
        public string TagsFile { get; set; }
        public string MapImage { get; set; }

        // Misc Project Files
        public List<string> Files { get; set; }

        // References
        public List<string> Tags { get; set; }

        Project()
        {
            // Init Lists
            Tags = new List<string>();
            Files = new List<string>();
        }

        public Project(string File)
        {
            // Open Project
            StreamReader sr = new StreamReader(File);

            // Read
            Directory = File.Substring(0, File.LastIndexOf("\\") + 1);
            Name = sr.ReadLine();
            Engine = sr.ReadLine();
            Build = sr.ReadLine();
            TagFolder = sr.ReadLine();
            BinFolder = sr.ReadLine();
            TagsFile = sr.ReadLine();
            MapImage = sr.ReadLine();

            // Misc Files
            Files = new List<string>();
            while (!sr.EndOfStream)
                Files.Add(sr.ReadLine());

            // Read Tags
            sr = new StreamReader(TagsFile);
            Tags = new List<string>();
            while (!sr.EndOfStream)
                Tags.Add(sr.ReadLine());

            // Close
            sr.Close();
        }

        public static Project Create(string Directory, string Name, string Engine, string MapImage)
        {
            // Create new Project
            Project p = new Project();

            // Fill in Fields
            p.Name = Name;
            p.Engine = Engine;
            p.Build = "0.0.0.0";
            p.Directory = Directory + "\\";
            p.TagFolder = p.Directory + "Tags\\";
            p.BinFolder = p.Directory + "Bin\\";
            p.TagsFile = p.Directory + Name + ".htl";
            p.MapImage = MapImage.Length > 0 ? p.BinFolder + p.Name + "_image." + MapImage.Substring(MapImage.Length - 3) : "";

            // Copy Map Image To Bin Folder
            try { File.Copy(MapImage, p.MapImage); }
            catch { }

            // Create Folders
            System.IO.Directory.CreateDirectory(p.TagFolder);
            System.IO.Directory.CreateDirectory(p.BinFolder);

            // Create Main Project File
            StreamWriter sw = new StreamWriter(Directory + "\\" + Name + ".hpr");
            sw.WriteLine(p.Name);
            sw.WriteLine(p.Engine);
            sw.WriteLine(p.Build);
            sw.WriteLine(p.TagFolder);
            sw.WriteLine(p.BinFolder);
            sw.WriteLine(p.TagsFile);
            sw.WriteLine(p.MapImage);
            sw.Close();

            // Return
            return p;
        }

        public void Save()
        {
            // Create New File
            StreamWriter sw = new StreamWriter(Directory + "\\" + Name + ".hpr");

            // Write Basic Info
            sw.WriteLine(Name);
            sw.WriteLine(Engine);
            sw.WriteLine(Build);
            sw.WriteLine(TagFolder);
            sw.WriteLine(BinFolder);
            sw.WriteLine(TagsFile);
            sw.WriteLine(MapImage);

            // Write Misc Info
            for (int i = 0; i < Files.Count; i++)
                sw.WriteLine(Files[i]);

            // Close
            sw.Close();

            // Write Tags
            sw = new StreamWriter(TagsFile);
            for (int i = 0; i < Tags.Count; i++)
                sw.WriteLine(Tags[i]);

            // Close
            sw.Close();
        }

        public int AddTag(string Tag)
        {
            // Create directorys
            //string Path = this.Directory + this.TagFolder;
            //string[] Folders = Tag.Split(new string[] { "\\" }, StringSplitOptions.None);
            //for (int i = 0; i < Folders.Length - 1; i++)
            //{
            //    Path += "\\" + Folders[i] + "\\";
            //    if (!System.IO.Directory.Exists(Path))
            //        System.IO.Directory.CreateDirectory(Path);
            //}

            // Copy the Tag
            //string Name = Tag.Substring(Tag.LastIndexOf("\\") + 1);
            //File.Copy(Tag, Path + Name);

            // Add to project
            //Tags.Insert(Tags.Count - 2, this.Directory + this.TagFolder + Name);
            return 0;
        }
    }
}
