import { blockEntityManager } from "../blockEntitiesManager";
import { SuppEntities } from "../reg/suppEntities";
import { SuppStates } from "../reg/suppStates";
import { getADirection, offsetPos } from "../util/dirUtils";
import { SuppParticles } from "../reg/suppParticles";
import { UIBuilder } from "../util/uiBuilder";
import { configs } from "../configs";
import { translate } from "../util/miscUtils";
const VOLUME = "razz_sup:volume";
const TEXT = "razz_sup:text";
blockEntityManager.registerEntityBlock("razz_sup:speaker_block" /* SuppBlocks.Speaker */, SuppEntities.Speaker, {
    isOptional() {
        return true;
    }
});
export const SpeakerBlockInteractComponent = {
    onPlayerInteract(event) {
        let block = event.block;
        const player = event.player;
        if (!player)
            return;
        const be = blockEntityManager.getOrCreateBlockEntity(block);
        const defaultVolume = configs.speakerMaxRange.getValue();
        const volume = be.getDynamicProperty(VOLUME) ?? defaultVolume;
        const text = be.getDynamicProperty(TEXT) ?? "";
        const ui = UIBuilder.create("speaker")
            .formPage()
            .text("message", text)
            .slider("volume", 8, 128, volume)
            .onSubmit((r, b) => {
            const newText = r.text[0];
            const newVolume = r.slider[0];
            be.setDynamicProperty(TEXT, newText);
            be.setDynamicProperty(VOLUME, newVolume);
            if (newText.length == 0 && newVolume == defaultVolume) {
                be.remove();
            }
        })
            .onFail(() => {
            if (text.length == 0) {
                be.remove();
            }
        })
            .build();
        ui.open(player);
    }
};
export const SpeakerBlockTickComponent = {
    onTick(event) {
        const block = event.block;
        const power = block.getRedstonePower() | 0;
        const powerUp = power > 0;
        if (powerUp != block.permutation.getState(SuppStates.Powered)) {
            block.setPermutation(block.permutation.withState(SuppStates.Powered, powerUp));
            if (powerUp) {
                const dir = getADirection(block);
                const inFront = offsetPos(block, dir);
                if (inFront.isAir) {
                    const be = blockEntityManager.getOrCreateBlockEntity(block);
                    //sent message
                    const volume = be.getDynamicProperty(VOLUME);
                    let text = be.getDynamicProperty(TEXT);
                    if (volume && text && text.length > 0) {
                        const players = event.dimension.getPlayers({ maxDistance: volume, location: block.location });
                        text = censorTextIfNeeded(text);
                        const message = configs.speakerPrefix.getValue() ? translate("message", "speaker", [text]) : text;
                        for (const player of players) {
                            player.sendMessage(message);
                        }
                    }
                    inFront.dimension.spawnParticle(SuppParticles.Speaker, inFront.center());
                }
            }
        }
    }
};
function censorTextIfNeeded(text) {
    const regexString = configs.speakerCensor.getValue();
    if (regexString.length == 0)
        return text;
    try {
        const regex = new RegExp(regexString, "gi"); // "gi" for global & case-insensitive matching
        return text.replace(regex, (match) => '*'.repeat(match.length));
    }
    catch (e) {
    }
    return text;
}
