package eu.mhsl.minecraft.pixelpic;

import eu.mhsl.minecraft.pixelpic.commands.PixelPicCommand;
import eu.mhsl.minecraft.pixelpic.render.render.DefaultScreenRenderer;
import eu.mhsl.minecraft.pixelpic.render.render.Renderer;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public final class Main extends JavaPlugin {
    private static Main instance;
    private Renderer screenRenderer;

    @Override
    public void onEnable() {
        instance = this;
        extractJsonResources();

        Bukkit.getPluginCommand("pixelPic").setExecutor(new PixelPicCommand());

        Bukkit.getPluginCommand("test").setExecutor((sender, command, label, args) -> {
            Material.getMaterial("acacia_button");
            Bukkit.broadcast(Component.text(Material.STONE.getBlockTranslationKey().replace("block.minecraft.", "")));

            if(!(sender instanceof Player player))
                throw new IllegalStateException("Dieser Command kann nur von einem Spieler ausgeführt werden!");

            File blockDir = new File(getDataFolder(), "models/block");
            for (File file : blockDir.listFiles()) {
                String blockName = file.getName().substring(0, file.getName().lastIndexOf('.'));
                Material material = Material.getMaterial(blockName.toUpperCase());
                System.out.println(material);
                if(material == null) {
                    System.out.println(blockName);
                }
            }

            return true;
        });
    }

    @Override
    public void onDisable() {
    }

    public void extractJsonResources() {
        String resourcePath = "models/block/"; // Pfad im JAR
        File outputDir = new File(getDataFolder(), resourcePath);
        if (outputDir.exists()) return;
        outputDir.mkdirs();

        try {
            URL jarUrl = getClass().getProtectionDomain().getCodeSource().getLocation();
            File jarFile = new File(jarUrl.toURI());

            try (JarFile jar = new JarFile(jarFile)) {
                Enumeration<JarEntry> entries = jar.entries();

                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    String entryName = entry.getName();

                    // Nur JSON-Dateien im gewünschten Ordner
                    if (entryName.startsWith(resourcePath) && entryName.endsWith(".json")) {
                        InputStream in = getResource(entryName);
                        if (in == null) continue;

                        File outFile = new File(getDataFolder(), entryName);
                        outFile.getParentFile().mkdirs(); // Ordnerstruktur sicherstellen

                        try (OutputStream out = new FileOutputStream(outFile)) {
                            byte[] buffer = new byte[1024];
                            int len;
                            while ((len = in.read(buffer)) != -1) {
                                out.write(buffer, 0, len);
                            }
                            System.out.println("Extrahiert: " + entryName);
                        }

                        in.close();
                    }
                }
            }
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public Renderer getScreenRenderer() {
        if(this.screenRenderer == null) this.screenRenderer = new DefaultScreenRenderer();
        return this.screenRenderer;
    }

    public static Main getInstance() {
        return instance;
    }
}
