package net.geforcemods.securitycraft.blockentities;

import java.util.ArrayList;

import net.geforcemods.securitycraft.api.LinkableBlockEntity;
import net.geforcemods.securitycraft.api.LinkedAction;
import net.geforcemods.securitycraft.api.Option;
import net.geforcemods.securitycraft.api.Option.BooleanOption;
import net.geforcemods.securitycraft.api.Option.IntOption;
import net.geforcemods.securitycraft.misc.ModuleType;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.DoorBlock;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;

public abstract class SpecialDoorBlockEntity extends LinkableBlockEntity {
	private BooleanOption sendMessage = new BooleanOption("sendMessage", true);
	private IntOption signalLength = new IntOption(this::getBlockPos, "signalLength", defaultSignalLength(), 0, 400, 5, true); //20 seconds max

	public SpecialDoorBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
		super(type, pos, state);
	}

	@Override
	public void onOwnerChanged(BlockState state, Level level, BlockPos pos, Player player) {
		pos = state.getValue(BlockStateProperties.DOUBLE_BLOCK_HALF) == DoubleBlockHalf.UPPER ? pos.below() : pos.above();

		if (level.getBlockEntity(pos) instanceof SpecialDoorBlockEntity be && isLinkedWith(this, be)) {
			be.setOwner(getOwner().getUUID(), getOwner().getName());

			if (!level.isClientSide)
				level.getServer().getPlayerList().broadcastAll(be.getUpdatePacket());
		}

		super.onOwnerChanged(state, level, pos, player);
	}

	@Override
	public void onModuleEnabled(ItemStack stack, ModuleType module) {
		handleModule(stack, module, false);
		super.onModuleEnabled(stack, module);
	}

	@Override
	public void onModuleDisabled(ItemStack stack, ModuleType module) {
		handleModule(stack, module, true);
		super.onModuleDisabled(stack, module);
	}

	private void handleModule(ItemStack stack, ModuleType module, boolean removed) {
		DoubleBlockHalf myHalf = getBlockState().getValue(DoorBlock.HALF);
		BlockPos otherPos;

		if (myHalf == DoubleBlockHalf.UPPER)
			otherPos = getBlockPos().below();
		else
			otherPos = getBlockPos().above();

		BlockState other = level.getBlockState(otherPos);

		if (other.getValue(DoorBlock.HALF) != myHalf) {
			if (level.getBlockEntity(otherPos) instanceof SpecialDoorBlockEntity otherDoorBe) {
				if (!removed && !otherDoorBe.isModuleEnabled(module))
					otherDoorBe.insertModule(stack);
				else if (removed && otherDoorBe.isModuleEnabled(module))
					otherDoorBe.removeModule(module);
			}
		}
	}

	@Override
	protected void onLinkedBlockAction(LinkedAction action, Object[] parameters, ArrayList<LinkableBlockEntity> excludedBEs) {
		if (action == LinkedAction.OPTION_CHANGED) {
			Option<?> option = (Option<?>) parameters[0];

			if (option.getName().equals(sendMessage.getName()))
				sendMessage.copy(option);
			else if (option.getName().equals(signalLength.getName()))
				signalLength.copy(option);

			setChanged();
		}
	}

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

	public boolean sendsMessages() {
		return sendMessage.get();
	}

	public int getSignalLength() {
		return signalLength.get();
	}

	public abstract int defaultSignalLength();
}

@@ -2309,21 +2309,22 @@
 boolean 
-remov
+disabl
 ed) %7B%0D%0A%09
@@ -2749,20 +2749,30 @@
 f (!
-removed && !
+disabled) %7B%0D%0A%09%09%09%09%09if (
 othe
@@ -2775,39 +2775,33 @@
 otherDoorBe.
-i
+ha
 sModule
-Enabled
 (module))%0D%0A%09
@@ -2789,34 +2789,87 @@
 sModule(module))
+%0D%0A%09%09%09%09%09%09otherDoorBe.enableModule(module);%0D%0A%09%09%09%09%09else
 %0D%0A
+%09
 %09%09%09%09%09otherDoorBe
@@ -2887,24 +2887,31 @@
 le(stack);%0D%0A
+%09%09%09%09%7D%0D%0A
 %09%09%09%09else if 
@@ -2915,19 +2915,56 @@
 if (
-removed && 
+disabled) %7B%0D%0A%09%09%09%09%09if (hasModule(module))%0D%0A%09%09%09%09%09%09
 othe
@@ -2975,31 +2975,29 @@
 rBe.
+d
 is
+able
 Module
-Enabled
 (module)
 )%0D%0A%09
@@ -2988,27 +2988,39 @@
 dule(module)
-)
+;
 %0D%0A
+%09%09%09%09%09else%0D%0A%09
 %09%09%09%09%09otherDo
@@ -3047,16 +3047,23 @@
 dule);%0D%0A
+%09%09%09%09%7D%0D%0A
 %09%09%09%7D%0D%0A%09%09

fix toggling modules not working for doors