package com.loganmagnan.eventcore.managers;

import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.loganmagnan.eventcore.utils.Constants;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.entity.Player;
import java.util.UUID;

@Getter
@Setter
public class ChatManager {

    private EventCore main = EventCore.getInstance();

    // Toggle Chat System
    private boolean muted = false;
    private UUID toggledBy = null;
    private long lastToggledTime = 0;

    // Slow Chat System
    private int delayAmount = 0;
    private UUID slowedBy = null;
    private long lastSlowedTime = 0;

    // Clear Chat System
    private UUID clearedBy = null;
    private long lastClearedTime = 0;

    // Mute Chat
    public void toggleChat(Player player) {
        this.toggledBy = player.getUniqueId();
        this.lastToggledTime = System.currentTimeMillis();

        if (this.muted) {
            this.muted = false;
            this.broadcastMessage(Constants.CHAT_MANAGEMENT_MESSAGES.get("unmuted").get(0).replace("%player%", player.getName()));
        } else {
            this.muted = true;
            this.broadcastMessage(Constants.CHAT_MANAGEMENT_MESSAGES.get("muted").get(0).replace("%player%", player.getName()));
        }
    }

    // Slow Chat
    public void slowChat(Player player, int amount) {
        this.delayAmount = amount;
        this.slowedBy = player.getUniqueId();
        this.lastSlowedTime = System.currentTimeMillis();
        this.broadcastMessage(Constants.CHAT_MANAGEMENT_MESSAGES.get("slowed").get(0).replace("%player%", player.getName()).replace("%amount%", amount + " &csecond" + (amount > 1 ? "s" : "")));
    }

    // Clear Chat
    public void clearChat(Player player) {
        for (int i = 0; i < 100; i++) {
            this.broadcastMessage("");
        }

        this.clearedBy = player.getUniqueId();
        this.lastClearedTime = System.currentTimeMillis();
        this.broadcastMessage(Constants.CHAT_MANAGEMENT_MESSAGES.get("cleared").get(0).replace("%player%", player.getName()));
    }

    // Broadcast Message
    public void broadcastMessage(String message) {
        this.main.getServer().broadcastMessage(ColorUtils.getMessageType(message));
    }
}
