package eu.mhsl.minenet.minigames.instance.game.stateless.types.turtleGame.gameObjects;

import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.attribute.Attribute;
import net.minestom.server.timer.Task;
import net.minestom.server.timer.TaskSchedule;

public class Turtle extends EntityCreature {
    private final Player player;
    private int score = 0;
    private double speed;
    private double boostSpeedMultiplier = 1;
    private float boostChargeLevel = 0f;
    private Task boostTask;
    private Task boostRefillTask;

    public Turtle(Player player, double speed) {
        super(EntityType.TURTLE);
        this.player = player;
        this.speed = speed;

        this.getAttribute(Attribute.MOVEMENT_SPEED).setBaseValue(0.15);
        this.getAttribute(Attribute.MAX_HEALTH).setBaseValue(1);
        this.player.setExp(this.boostChargeLevel);
    }

    public void spawnTurtle() {
        this.setInstance(this.player.getInstance(), this.player.getPosition());
        this.addPassenger(this.player);
    }

    public void startBoostRefill() {
        this.boostRefillTask = MinecraftServer.getSchedulerManager().scheduleTask(() -> {
            if(this.boostChargeLevel >= 1f) return;
            this.increaseBoostChargeLevel(0.02f);
        }, TaskSchedule.seconds(1), TaskSchedule.seconds(1));
    }

    public void destroy() {
        this.removePassenger(this.player);
        this.remove();
        if(this.boostRefillTask != null && this.boostRefillTask.isAlive()) this.boostRefillTask.cancel();
        if(this.boostTask != null && this.boostTask.isAlive()) this.boostTask.cancel();
    }

    public void adaptView() {
        if(this.getInstance() == null) return;
        this.teleport(this.getPosition().withView(this.player.getPosition().withPitch(this.getPosition().pitch())));
        Vec lookingVector = this.player.getPosition().direction().withY(0).mul(100);
        this.lookAt(this.getPosition().add(lookingVector.asPosition()));
    }

    public void move() {
        this.adaptView();
        Vec direction = this.player.getPosition().direction();
        Vec movementVector = direction.withY(0).normalize().mul(this.speed * this.boostSpeedMultiplier);
        this.setVelocity(movementVector);
    }

    public boolean checkCollisionWithEntity(Entity other) {
        return this.getBoundingBox().intersectBox(other.getPosition().sub(this.getPosition()), other.getBoundingBox());
    }

    public void boostSpeed() {
        if(this.boostChargeLevel <= 0f) return;
        this.boostSpeedMultiplier = 3.5;
        this.boostTask = MinecraftServer.getSchedulerManager().scheduleTask(() -> {
            if(this.boostChargeLevel <= 0f) {
                this.cancelBoost();
                return;
            }
            this.boostChargeLevel = Math.max(0f, this.boostChargeLevel - 0.025f);
            this.player.setExp(this.boostChargeLevel);
        }, TaskSchedule.millis(30), TaskSchedule.millis(30));
    }

    public void cancelBoost() {
        if(this.boostTask == null || !this.boostTask.isAlive()) return;
        this.boostTask.cancel();
        this.boostSpeedMultiplier = 1;
    }

    public void increaseBoostChargeLevel(float amount) {
        this.boostChargeLevel = Math.min(1f, this.boostChargeLevel + amount);
        this.player.setExp(this.boostChargeLevel);
    }

    public void addSpeed(double amount) {
        this.speed += amount;
    }

    public int getScore() {
        return this.score;
    }

    public double getBombBorder() {
        // 1 bei speed 2; 2 bei speed 4; 4 bei speed 8
        return Math.clamp((this.speed * this.boostSpeedMultiplier) / 2, 1.5, 4);
    }

    public void increaseScore() {
        this.score += 1;
        this.player.setLevel(this.score);
    }
}
