/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pw.yumc.Residence.selection;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;

import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
import com.sk89q.worldedit.bukkit.selections.Selection;
import com.sk89q.worldedit.regions.CuboidRegion;

import pw.yumc.Residence.ResidenceMain;

/**
 *
 * @author Administrator
 */
public class WorldEditSelectionManager extends SelectionManager {
    ResidenceMain plugin;

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

    @Override
    public void bedrock(final Player player, final boolean resadmin) {
        this.worldEdit(player);
        super.bedrock(player, resadmin);
        afterSelectionUpdate(player);
    }

    @Override
    public void modify(final Player player, final boolean shift, final int amount) {
        this.worldEdit(player);
        super.modify(player, shift, amount);
        afterSelectionUpdate(player);
    }

    @Override
    public void placeLoc1(final Player player, final Location loc) {
        this.worldEdit(player);
        super.placeLoc1(player, loc);
        this.afterSelectionUpdate(player);
    }

    @Override
    public void placeLoc2(final Player player, final Location loc) {
        this.worldEdit(player);
        super.placeLoc2(player, loc);
        this.afterSelectionUpdate(player);
    }

    @Override
    public void selectChunk(final Player player) {
        this.worldEdit(player);
        super.selectChunk(player);
        afterSelectionUpdate(player);
    }

    @Override
    public void showSelectionInfo(final Player player) {
        this.worldEdit(player);
        super.showSelectionInfo(player);
    }

    @Override
    public void sky(final Player player, final boolean resadmin) {
        this.worldEdit(player);
        super.sky(player, resadmin);
        afterSelectionUpdate(player);
    }

    @Override
    public boolean worldEdit(final Player player) {
        final WorldEditPlugin wep = (WorldEditPlugin) server.getPluginManager().getPlugin("WorldEdit");
        final Selection sel = wep.getSelection(player);
        if (sel != null) {
            Location pos1 = sel.getMinimumPoint();
            Location pos2 = sel.getMaximumPoint();
            try {
                final CuboidRegion region = (CuboidRegion) sel.getRegionSelector().getRegion();
                pos1 = new Location(player.getWorld(), region.getPos1().getX(), region.getPos1().getY(), region.getPos1().getZ());
                pos2 = new Location(player.getWorld(), region.getPos2().getX(), region.getPos2().getY(), region.getPos2().getZ());
            } catch (final Exception e) {
            }
            this.playerLoc1.put(player.getName(), pos1);
            this.playerLoc2.put(player.getName(), pos2);
            return true;
        }
        return false;
    }

    private void afterSelectionUpdate(final Player player) {
        if (hasPlacedBoth(player.getName())) {
            final WorldEditPlugin wep = (WorldEditPlugin) server.getPluginManager().getPlugin("WorldEdit");
            final World world = playerLoc1.get(player.getName()).getWorld();
            final Selection selection = new CuboidSelection(world, playerLoc1.get(player.getName()), playerLoc2.get(player.getName()));
            wep.setSelection(player, selection);
        }
    }
}
