package me.trouper.sentinel.utils;

import io.github.itzispyder.pdk.utils.FileValidationUtils;
import me.trouper.sentinel.Sentinel;
import org.bukkit.inventory.ItemStack;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public final class FileUtils {

    public static String whoAmI() {
        String classPath = System.getProperty("java.class.path");
        String[] parts = classPath.split(File.pathSeparator);
        for (String part : parts) {
            if (part.endsWith(".jar")) {
                return new File(part).getName();
            }
        }
        return "Unknown.jar";
    }

    public static boolean fileExists(File file) {
        try {
            if (!file.getParentFile().exists()) {
                return false;
            } else {
                return file.exists();
            }
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean folderExists(String folderName) {
        File folder = new File(Sentinel.getInstance().getDirector().io.getDataFolder(), folderName);
        return folder.exists() && folder.isDirectory();
    }

    public static void createFolder(String folderName) {
        File folder = new File(Sentinel.getInstance().getDirector().io.getDataFolder(), folderName);
        if (!folder.exists()) {
            folder.mkdirs();
        }
    }

    public static String createNBTLog(String contents)  {
        ServerUtils.verbose("FileUtils: Creating NBT log");
        String fileName = "nbt_log-" + Random.generateID();

        File dataFolder = Sentinel.getInstance().getDirector().io.getDataFolder();

        File loggedNBTFolder = new File(dataFolder,"LoggedNBT");
        if (!loggedNBTFolder.exists()) {
            loggedNBTFolder.mkdirs();
        }

        File file = new File(loggedNBTFolder, fileName + ".txt");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }

            BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
            writer.append(contents);
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    public static String createNBTLog(ItemStack i)  {
        ServerUtils.verbose("FileUtils: Creating NBT log");

        String item = i.getType().name().toLowerCase() + i.getItemMeta().getAsString();

        String fileName = "nbt_log-" + Random.generateID();

        File dataFolder = Sentinel.getInstance().getDirector().io.getDataFolder();

        File loggedNBTFolder = new File(dataFolder,"LoggedNBT");
        if (!loggedNBTFolder.exists()) {
            loggedNBTFolder.mkdirs();
        }

        File file = new File(loggedNBTFolder, fileName + ".txt");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }

            BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
            writer.append(item);
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }


    public static String createCommandLog(String command)  {

        String fileName = "command_log-" + Random.generateID();
        File file = new File(Sentinel.getInstance().getDirector().io.getDataFolder() + "/LoggedCommands/" + fileName + ".txt");
        FileValidationUtils.validate(file);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }

            BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
            writer.append(command);
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return fileName;
    }

    private boolean deleteDirectory(File file) {
        if (file.isDirectory()) {
            for (File child : file.listFiles()) {
                deleteDirectory(child);
            }
        }
        return file.delete();
    }
}
