package land.chipmunk.chipmunkmod.modules;

import com.google.common.hash.Hashing;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.listeners.Listener;
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.text.PlainTextContent;
import net.minecraft.text.Text;
import net.minecraft.text.TextContent;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class ChomeNSAuth extends Listener {
    public static final ChomeNSAuth INSTANCE = new ChomeNSAuth();

    public final String id = "chomens_bot_verify";

    public ChomeNSAuth () {
        ListenerManager.addListener(this);
    }

    public void init () {}

    @Override
    public void chatMessageReceived(Text text) {
        final String authKey = ChipmunkMod.CONFIG.bots.chomens.authKey;

        if (authKey == null) return;

        final TextContent message = text.getContent();

        if (!(message instanceof PlainTextContent)) return;

        final String id = ((PlainTextContent) message).string();

        if (!id.equals(this.id)) return;

        final List<Text> children = text.getSiblings();

        if (children.size() != 2) return;

        if (!(children.getFirst().getContent() instanceof PlainTextContent)) return;

        final String hash = ((PlainTextContent) children.getFirst().getContent()).string();

        final long time = System.currentTimeMillis() / 10_000;

        final String actual = Hashing.sha256()
                // very pro hash input
                .hashString(authKey + time, StandardCharsets.UTF_8)
                .toString()
                .substring(0, 8);

        if (!hash.equals(actual)) return;

        if (!(children.get(1).getContent() instanceof PlainTextContent)) return;

        final String selector = ((PlainTextContent) children.get(1).getContent()).string();

        final String toSendHash = Hashing.sha256()
                // very pro hash input
                .hashString(authKey + authKey + time + time, StandardCharsets.UTF_8)
                .toString()
                .substring(0, 8);

        final Component toSend = Component.text(id)
                        .append(Component.text(toSendHash));

        final String toSendString = GsonComponentSerializer.gson().serialize(toSend);

        System.out.println("Sending " + toSendString + " to " + selector);

        CommandCore.INSTANCE.run("tellraw " + selector + " " + toSendString);

        CustomChat.INSTANCE.resetTotal();
    }
}
