package com.loganmagnan.eventcore.listeners;

import com.loganmagnan.eventcore.EventCore;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.InventoryCreativeEvent;
import org.bukkit.event.inventory.InventoryInteractEvent;
import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;

public class StaffModeListener implements Listener {

    private EventCore main = EventCore.getInstance();

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();

        for (Player playerOnline : this.main.getServer().getOnlinePlayers()) {
            for (UUID playerVanishedUUID : this.main.getStaffManager().getPlayersVanished()) {
                if (player == playerOnline) {
                    continue;
                }

                Player playerVanished = this.main.getServer().getPlayer(playerVanishedUUID);

                if (playerVanished == null) {
                    continue;
                }

                playerOnline.hidePlayer(playerVanished);
            }
        }

        if (!player.hasPermission("eventcore.command.staffmode")) {
            return;
        }

        this.main.getStaffManager().setStaffMode(player, true);
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();

        if (!this.main.getStaffManager().isPlayerInStaffMode(player)) {
            return;
        }

        this.main.getStaffManager().setStaffMode(player, false);
    }

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        Player player = event.getPlayer();

        if (!this.main.getStaffManager().isPlayerInStaffMode(player)) {
            return;
        }

        if (this.main.getStaffManager().getPlayersBypassed().contains(player.getUniqueId())) {
            return;
        }

        event.setCancelled(true);
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();

        if (!this.main.getStaffManager().isPlayerInStaffMode(player)) {
            return;
        }

        if (this.main.getStaffManager().getPlayersBypassed().contains(player.getUniqueId())) {
            return;
        }

        event.setCancelled(true);
    }

    @EventHandler
    public void onPlayerPickupItem(PlayerPickupItemEvent event) {
        Player player = event.getPlayer();

        if (!this.main.getStaffManager().isPlayerInStaffMode(player)) {
            return;
        }

        event.setCancelled(true);
    }

    @EventHandler
    public void onPlayerDropItem(PlayerDropItemEvent event) {
        Player player = event.getPlayer();

        if (!this.main.getStaffManager().isPlayerInStaffMode(player)) {
            return;
        }

        event.setCancelled(true);
    }

    @EventHandler
    public void onInventoryCreative(InventoryCreativeEvent event) {
        HumanEntity humanEntity = event.getWhoClicked();

        if (!(humanEntity instanceof Player)) {
            return;
        }

        Player player = (Player) humanEntity;

        if (!this.main.getStaffManager().isPlayerInStaffMode(player)) {
            return;
        }

        if (this.main.getStaffManager().getPlayersBypassed().contains(player.getUniqueId())) {
            return;
        }

        event.setCancelled(true);
    }

    @EventHandler
    public void onInventoryInteract(InventoryInteractEvent event) {
        HumanEntity humanEntity = event.getWhoClicked();

        if (!(humanEntity instanceof Player)) {
            return;
        }

        Player player = (Player) humanEntity;

        if (!this.main.getStaffManager().isPlayerInStaffMode(player)) {
            return;
        }

        if (this.main.getStaffManager().getPlayersBypassed().contains(player.getUniqueId())) {
            return;
        }

        event.setCancelled(true);
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();

        if (!this.main.getStaffManager().isPlayerInStaffMode(player)) {
            return;
        }

        if (this.main.getStaffManager().getPlayersBypassed().contains(player.getUniqueId())) {
            return;
        }

        event.setCancelled(true);
    }

    @EventHandler
    public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
        Player player = event.getPlayer();

        ItemStack itemStack = player.getItemInHand();

        if (!this.main.getStaffManager().isPlayerInStaffMode(player)) {
            return;
        }

        event.setCancelled(true);

        if (itemStack.getType() != Material.BOOK) {
            return;
        }

        if (!(event.getRightClicked() instanceof Player)) {
            return;
        }

        Player target = (Player) event.getRightClicked();

        player.openInventory(target.getInventory());
    }

    @EventHandler
    public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
        Entity entity = event.getEntity();

        if (!(entity instanceof Player)) {
            return;
        }

        Player player = (Player) entity;

        if (!this.main.getStaffManager().isPlayerVanished(player)) {
            return;
        }

        event.setCancelled(true);
    }
}
