package me.km.snuviscript.commands;

import me.hammerle.snuviscript.code.ScriptManager;
import me.hammerle.snuviscript.code.SnuviUtils;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.play.server.STitlePacket;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;

public class TitleCommands {
    public static void registerFunctions(ScriptManager sm) {
        sm.registerConsumer("title.settime", (sc, in) -> {
            STitlePacket packet = new STitlePacket(in[1].getInt(sc), in[2].getInt(sc), in[3].getInt(sc));
            ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(packet);
        });
        sm.registerConsumer("title.clear", (sc, in) -> {
            ServerPlayerEntity p = (ServerPlayerEntity) in[0].get(sc);
            p.connection.sendPacket(new STitlePacket(STitlePacket.Type.CLEAR, null));
            p.connection.sendPacket(new STitlePacket(STitlePacket.Type.RESET, null));
        });
        sm.registerConsumer("title.reset", (sc, in) -> {
            ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new STitlePacket(STitlePacket.Type.RESET, null));
        });
        sm.registerConsumer("title.send", (sc, in) -> {
            ITextComponent text = new StringTextComponent(SnuviUtils.connect(sc, in, 1));
            ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new STitlePacket(STitlePacket.Type.TITLE, text));
        });
        sm.registerConsumer("title.setsub", (sc, in) -> {
            ITextComponent text = new StringTextComponent(SnuviUtils.connect(sc, in, 1));
            ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new STitlePacket(STitlePacket.Type.SUBTITLE, text));
        });
    }
}
