package com.zivilon.cinder_loe.util;

import com.zivilon.cinder_loe.client.render.item.RenderHelper;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.Vector;

import lotr.common.LOTRCreativeTabs;
import lotr.common.world.biome.LOTRBiome;

import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;

public class Utilities {

    public static int[] LOTRIntCache = null;
    public static LOTRBiome reflected_river;
    public static LOTRCreativeTabs reflected_tab_block;

    public static void initialize_reflects() {
        try {
            Field riverField = LOTRBiome.class.getField("river");
            reflected_river = (LOTRBiome)riverField.get(null);

        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void pre_init_reflects() {
        try {
            Field tabBlockField = LOTRCreativeTabs.class.getField("tabBlock");
            reflected_tab_block = (LOTRCreativeTabs)tabBlockField.get(null);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static EntityPlayerMP getPlayerByUUID(UUID playerUUID) {
        MinecraftServer server = MinecraftServer.getServer(); // Get the server instance
        if (server == null) {
            return null; // Server instance is not available
        }

        // Iterate through the list of online players and match UUID
        for (Object obj : server.getConfigurationManager().playerEntityList) {
            if (obj instanceof EntityPlayerMP) {
                EntityPlayerMP player = (EntityPlayerMP) obj;
                if (player.getUniqueID().equals(playerUUID)) {
                    return player;
                }
            } else {
                System.err.println("[CinderCore] Player list having non-players, WHAT?!?!?!");
            }
        }
        return null; // Player not found or not online
    }

    public static String toSnakeCase(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }

        StringBuilder builder = new StringBuilder();
        char[] chars = input.toCharArray();

        builder.append(Character.toLowerCase(chars[0]));
        for (int i = 1; i < chars.length; i++) {
            char c = chars[i];
            if (Character.isUpperCase(c)) {
                builder.append('_').append(Character.toLowerCase(c));
            } else {
                builder.append(c);
            }
        }

        return builder.toString();
    }

    public static void dumpClass(String className, String outputFilePath, Class<?> contextClass) {
        // Convert class name to a resource path
        String resourcePath = className.replace('.', '/') + ".class";
        // Use the class loader of the context class to find the resource
        try (InputStream classStream = contextClass.getClassLoader().getResourceAsStream(resourcePath)) {
            if (classStream == null) {
                System.err.println("Class " + className + " could not be found.");
                return;
            }
            try (OutputStream outputStream = new FileOutputStream(outputFilePath)) {
                // Copy the class stream to the output file
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = classStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                System.out.println("Dumped " + className + " to " + outputFilePath);
            } catch (IOException e) {
                System.err.println("Failed to write class file: " + e.getMessage());
            }
        } catch (IOException e) {
            System.err.println("Failed to read class file: " + e.getMessage());
        }
    }

    public static void writeLog(String message) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("custom_log.txt", true))) {
            LocalDateTime now = LocalDateTime.now();
            String timestamp = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            writer.write(timestamp + " - " + message + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
