package net.geforcemods.securitycraft.blockentities;

import net.geforcemods.securitycraft.SCContent;
import net.geforcemods.securitycraft.api.CustomizableBlockEntity;
import net.geforcemods.securitycraft.api.Option;
import net.geforcemods.securitycraft.api.Option.IntOption;
import net.geforcemods.securitycraft.blocks.mines.ClaymoreBlock;
import net.geforcemods.securitycraft.misc.ModuleType;
import net.geforcemods.securitycraft.util.EntityUtils;
import net.geforcemods.securitycraft.util.ITickingBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;

public class ClaymoreBlockEntity extends CustomizableBlockEntity implements ITickingBlockEntity
{
	private IntOption range = new IntOption(this::getBlockPos, "range", 5, 1, 10, 1, true);
	private double entityX = -1D;
	private double entityY = -1D;
	private double entityZ = -1D;
	private int cooldown = -1;

	public ClaymoreBlockEntity(BlockPos pos, BlockState state)
	{
		super(SCContent.beTypeClaymore, pos, state);
	}

	@Override
	public void tick(Level level, BlockPos pos, BlockState state) { //server only as per ClaymoreBlock
		if(state.getValue(ClaymoreBlock.DEACTIVATED))
			return;

		if(cooldown > 0){
			cooldown--;
			return;
		}

		if(cooldown == 0){
			((ClaymoreBlock)state.getBlock()).explode(level, pos);
			return;
		}

		Direction dir = state.getValue(ClaymoreBlock.FACING);
		AABB area = new AABB(pos);

		area = switch(dir) {
			case NORTH -> area.contract(0, 0, range.get());
			case SOUTH -> area.contract(0, 0, -range.get());
			case EAST -> area.contract(-range.get(), 0, 0);
			case WEST -> area.contract(range.get(), 0, 0);
			default -> area;
		};

		level.getEntitiesOfClass(LivingEntity.class, area, e -> !EntityUtils.isInvisible(e) && !e.isSpectator() && !EntityUtils.doesEntityOwn(e, level, pos))
		.stream().findFirst().ifPresent(entity -> {
			entityX = entity.getX();
			entityY = entity.getY();
			entityZ = entity.getZ();
			cooldown = 20;
			level.playSound(null, new BlockPos(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D), SoundEvents.LEVER_CLICK, SoundSource.BLOCKS, 0.3F, 0.6F);
		});
	}

	/**
	 * Writes a tile entity to NBT.
	 * @return
	 */
	@Override
	public CompoundTag save(CompoundTag tag)
	{
		super.save(tag);
		writeOptions(tag);
		tag.putInt("cooldown", cooldown);
		tag.putDouble("entityX", entityX);
		tag.putDouble("entityY", entityY);
		tag.putDouble("entityZ", entityZ);
		return tag;
	}

	/**
	 * Reads a tile entity from NBT.
	 */
	@Override
	public void load(CompoundTag tag)
	{
		super.load(tag);

		readOptions(tag);
		cooldown = tag.getInt("cooldown");
		entityX = tag.getDouble("entityX");
		entityY = tag.getDouble("entityY");
		entityZ = tag.getDouble("entityZ");
	}

	@Override
	public Option<?>[] customOptions()
	{
		return new Option[]{range};
	}

	@Override
	public ModuleType[] acceptedModules()
	{
		return new ModuleType[0];
	}
}

@@ -1074,101 +1074,8 @@
 e);%0A
-%09private double entityX = -1D;%0A%09private double entityY = -1D;%0A%09private double entityZ = -1D;%0A
 %09pri
@@ -2071,92 +2071,8 @@
 %3E %7B%0A
-%09%09%09entityX = entity.getX();%0A%09%09%09entityY = entity.getY();%0A%09%09%09entityZ = entity.getZ();%0A
 %09%09%09c
@@ -2253,63 +2253,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Writes a tile entity to NBT.%0A%09 * @return%0A%09 */%0A
 %09@Ov
@@ -2387,178 +2387,23 @@
 ;%0A%09%09
-tag.putDouble(%22entityX%22, entityX);%0A%09%09tag.putDouble(%22entityY%22, entityY);%0A%09%09tag.putDouble(%22entityZ%22, entityZ);%0A%09%09return tag;%0A%09%7D%0A%0A%09/**%0A%09 * Reads a tile entity from NBT.%0A%09 */
+return tag;%0A%09%7D%0A
 %0A%09@O
@@ -2529,122 +2529,8 @@
 %22);%0A
-%09%09entityX = tag.getDouble(%22entityX%22);%0A%09%09entityY = tag.getDouble(%22entityY%22);%0A%09%09entityZ = tag.getDouble(%22entityZ%22);%0A
 %09%7D%0A%0A

remove unused fields from claymore be