package net.minecraft.client.gui.chat;

import org.apache.logging.log4j.LogManager;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.client.gui.components.toasts.ToastComponent;
import net.minecraft.client.gui.components.toasts.SystemToast;
import net.minecraft.SharedConstants;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.NarratorStatus;
import net.minecraft.client.Minecraft;
import java.util.UUID;
import net.minecraft.network.chat.ChatType;
import com.mojang.text2speech.Narrator;
import org.apache.logging.log4j.Logger;
import net.minecraft.network.chat.Component;

public class NarratorChatListener implements ChatListener {
    public static final Component NO_TITLE;
    private static final Logger LOGGER;
    public static final NarratorChatListener INSTANCE;
    private final Narrator narrator;
    
    public NarratorChatListener() {
        this.narrator = Narrator.getNarrator();
    }
    
    @Override
    public void handle(final ChatType mo, final Component mr, final UUID uUID) {
        if (Minecraft.getInstance().isBlocked(uUID)) {
            return;
        }
        final NarratorStatus dlz5 = getStatus();
        if (dlz5 == NarratorStatus.OFF || !this.narrator.active()) {
            return;
        }
        if (dlz5 == NarratorStatus.ALL || (dlz5 == NarratorStatus.CHAT && mo == ChatType.CHAT) || (dlz5 == NarratorStatus.SYSTEM && mo == ChatType.SYSTEM)) {
            Component mr2;
            if (mr instanceof TranslatableComponent && "chat.type.text".equals(((TranslatableComponent)mr).getKey())) {
                mr2 = new TranslatableComponent("chat.type.text.narrate", ((TranslatableComponent)mr).getArgs());
            }
            else {
                mr2 = mr;
            }
            this.doSay(mo.shouldInterrupt(), mr2.getString());
        }
    }
    
    public void sayNow(final String string) {
        final NarratorStatus dlz3 = getStatus();
        if (this.narrator.active() && dlz3 != NarratorStatus.OFF && dlz3 != NarratorStatus.CHAT && !string.isEmpty()) {
            this.narrator.clear();
            this.doSay(true, string);
        }
    }
    
    private static NarratorStatus getStatus() {
        return Minecraft.getInstance().options.narratorStatus;
    }
    
    private void doSay(final boolean boolean1, final String string) {
        if (SharedConstants.IS_RUNNING_IN_IDE) {
            NarratorChatListener.LOGGER.debug("Narrating: {}", string.replaceAll("\n", "\\\\n"));
        }
        this.narrator.say(string, boolean1);
    }
    
    public void updateNarratorStatus(final NarratorStatus dlz) {
        this.clear();
        this.narrator.say(new TranslatableComponent("options.narrator").append(" : ").append(dlz.getName()).getString(), true);
        final ToastComponent doo3 = Minecraft.getInstance().getToasts();
        if (this.narrator.active()) {
            if (dlz == NarratorStatus.OFF) {
                SystemToast.addOrUpdate(doo3, SystemToast.SystemToastIds.NARRATOR_TOGGLE, new TranslatableComponent("narrator.toast.disabled"), null);
            }
            else {
                SystemToast.addOrUpdate(doo3, SystemToast.SystemToastIds.NARRATOR_TOGGLE, new TranslatableComponent("narrator.toast.enabled"), dlz.getName());
            }
        }
        else {
            SystemToast.addOrUpdate(doo3, SystemToast.SystemToastIds.NARRATOR_TOGGLE, new TranslatableComponent("narrator.toast.disabled"), new TranslatableComponent("options.narrator.notavailable"));
        }
    }
    
    public boolean isActive() {
        return this.narrator.active();
    }
    
    public void clear() {
        if (getStatus() == NarratorStatus.OFF || !this.narrator.active()) {
            return;
        }
        this.narrator.clear();
    }
    
    public void destroy() {
        this.narrator.destroy();
    }
    
    static {
        NO_TITLE = TextComponent.EMPTY;
        LOGGER = LogManager.getLogger();
        INSTANCE = new NarratorChatListener();
    }
}
