using General;
using System;
using System.IO;
using System.Reflection;

namespace CXMLCli
{
    class Program
    {
        static void Main(string[] args)
        {
            //args = "src20.vag -iv".Split(' ');

            bool Check = true;

            if (args.Length == 0)
            {
                Console.WriteLine("-- app.info/CXML Decompiler --");
                Console.WriteLine("I like to see girls die :3");
                Console.WriteLine("Required Arguments:");
                Console.WriteLine("\t<input_filename>");
                Console.WriteLine("Optional Arguments:");
                Console.WriteLine("\t-iv --is-vag               File is HE-VAG audio and NOT CXML");
                Console.WriteLine("\t-f --force                 Dont check magic number.");
                Console.WriteLine("\t-dt --dump-tables          Dump ALL tables.");
                Console.WriteLine("\t-tt --dump-tree            Dump tree table.");
                Console.WriteLine("\t-ist --dump-string-id      Dump string ID table.");
                Console.WriteLine("\t-iit --dump-int-id         Dump int ID table.");
                Console.WriteLine("\t-st --dump-string          Dump string table.");
                Console.WriteLine("\t-ct --dump-char            Dump char table.");
                Console.WriteLine("\t-hit --dump-hash-id        Dump hash ID table.");
                Console.WriteLine("\t-iat --dump-int-array      Dump int array table.");
                Console.WriteLine("\t-fat --dump-float-array    Dump float array table.");
                Console.WriteLine("\t-ft --dump-file            Dump file table.");
                Console.WriteLine("\t-d --decompile             Decompile CXML.");
                Console.WriteLine("Example: " + Path.GetFileName(Assembly.GetEntryAssembly().Location) + " app.info -f -s -t -f -d");
                Console.WriteLine("Default functonality is to Decompile,\ntThis is canceled if any other arguments passed.");
                return;
            }

            String ArgsFull = String.Join(" ", args);
            String path = args[0];


            if (ArgsFull.Contains("-iv") || ArgsFull.Contains("--is-vag"))
            {
                byte[] WaveData = VAG.VAGAudio.Vag2Wav(path);
                string FileName = Path.GetFileNameWithoutExtension(path) + "-" + VAG.VAGAudio.GetFilename(path) + ".wav";
                Console.WriteLine("Writing "+FileName);
                File.WriteAllBytes(FileName, WaveData);
                return;
            }

            CXML.CXMLParser.Init(path, Check);


            if (ArgsFull.Contains("-f") || ArgsFull.Contains("--force"))
            {
                Check = false;
            }

            if (ArgsFull.Contains("-dt") || ArgsFull.Contains("--dump-tables"))
            {
                ArgsFull += "-tt -ist -iit -st -ct -sit -iat -fat -ft";
            }

            if (ArgsFull.Contains("-tt") || ArgsFull.Contains("--dump-tree"))
            {
                Console.WriteLine("Dumping tree table.");
                File.WriteAllBytes("tree-table.bin", CXML.CXMLParser.GetTreeTable());

            }

            if (ArgsFull.Contains("-ist") || ArgsFull.Contains("--dump-string-id"))
            {
                Console.WriteLine("Dumping string ID table.");
                File.WriteAllBytes("string-id-table.bin", CXML.CXMLParser.GetStringIDTable());

            }

            if (ArgsFull.Contains("-iit") || ArgsFull.Contains("--dump-int-id"))
            {
                Console.WriteLine("Dumping int ID table.");
                File.WriteAllBytes("int-id-table.bin", CXML.CXMLParser.GetIntIDTable());
            }

            if (ArgsFull.Contains("-st") || ArgsFull.Contains("--dump-string"))
            {
                Console.WriteLine("Dumping string table.");
                File.WriteAllBytes("string-table.bin",CXML.CXMLParser.GetStringTable());

            }

            if (ArgsFull.Contains("-ct") || ArgsFull.Contains("--dump-char"))
            {
                Console.WriteLine("Dumping char table.");
                File.WriteAllBytes("char-table.bin", CXML.CXMLParser.GetCharTable());
            }

            if ((ArgsFull.Contains("-sit") || ArgsFull.Contains("--dump-style-id")) /*kept for backwards comp*/ || (ArgsFull.Contains("-hit") || ArgsFull.Contains("--dump-hash-id")))
            {
                Console.WriteLine("Dumping hash ID table.");
                File.WriteAllBytes("hash-id-table.bin", CXML.CXMLParser.GetHashIDTable());
            }


            if (ArgsFull.Contains("-iat") || ArgsFull.Contains("--dump-int-array"))
            {
                Console.WriteLine("Dumping int array table.");
                File.WriteAllBytes("int-array-table.bin", CXML.CXMLParser.GetIntArrayTable());
            }

            if (ArgsFull.Contains("-fat") || ArgsFull.Contains("--dump-float-array"))
            {
                Console.WriteLine("Dumping float array table.");
                File.WriteAllBytes("float-array-table.bin", CXML.CXMLParser.GetFloatArrayTable());
            }


            if (ArgsFull.Contains("-ft") || ArgsFull.Contains("--dump-tree"))
            {
                Console.WriteLine("Dumping file table.");
                File.WriteAllBytes("file-table.bin", CXML.CXMLParser.GetTreeTable());
            }

            if (ArgsFull.Contains("-d") || ArgsFull.Contains("--decompile") || args.Length == 1)
            {
                Console.WriteLine("Decompiling.");
                CXML.CXMLParser.DecompileCXML(path, Check);
                Console.WriteLine("\n\nDECOMPILATION COMPLETE!");
                Console.ReadKey();
            }
        }
    }
}
