package me.km.events;

import java.util.UUID;
import me.hammerle.snuviscript.code.Script;
import me.km.Server;
import me.km.utils.Location;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.world.IWorld;

public class PlayerMoveData {
    private static int idCounter = 0;

    private final Script sc;
    private final int coolDown;
    private int ticks;
    private int livingTime;

    private final IWorld w;
    private final double minX;
    private final double minY;
    private final double minZ;
    private final double maxX;
    private final double maxY;
    private final double maxZ;

    private final int id = idCounter++;
    private final UUID uuid;

    public PlayerMoveData(Script sc, Location l1, Location l2, int cooldown, int livingTime, UUID uuid) {
        this.sc = sc;
        this.coolDown = cooldown;
        this.ticks = cooldown;
        this.livingTime = livingTime;
        this.w = l1.getWorld();
        this.minX = Math.min(l1.getX(), l2.getX());
        this.minY = Math.min(l1.getY(), l2.getY());
        this.minZ = Math.min(l1.getZ(), l2.getZ());
        this.maxX = Math.max(l1.getX(), l2.getX());
        this.maxY = Math.max(l1.getY(), l2.getY());
        this.maxZ = Math.max(l1.getZ(), l2.getZ());
        this.uuid = uuid;
    }

    public int getId() {
        return id;
    }

    public boolean isSameScript(Script sc) {
        return this.sc == sc;
    }

    public boolean tickLiving() {
        if(livingTime == -1) {
            return false;
        }
        livingTime--;
        return livingTime < 0;
    }

    public boolean tick() {
        ticks--;
        if(ticks > 0) {
            return true;
        }
        ticks = coolDown;
        return false;
    }

    public boolean check(ServerPlayerEntity p) {
        double posX = p.getPosX();
        double posY = p.getPosY();
        double posZ = p.getPosZ();
        if(p.world != w || posX < minX || posX > maxX || posZ < minZ || posZ > maxZ || posY < minY || posY > maxY) {
            return false;
        }
        boolean b = (uuid == null || p.getUniqueID().equals(uuid));
        if(sc != null && b) {
            Server.scriptEvents.onPlayerMove(p, id);
        }
        return coolDown == -1 && b;
    }
}
