package eu.mhsl.minenet.minigames.instance.room.entity;

import eu.mhsl.minenet.minigames.instance.room.Room;
import eu.mhsl.minenet.minigames.instance.room.inventory.MinigameSelectInventory;
import eu.mhsl.minenet.minigames.message.Icon;
import eu.mhsl.minenet.minigames.message.type.ChatMessage;
import eu.mhsl.minenet.minigames.shared.entity.InteractableEntity;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.metadata.villager.AbstractVillagerMeta;
import net.minestom.server.event.entity.EntityAttackEvent;
import net.minestom.server.event.instance.AddEntityToInstanceEvent;
import net.minestom.server.event.player.PlayerEntityInteractEvent;
import org.jetbrains.annotations.NotNull;

public class GameSelector extends InteractableEntity {
    final AbstractVillagerMeta abstractVillagerMeta;
    public GameSelector() {
        super(EntityType.VILLAGER);

        abstractVillagerMeta = (AbstractVillagerMeta) this.getEntityMeta();
    }

    @Override
    public void onSpawn(@NotNull AddEntityToInstanceEvent addEntityToInstanceEvent) {

    }

    @Override
    public void onAttack(@NotNull EntityAttackEvent entityAttackEvent) {
        super.onAttack(entityAttackEvent);
    }

    @Override
    public void onInteract(@NotNull PlayerEntityInteractEvent playerEntityInteractEvent) {
        Room room = (Room) instance;

        if(playerEntityInteractEvent.getPlayer() != room.getOwner()) {
            abstractVillagerMeta.setHeadShakeTimer(20);

            new ChatMessage(Icon.ERROR).appendStatic("Only the room leader can start games!").send(playerEntityInteractEvent.getPlayer());
            return;
        }

        playerEntityInteractEvent.getPlayer().openInventory(new MinigameSelectInventory(room, playerEntityInteractEvent.getPlayer()));
    }
}
