package net.viper.status.modules.forum;

import java.util.UUID;

/**
 * Eine einzelne Forum-Benachrichtigung.
 */
public class ForumNotification {

    private final UUID playerUuid;
    private final String type;       // reply, mention, message
    private final String title;
    private final String author;
    private final String url;
    private final long timestamp;
    private boolean delivered;

    public ForumNotification(UUID playerUuid, String type, String title, String author, String url) {
        this.playerUuid = playerUuid;
        this.type = type != null ? type : "reply";
        this.title = title != null ? title : "";
        this.author = author != null ? author : "Unbekannt";
        this.url = url != null ? url : "";
        this.timestamp = System.currentTimeMillis();
        this.delivered = false;
    }

    /** Interner Konstruktor f\u00fcr Deserialisierung */
    ForumNotification(UUID playerUuid, String type, String title, String author, String url, long timestamp, boolean delivered) {
        this.playerUuid = playerUuid;
        this.type = type;
        this.title = title;
        this.author = author;
        this.url = url;
        this.timestamp = timestamp;
        this.delivered = delivered;
    }

    // --- Getter ---

    public UUID getPlayerUuid() { return playerUuid; }
    public String getType() { return type; }
    public String getTitle() { return title; }
    public String getAuthor() { return author; }
    public String getUrl() { return url; }
    public long getTimestamp() { return timestamp; }
    public boolean isDelivered() { return delivered; }
    public void setDelivered(boolean d) { this.delivered = d; }

    /**
     * Deutsches Label f\u00fcr den Benachrichtigungstyp.
     */
    public String getTypeLabel() {
        switch (type) {
            case "reply":   return "Neue Antwort";
            case "mention": return "Erw\u00e4hnung";
            case "message": return "Neue PN";
            case "thread":  return "Neuer Thread";
            case "poll":    return "Neue Umfrage";
            case "answer":  return "Antwort auf deinen Thread";
            default:        return "Benachrichtigung";
        }
    }

    /**
     * Farbcode (Minecraft) je nach Typ.
     */
    public String getTypeColor() {
        switch (type) {
            case "reply":   return "§b";  // Aqua
            case "mention": return "§e";  // Gelb
            case "message": return "§d";  // Rosa
            case "thread":  return "§a";  // Gr\u00fcn
            case "poll":    return "§3";  // Dunkel-Aqua
            case "answer":  return "§2";  // Dunkel-Gr\u00fcn
            default:        return "§f";  // Wei\u00df
        }
    }

    /**
     * Serialisierung f\u00fcr Datei-Speicherung.
     * Format: uuid|type|title|author|url|timestamp|delivered
     */
    public String toLine() {
        return playerUuid.toString() + "|"
                + type + "|"
                + title.replace("|", "_") + "|"
                + author.replace("|", "_") + "|"
                + url.replace("|", "_") + "|"
                + timestamp + "|"
                + (delivered ? "1" : "0");
    }

    /**
     * Deserialisierung aus einer Zeile.
     */
    public static ForumNotification fromLine(String line) {
        if (line == null || line.trim().isEmpty()) return null;
        String[] parts = line.split("\\|", -1);
        if (parts.length < 7) return null;
        try {
            UUID uuid = UUID.fromString(parts[0]);
            String type = parts[1];
            String title = parts[2];
            String author = parts[3];
            String url = parts[4];
            long timestamp = Long.parseLong(parts[5]);
            boolean delivered = "1".equals(parts[6]);
            return new ForumNotification(uuid, type, title, author, url, timestamp, delivered);
        } catch (Exception e) {
            return null;
        }
    }
}
