using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// nj josh man's assetbundle namespaces
using NJJM.AssetProcessor;
using NJJM.Starlite;
using NJJM.Internal;
using ImpromptuNinjas.ZStd;

// main program code
namespace StarliteExtractor
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("usage: moonlite.exe [input path (the folder should have root.assets file)] [output path (WARNING: everything in this path will be PERMENANTLY removed!!!)]");
            Console.WriteLine("");
            Console.WriteLine("compatible games");
            Console.WriteLine("My Talking Tom 2 3.3 or older");
            Console.WriteLine("Talking Tom Sky Run 1.0");
            Console.WriteLine("My Talking Angela 2 2.0 or older");
            Console.WriteLine("My Talking Tom Friends 2.2 or older");

            if (true)
            {
                if (File.Exists(args[0] + ".assets"))
                {
                    if (Directory.Exists(args[1]))
                    {
                        string iPath = args[0];
                        string oPath = args[1];

                        Extract(iPath, oPath);
                    }
                    else
                    {
                        Console.WriteLine("error: output path is incorrect or is not a path");
                    }
                }
                else
                {
                    Console.WriteLine("error: input path is incorrect or is not a path");
                }
            }
        }

        // arg1 in arg2 out
        private static void Extract(string arg1, string arg2)
        {
            try
            {
                BinaryReader reader = new BinaryReader(new FileStream(arg1 + ".assets", FileMode.Open));

                List<ZstdChunkFrame> frames = new List<ZstdChunkFrame>();
                List<Asset> output = new List<Asset>();

                if (reader.ReadUInt32() != 0x0000ffff)
                {
                    reader.BaseStream.Seek(12, SeekOrigin.Current);
                }
                else
                {
                    reader.BaseStream.Seek(-4, SeekOrigin.Current);
                }

                // collect all of the zstd frames
                ulong idx = 0;
                while (reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    frames.Add(new ZstdChunkFrame(reader));
                    Console.WriteLine("read zstd frame: " + frames[(int)idx].frame_length + ", " + reader.BaseStream.Position.ToString("x16"));
                    idx++;
                }

                // use binparse to sort frames
                BinParse parse = new BinParse();

                MemoryStream str = parse.ExtractProject(arg1 + ".project");

                parse.ParseMain(frames, str, output);

                Console.WriteLine("asset count: " + output.Count);

                //ConfigCreator configM = new ConfigCreator();

                //FileStream file = new FileStream(arg2 + "\\config.njcf", FileMode.Create);

                //configM.CreateConfig(file, arg2);

                //Console.WriteLine("created config file 'config.njcf'");

                ulong i2 = 0;

                foreach (Asset item in output)
                {
                    FileStream stream = new FileStream(arg2 + "\\" + item.path, FileMode.Create);

                    stream.Write(item.bytes, 0, item.bytes.Length);

                    Console.WriteLine("export asset: " + item.path);

                    stream.Close();

                    i2++;
                }

                Console.WriteLine("export complete");

                //file.Close();

                reader.Close();

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
 when it outputs files, it is writing them with guid, not path