using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Automation
{
    class Automation
    {
        private static readonly string Name = "automation";

        private static readonly string XmlPrefix = "<!--Start Dependencies-->\n  <ItemGroup>\n";
        private static readonly string XmlSuffix = "  </ItemGroup>\n<!--End Dependencies-->";

        private static readonly string GamecraftModdingAPI_csproj = @"\GamecraftModdingAPI.csproj";
        static void Main(string[] args)
        {
            if (args.Length != 2 || args[0] == "-h" || args[0] == "--help")
            {
                Console.WriteLine($"Usage : {Name}.exe <path to Gamecraft DLLs> <path to GamecraftModdingAPI.csproj>");
                Console.WriteLine("Other arguments:");
                Console.WriteLine(" --help : display this message");
                Console.WriteLine($" --version : display {Name}'s version");
                return;
            }
            Console.WriteLine("Building Assembly references");
            string asmXml = BuildAssemblyReferencesXML(args[0]);
            //Console.WriteLine(asmXml);
            string csprojPath = args[1].EndsWith(GamecraftModdingAPI_csproj)? args[1] : args[1]+GamecraftModdingAPI_csproj;
            Console.WriteLine($"Parsing {csprojPath}");
            string csprojContents = File.ReadAllText(csprojPath);
            Match dependenciesStart = (new Regex(@"<\s*!--\s*Start\s+Dependencies\s*--\s*>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline)).Match(csprojContents);
            Match dependenciesEnd = (new Regex(@"<\s*!--\s*End\s+Dependencies\s*--\s*>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline)).Match(csprojContents);
            if (!dependenciesEnd.Success || !dependenciesStart.Success)
            {
                Console.WriteLine("Unable to find dependency XML comments, aborting!");
                return;
            }
            csprojContents = csprojContents.Substring(0, dependenciesStart.Index) + asmXml + csprojContents.Substring(dependenciesEnd.Index+dependenciesEnd.Length);
            //Console.WriteLine(csprojContents);
            Console.WriteLine($"Writing Assembly references into {csprojPath}");
            File.WriteAllText(csprojPath, csprojContents);
            Console.WriteLine("Successfully generated Gamecraft assembly DLL references");
        }

        static string BuildAssemblyReferencesXML(string path)
        {
            StringBuilder result = new StringBuilder();
            result.Append(XmlPrefix);
            string[] files = Directory.GetFiles(path, "*.dll");
            for(int i = 0; i<files.Length; i++)
            {
                if (files[i].Contains(@"\System.") || files[i].EndsWith(@"mscorlib.dll") || files[i].Contains(@"\Mono.")) 
                { 
                    // no
                }
                else
                {
                    result.Append($"    <Reference Include=\"{files[i].Substring(files[i].LastIndexOf(@"\") + 1).Replace(".dll", "")}\">\n      <HintPath>{files[i]}</HintPath>\n    </Reference>\n");
                }
            }
            result.Append(XmlSuffix);
            return result.ToString();
        }
    }
}