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

namespace VR4LifeInstaller
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== VR4Life: Instalador Professional (Limpeza + Logica Babylon) ===");

            try
            {
                // --- PARTE 1: LIMPEZA DA INSTALACAO ANTIGA ---
                string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                string oldLoader = Path.Combine(appData, @"Autodesk\3dsMax\2026 - 64bit\ENU\scripts\startup\vr4life_loader.ms");
                string oldFolder = Path.Combine(appData, @"Autodesk\3dsMax\2026 - 64bit\ENU\scripts\VR4Life_Plugin");

                if (File.Exists(oldLoader))
                {
                    File.Delete(oldLoader);
                    Console.WriteLine(" - Loader antigo removido.");
                }
                if (Directory.Exists(oldFolder))
                {
                    Directory.Delete(oldFolder, true);
                    Console.WriteLine(" - Pasta de scripts antiga removida.");
                }

                // --- PARTE 2: NOVA INSTALACAO (DIRETRIZ BABYLON) ---
                string programData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                string pluginRoot = Path.Combine(programData, @"Autodesk\ApplicationPlugins\VR4Life");
                string contentsPath = Path.Combine(pluginRoot, "Contents");
                string scriptsPath = Path.Combine(contentsPath, "scripts");

                if (!Directory.Exists(scriptsPath)) Directory.CreateDirectory(scriptsPath);

                // Gerar PackageContents.xml na RAIZ
                string xmlContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<ApplicationPackage SchemaVersion=""1.0"" Version=""1.0"" Name=""VR4Life"" ProductCode=""{A1B2C3D4-E5F6-4789-A1B2-C3D4E5F67890}"">
  <Components>
    <RuntimeRequirements OS=""Win64"" Platform=""3ds Max"" SeriesMin=""2024"" SeriesMax=""2026"" />
    <ComponentEntry ComponentName=""VR4Life_Connector"" ModuleName=""./Contents/VR4Life-dll.dll"" />
    <ComponentEntry ComponentName=""VR4Life_Scripts"" ModuleName=""./Contents/scripts/vr4life_macros.ms"" />
  </Components>
</ApplicationPackage>";
                File.WriteAllText(Path.Combine(pluginRoot, "PackageContents.xml"), xmlContent, Encoding.UTF8);

                // Copiar DLL e Scripts para as pastas corretas
                CopyFile("VR4Life-dll.dll", Path.Combine(contentsPath, "VR4Life-dll.dll"));

                string[] files = { "run_vr4life.py", "vr4life_updater.py", "version.txt" };
                foreach (var f in files) CopyFile(f, Path.Combine(scriptsPath, f));

                // Gerar as Macros MS no destino novo
                string macros = $@"
macroScript VR4Life_Launcher category:""Immerse Games"" (on execute do python.executeFile ""{scriptsPath.Replace("\\", "/")}/run_vr4life.py"")
macroScript VR4Life_Update category:""Immerse Games"" (on execute do python.executeFile ""{scriptsPath.Replace("\\", "/")}/vr4life_updater.py"")
";
                File.WriteAllText(Path.Combine(scriptsPath, "vr4life_macros.ms"), macros, Encoding.UTF8);

                Console.WriteLine("\n>>> INSTALACAO E LIMPEZA CONCLUIDAS COM SUCESSO!");
            }
            catch (Exception ex) { Console.WriteLine("ERRO: " + ex.Message); }
            Console.ReadKey();
        }

        static void CopyFile(string src, string dest)
        {
            if (File.Exists(src)) File.Copy(src, dest, true);
        }
    }
}