package eu.mhsl.minecraft.pixelblocks.pixelblock;

import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.utils.EntityTagUtil;
import eu.mhsl.minecraft.pixelblocks.utils.MinMaxUtil;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Interaction;

import java.util.List;

public class PixelBlockHitbox {
    private static final NamespacedKey hitboxOfTag = new NamespacedKey(Main.plugin(), "hitbox_of");

    private final PixelBlock parentBlock;

    public PixelBlockHitbox(PixelBlock parentBlock) {
        this.parentBlock = parentBlock;
    }

    public void spawn() {
        Location absoluteLocation = this.parentBlock.getPixelBlockLocation();
        List<PixelBlockWorld.PixelData> pixels = this.parentBlock.getPixelData();
        int pixelsPerBlock = Main.configuration().pixelsPerBlock();

        Interaction interaction;
        if (pixels.size() <= 5) {
            interaction = (Interaction) absoluteLocation.getWorld().spawnEntity(
                absoluteLocation.clone().add(0.5, 0, 0.5),
                EntityType.INTERACTION
            );
            interaction.setInteractionHeight(1);
            interaction.setInteractionWidth(1);
        } else {
            double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation().getX());
            double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation().getY());
            double startingZ = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation().getZ());

            double endingX = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation().getX());
            double endingY = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation().getY());
            double endingZ = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation().getZ());

            Location spawnLocation = absoluteLocation.clone().add(
                ((startingX+endingX)/2+0.5)/pixelsPerBlock,
                startingY/pixelsPerBlock,
                ((startingZ+endingZ)/2+0.5)/pixelsPerBlock
            );

            float height = (float) (endingY-startingY+1)/pixelsPerBlock;

            float width;
            if((endingX-startingX) > (endingZ-startingZ)) {
                width = (float) (endingX-startingX+1)/pixelsPerBlock;
            } else {
                width = (float) (endingZ-startingZ+1)/pixelsPerBlock;
            }

            if(spawnLocation.getX()+width/2 > absoluteLocation.getX()+1) {
                spawnLocation.subtract((spawnLocation.getX()+width/2)-(absoluteLocation.getX()+1), 0, 0);
            }
            if(spawnLocation.getX()-width/2 < absoluteLocation.getX()) {
                spawnLocation.add(absoluteLocation.getX()-(spawnLocation.getX()-width/2), 0, 0);
            }

            if(spawnLocation.getZ()+width/2 > absoluteLocation.getZ()+1) {
                spawnLocation.subtract(0, 0, (spawnLocation.getZ()+width/2)-(absoluteLocation.getZ()+1));
            }
            if(spawnLocation.getZ()-width/2 < absoluteLocation.getZ()) {
                spawnLocation.add(0, 0, absoluteLocation.getZ()-(spawnLocation.getZ()-width/2));
            }

            interaction = (Interaction) absoluteLocation.getWorld().spawnEntity(
                spawnLocation,
                EntityType.INTERACTION
            );

            interaction.setInteractionHeight(height);
            interaction.setInteractionWidth(width);
        }

        EntityTagUtil.tag(interaction, hitboxOfTag, this.parentBlock.getBlockUUID());
    }

    public void destroy() {
        EntityTagUtil.removeTagged(this.parentBlock.getPixelBlockLocation(), Interaction.class, hitboxOfTag, this.parentBlock.getBlockUUID());
    }
}
