package net.knarcraft.minecraftserverlauncher.server;

import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController;
import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import javax.naming.ConfigurationException;
import java.io.File;
import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JarBuilderTest {

    private static JarBuilder jarBuilder;
    private static String targetDirectory;
    private static String jarDirectory;

    @BeforeAll
    public static void preSetUp() {
        try {
            ServerLauncherController.getInstance().loadState(true);
        } catch (ConfigurationException | IOException e) {
            e.printStackTrace();
        }
    }

    @BeforeEach
    public void setUp() {
        targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +
                "BuildTools" + File.separator;
        jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +
                "testjars" + File.separator;
        jarBuilder = new JarBuilder(targetDirectory, jarDirectory, new FakeGUI());
        removeBuildToolsFiles();
    }

    @AfterAll
    public static void tearDown() {
        removeBuildToolsFiles();
    }

    @Test
    @Order(3)
    public void buildLatestSpigotJarTest() {
        File spigotFile = new File(jarDirectory + "SpigotLatest.jar");
        if (spigotFile.exists() && !spigotFile.delete()) {
            throw new IllegalArgumentException("Unable to remove existing spigot .jar");
        }
        jarBuilder.buildSpigotJar();
        assertTrue(spigotFile.exists());
    }

    @Test
    @Order(4)
    public void buildLatestBukkitJarTest() {
        File bukkitFile = new File(jarDirectory + "BukkitLatest.jar");
        if (bukkitFile.exists() && !bukkitFile.delete()) {
            throw new IllegalArgumentException("Unable to remove existing bukkit .jar");
        }
        jarBuilder.buildBukkitJar();
        assertTrue(new File(jarDirectory + "BukkitLatest.jar").exists());
    }

    @Test
    @Order(2)
    public void downloadLatestBuildToolsJarTest() {
        jarBuilder.downloadBuildTools();
        assertTrue(new File(targetDirectory + "BuildTools.jar").exists());
    }

    @Test
    @Order(1)
    public void getLatestBuildToolsVersionTest() {
        try {
            String latestVersion = jarBuilder.getLatestBuildToolsVersion();
            assertNotEquals("", latestVersion);
            int newVersion = Integer.parseInt(latestVersion);
            assertNotEquals(newVersion, 0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Removes build tools files
     */
    private static void removeBuildToolsFiles() {
        File target = new File(targetDirectory);
        if (!target.exists() && !target.mkdirs()) {
            throw new IllegalArgumentException("Unable to create the test files directory");
        }
        CommonFunctions.removeFilesRecursively(target);
    }

}