using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MxFilterGen2
{
    internal class Program
    {
        public static string version = "1.1.1";
        public static string docpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        static void Main(string[] args)
        {
            Message.Splash("MxFilterGen2", "mikx");
            IO.Cleaner.CleanOut();
            IO.Creator.CreateBaseDir();
            string fname = JSON.settings.GetName();
            foreach (string s in JSON.settings.GetStructure())
            {
                if (s != "GEN")
                {
                    Message.CMW($"Section: {s}", true, 2);
                    foreach (string f in JSON.settings.GetType())
                    {
                        Compiler.section.Compile(s, f);
                    }
                }               
            }

            Generator.Body.Gen();
            Generator.Boots.Gen();
            Generator.Bows.Gen();
            Generator.Claws.Gen();
            Generator.Daggers.Gen();
            Generator.Focus.Gen();
            Generator.Gloves.Gen();
            Generator.Helmets.Gen();
            Generator.OneHander.Gen();
            Generator.Shields.Gen();
            Generator.Staves.Gen();
            Generator.TwoHander.Gen();
            Generator.Wands.Gen();

            var gens = Directory.GetFiles("out/Gen");
            string genstring = "";
            foreach (var g in gens)
            {                
                foreach (string f in JSON.settings.GetType())
                {
                    File.AppendAllText($"out/{f}/GEN.filter",File.ReadAllText(g));
                }
            }

            foreach (string f in JSON.settings.GetType())
            {
                Message.CMW($"Creating the {f} filter...", true, 2);
                string outp = "out/" + fname + "_" + f + ".filter";
                ////////// Splash //////////
                File.AppendAllText(outp, $"#### Filson - PoE2 Item Filter JSON Parsing - MxFilterGen2 v{Program.version}{Environment.NewLine}");
                File.AppendAllText(outp, $"#### Filson and MxFilterGen2 are developped by mikx.{Environment.NewLine}");
                File.AppendAllText(outp, $"#### MxGit:     https://mxgit.ovh/mikx/PoE2-MxFilterGen2{Environment.NewLine}");
                File.AppendAllText(outp, $"#### MxPoE:     https://mxpoe.ovh/{Environment.NewLine}");
                File.AppendAllText(outp, $"#### Contact:   mikx@mxpoe.ovh / http://discord.mxg.ovh{Environment.NewLine}");
                File.AppendAllText(outp, $"{Environment.NewLine}");
                File.AppendAllText(outp, $"#### SECTIONS{Environment.NewLine}");
                foreach (var sspl in JSON.settings.GetStructure())
                {
                    File.AppendAllText(outp, $"# {sspl}{Environment.NewLine}");
                }
                File.AppendAllText(outp, $"{Environment.NewLine}");               
                foreach (var structure in JSON.settings.GetStructure())
                {                   
                    File.AppendAllText("out/" + fname + "_" + f + ".filter", string.Format("#### SECTION: {0}", (object)structure) + Environment.NewLine);
                    File.AppendAllText("out/" + fname + "_" + f + ".filter", File.ReadAllText(string.Format("out/" + f + "/" + structure + ".filter")));
                    File.AppendAllText("out/" + fname + "_" + f + ".filter", Environment.NewLine ?? "");
                }
            }

            if (JSON.settings.GetInstall())
            {
                Message.CMW($"Install is \"True\". Installing to the game directory...", true, 1);
                foreach (string f in JSON.settings.GetType())
                {
                    File.Copy($"out/{JSON.settings.GetName()}_{f}.filter", $@"{docpath}\My Games\Path of Exile 2\{JSON.settings.GetName()}_{f}.filter", true);
                }
                foreach (var s in Directory.EnumerateFiles("sound"))
                {
                    string sf = Path.GetFileName(s);
                    File.Copy($"{s}", $@"{docpath}\My Games\Path of Exile 2\{sf}", true);
                }
            } else
            {
                int fc = JSON.settings.GetType().Count;
                Message.CMW($"Install is \"False\". Use the {fc} Filter(s) from the \"out\" directory.", true, 1);
            }
        }
    }
}