using System.Diagnostics;

namespace Nerfed.Builder;

internal class Program
{
    private static int Main(string[] args)
    {
        if (Debugger.IsAttached)
        {
            return Run(args);
        }
        else
        {
            try
            {
                return Run(args);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                return -1;
            }
        }
    }

    private static int Run(string[] rawArgs)
    {
        if (rawArgs.Length == 0)
        {
            Console.Error.WriteLine($"Invalid build type '{rawArgs[0]}' expected '-build' or '-package'");
            return -1;
        }

        string buildType = rawArgs[0].ToLower();
        if (buildType == "-build")
        {
            ArgsParser<BuildArgs> parser = new ArgsParser<BuildArgs>(rawArgs);
            if (!parser.Parse())
            {
                Console.Error.Write("Failed to parse build arguments");
                return -1;
            }

            using (Builder builder = new Builder())
            {
                builder.Run(parser.Arguments);
            }
        }
        else if (buildType == "-package")
        {
            ArgsParser<PackageArgs> parser = new ArgsParser<PackageArgs>(rawArgs);
            if (!parser.Parse())
            {
                Console.Error.Write("Failed to parse package arguments");
                return -1;
            }

            using (Packager packager = new Packager())
            {
                packager.Run(parser.Arguments);
            }

            Console.Error.WriteLine("Packaging not yet implemented");
            return -1;
        }

        return 0;
    }
}
