package nl.sbdeveloper.themeparkplus.listeners;

import nl.sbdeveloper.themeparkplus.ThemeParkPlus;
import nl.sbdeveloper.themeparkplus.api.PlusAPI;
import nl.sbdeveloper.themeparkplus.api.enums.WalkingDirection;
import nl.sbdeveloper.themeparkplus.api.objects.Gate;
import nl.sbdeveloper.themeparkplus.util.ConfigUtil;
import nl.sbdeveloper.themeparkplus.util.LGUtil;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;

/**
 * Gate move listeners (directional and count checks)
 */
public class DirectionalGateListener implements Listener {
    @EventHandler
    public void onWalkThroughFenceGate(PlayerMoveEvent e) {
        if (e.getTo() != null
            && (e.getFrom().getBlockX() != e.getTo().getBlockX()
            || e.getFrom().getBlockY() != e.getTo().getBlockY()
            || e.getFrom().getBlockZ() != e.getTo().getBlockZ())) {
            Location oldLoc = e.getFrom();
            Location newLoc = e.getTo();
            Block oldBlock = oldLoc.getBlock();

            if (oldBlock.getType().name().contains("FENCE_GATE")) {
                Gate gate = PlusAPI.getGate(oldBlock.getLocation());
                if (gate == null) return;

                if (!gate.isDirectional()) {
                    if (gate.getMaxCount() != 0) {
                        if (gate.getPassedCount() + 1 == gate.getMaxCount()) {
                            if (LGUtil.isOpenable(oldBlock)) {
                                LGUtil.closeGate(oldBlock);
                            } else {
                                PlusAPI.removeGate(gate);
                                ThemeParkPlus.getData().save();
                                return;
                            }
                            PlusAPI.removeGate(gate);
                            ThemeParkPlus.getData().save();
                        } else {
                            gate.setPassedCount(gate.getPassedCount() + 1);
                        }
                    }
                } else {
                    WalkingDirection loopdir = WalkingDirection.getDirection(oldLoc.getBlockX(), oldLoc.getBlockZ(), newLoc.getBlockX(), newLoc.getBlockZ());
                    if (loopdir == null) return;
                    if (loopdir.getBlockFace() != gate.getDirection().getBlockFace()) {
                        e.getPlayer().teleport(oldLoc);
                        e.getPlayer().sendMessage(ConfigUtil.getMessage("Gates.WrongDir"));
                        return;
                    }

                    if (gate.getMaxCount() != 0) {
                        if (gate.getPassedCount() + 1 == gate.getMaxCount()) {
                            if (LGUtil.isOpenable(oldBlock)) {
                                LGUtil.closeGate(oldBlock);
                            } else {
                                PlusAPI.removeGate(gate);
                                ThemeParkPlus.getData().save();
                                return;
                            }
                            PlusAPI.removeGate(gate);
                            ThemeParkPlus.getData().save();
                        } else {
                            gate.setPassedCount(gate.getPassedCount() + 1);
                        }
                    }
                }
            }
        }
    }
}
