package de.viper.survivalplus.listeners;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.ChatColor; // Für &-Codes
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.SignChangeEvent;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SignColorListener implements Listener {

    private final SurvivalPlus plugin;

    // Muster für Hex-Farbcodes (#FFFFFF)
    private static final Pattern HEX_PATTERN = Pattern.compile("#[a-fA-F0-9]{6}");
    // Muster für RGB-Farben rgb(255,255,255)
    private static final Pattern RGB_PATTERN = Pattern.compile("rgb\\((\\d{1,3}),(\\d{1,3}),(\\d{1,3})\\)");

    public SignColorListener(SurvivalPlus plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onSignChange(SignChangeEvent event) {
        // Optional: Permission-Check einfügen
        // if (!event.getPlayer().hasPermission("survivalplus.signcolor")) return;

        for (int i = 0; i < event.getLines().length; i++) {
            String line = event.getLine(i);
            if (line != null && !line.isEmpty()) {

                // 1. &-Codes übersetzen (z.B. &a -> §a)
                String translated = ChatColor.translateAlternateColorCodes('&', line);

                // 2. Hex-Farben ersetzen ( #RRGGBB )
                translated = replaceHexColors(translated);

                // 3. RGB-Farben ersetzen ( rgb(r,g,b) )
                translated = replaceRgbColors(translated);

                // 4. Final auf dem Schild setzen
                event.setLine(i, translated);
            }
        }
    }

    private String replaceHexColors(String input) {
        Matcher matcher = HEX_PATTERN.matcher(input);
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            String hexCode = matcher.group();
            // Mit Bungee ChatColor.of in §x-Format umwandeln
            String color = net.md_5.bungee.api.ChatColor.of(hexCode).toString();
            matcher.appendReplacement(sb, color);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    private String replaceRgbColors(String input) {
        Matcher matcher = RGB_PATTERN.matcher(input);
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            int r = clampColorValue(matcher.group(1));
            int g = clampColorValue(matcher.group(2));
            int b = clampColorValue(matcher.group(3));

            String hex = String.format("#%02X%02X%02X", r, g, b);
            String color = net.md_5.bungee.api.ChatColor.of(hex).toString();

            matcher.appendReplacement(sb, color);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    private int clampColorValue(String val) {
        try {
            int v = Integer.parseInt(val);
            return Math.min(255, Math.max(0, v));
        } catch (NumberFormatException e) {
            return 0;
        }
    }
}
