package eu.mhsl.minenet.minigames.instance.game.stateless.types.stickfight;

import eu.mhsl.minenet.minigames.instance.Dimension;
import eu.mhsl.minenet.minigames.instance.game.stateless.StatelessGame;
import eu.mhsl.minenet.minigames.score.LastWinsScore;
import eu.mhsl.minenet.minigames.util.BatchUtil;
import eu.mhsl.minenet.minigames.world.generator.terrain.CircularPlateTerrainGenerator;
import io.github.togar2.pvp.config.PvPConfig;
import io.github.togar2.pvp.events.FinalAttackEvent;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerMoveEvent;
import net.minestom.server.instance.batch.AbsoluteBlockBatch;
import net.minestom.server.instance.block.Block;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CompletableFuture;

public class Stickfight extends StatelessGame {
    public Stickfight() {
        super(Dimension.OVERWORLD.key, "Stickfight", new LastWinsScore());

//        eventNode().addChild( // TODO update
//                PvPConfig.emptyBuilder()
//                        .damage(DamageConfig.legacyBuilder().fallDamage(false))
//                        .attack(AttackConfig.legacyBuilder().attackCooldown(true))
//                        .build().createNode()
//        );

        eventNode().addListener(FinalAttackEvent.class, finalAttackEvent -> {
            finalAttackEvent.setBaseDamage(0);
            ((Player) finalAttackEvent.getTarget()).setHealth(20);
        });

        setGenerator(new CircularPlateTerrainGenerator(20));
    }

    @Override
    protected void onLoad(@NotNull CompletableFuture<Void> callback) {
        AbsoluteBlockBatch batch = new AbsoluteBlockBatch();
        for (int z = -10; z <= 10; z++) {
            batch.setBlock(0, 50, z, Block.SANDSTONE);
        }
        batch.setBlock(0, 50, 0, Block.GOLD_BLOCK);

        BatchUtil.loadAndApplyBatch(batch, this, () -> callback.complete(null));
    }

    @Override
    protected void onPlayerMove(@NotNull PlayerMoveEvent playerMoveEvent) {
        if(isBeforeBeginning) playerMoveEvent.setCancelled(true);

        if(playerMoveEvent.getNewPosition().y() < 40) {
            playerMoveEvent.getPlayer().teleport(new Pos(0, 51, 0));
        }
    }

    @Override
    public Pos getSpawn() {
        return new Pos(0.5, 51, 0.5);
    }
}
