/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pw.yumc.Residence.listeners;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockIgniteEvent.IgniteCause;

import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import com.bekvon.bukkit.residence.protection.FlagPermissions;

import pw.yumc.Residence.ResidenceMain;

import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockSpreadEvent;

/**
 *
 * @author Administrator
 */
public class ResidenceBlockListener implements Listener {
    ResidenceMain plugin;

    public ResidenceBlockListener(final ResidenceMain plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onBlockBreak(final BlockBreakEvent event) {
        final Player player = event.getPlayer();
        if (plugin.isResAdminOn(player)) {
            return;
        }
        final Material mat = event.getBlock().getType();
        final String world = event.getBlock().getWorld().getName();
        final String group = plugin.getPermissionManager().getGroupNameByPlayer(player);
        if (plugin.getItemManager().isIgnored(mat, group, world)) {
            return;
        }
        final ClaimedResidence res = plugin.getResidenceManager().getByLoc(event.getBlock().getLocation());
        if (plugin.getConfigManager().enabledRentSystem()) {
            if (res != null) {
                final String resname = res.getName();
                if (plugin.getConfigManager().preventRentModify() && plugin.getRentManager().isRented(resname)) {
                    player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("RentedModifyDeny"));
                    event.setCancelled(true);
                    return;
                }
            }
        }
        final FlagPermissions perms = plugin.getPermsByLocForPlayer(event.getBlock().getLocation(), player);
        final String pname = player.getName();
        if (res != null) {
            if (res.getItemIgnoreList().isListed(mat)) {
                return;
            }
        }
        final boolean hasdestroy = perms.playerHas(pname, player.getWorld().getName(), "destroy", perms.playerHas(pname, player.getWorld().getName(), "build", true));
        final boolean hasContainer = perms.playerHas(pname, player.getWorld().getName(), "container", true);
        if (!hasdestroy || (!hasContainer && mat == Material.CHEST)) {
            event.setCancelled(true);
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoPermission"));
            return;
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onBlockBurn(final BlockBurnEvent event) {
        final FlagPermissions perms = plugin.getPermsByLoc(event.getBlock().getLocation());
        if (!perms.has("firespread", true)) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onBlockFromTo(final BlockFromToEvent event) {
        final FlagPermissions perms = plugin.getPermsByLoc(event.getToBlock().getLocation());
        final boolean hasflow = perms.has("flow", true);
        final Material mat = event.getBlock().getType();
        if (!hasflow) {
            event.setCancelled(true);
            return;
        }
        if (mat == Material.LAVA || mat == Material.STATIONARY_LAVA) {
            if (!perms.has("lavaflow", hasflow)) {
                event.setCancelled(true);
            }
            return;
        }
        if (mat == Material.WATER || mat == Material.STATIONARY_WATER) {
            if (!perms.has("waterflow", hasflow)) {
                event.setCancelled(true);
            }
            return;
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onBlockIgnite(final BlockIgniteEvent event) {
        final FlagPermissions perms = plugin.getPermsByLocForPlayer(event.getBlock().getLocation(), event.getPlayer());
        final IgniteCause cause = event.getCause();
        if (cause == IgniteCause.SPREAD) {
            if (!perms.has("firespread", true)) {
                event.setCancelled(true);
            }
        } else if (cause == IgniteCause.FLINT_AND_STEEL) {
            final Player player = event.getPlayer();
            if (player != null && !perms.playerHas(player.getName(), player.getWorld().getName(), "ignite", true) && !plugin.isResAdminOn(player)) {
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoPermission"));
            }
        } else {
            if (!perms.has("ignite", true)) {
                event.setCancelled(true);
            }
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onBlockPistonExtend(final BlockPistonExtendEvent event) {
        final FlagPermissions perms = plugin.getPermsByLoc(event.getBlock().getLocation());
        if (!perms.has("piston", true)) {
            event.setCancelled(true);
        }
        for (final Block block : event.getBlocks()) {
            final FlagPermissions blockpermsfrom = plugin.getPermsByLoc(block.getLocation());
            final Location blockto = block.getLocation();
            blockto.setX(blockto.getX() + event.getDirection().getModX());
            blockto.setY(blockto.getY() + event.getDirection().getModY());
            blockto.setZ(blockto.getZ() + event.getDirection().getModZ());
            final FlagPermissions blockpermsto = plugin.getPermsByLoc(blockto);
            if (!blockpermsfrom.has("piston", true) || !blockpermsto.has("piston", true)) {
                event.setCancelled(true);
                return;
            }
        }
    }

    @SuppressWarnings("deprecation")
    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onBlockPistonRetract(final BlockPistonRetractEvent event) {
        final FlagPermissions perms = plugin.getPermsByLoc(event.getBlock().getLocation());
        if (!perms.has("piston", true)) {
            event.setCancelled(true);
            return;
        }
        if (event.isSticky()) {
            final Location location = event.getRetractLocation();
            final FlagPermissions blockperms = plugin.getPermsByLoc(location);
            if (!blockperms.has("piston", true)) {
                event.setCancelled(true);
            }
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onBlockPlace(final BlockPlaceEvent event) {
        final Player player = event.getPlayer();
        if (plugin.isResAdminOn(player)) {
            return;
        }
        final Material mat = event.getBlock().getType();
        final String world = event.getBlock().getWorld().getName();
        final String group = plugin.getPermissionManager().getGroupNameByPlayer(player);
        if (plugin.getItemManager().isIgnored(mat, group, world)) {
            return;
        }
        final ClaimedResidence res = plugin.getResidenceManager().getByLoc(event.getBlock().getLocation());
        if (plugin.getConfigManager().enabledRentSystem()) {
            if (res != null) {
                final String resname = res.getName();
                if (plugin.getConfigManager().preventRentModify() && plugin.getRentManager().isRented(resname)) {
                    player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("RentedModifyDeny"));
                    event.setCancelled(true);
                    return;
                }
            }
        }
        final String pname = player.getName();
        if (res != null) {
            if (!res.getItemBlacklist().isAllowed(mat)) {
                player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("ItemBlacklisted"));
                event.setCancelled(true);
                return;
            }
        }
        final FlagPermissions perms = plugin.getPermsByLocForPlayer(event.getBlock().getLocation(), player);
        final boolean hasplace = perms.playerHas(pname, player.getWorld().getName(), "place", perms.playerHas(pname, player.getWorld().getName(), "build", true));
        if (!hasplace) {
            event.setCancelled(true);
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoPermission"));
            return;
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onBlockSpread(final BlockSpreadEvent event) {
        final Location loc = event.getBlock().getLocation();
        final FlagPermissions perms = plugin.getPermsByLoc(loc);
        if (!perms.has("spread", true)) {
            event.setCancelled(true);
        }
    }
}
