package de.hessj.environmentex;

import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.event.EventPriority;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.event.Listener;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.type.Door;
import org.bukkit.block.data.type.Gate;

public class DoubleDoorListener implements Listener {

    private static final BlockFace[] DOORFACES = new BlockFace[] { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH,
            BlockFace.WEST };
            private static final BlockFace[] FENCEFACES = new BlockFace[] { BlockFace.UP, BlockFace.DOWN };

    private void doStuff(final Block door) {

        if (door.getBlockData() instanceof Door) {

            Bukkit.getScheduler().scheduleSyncDelayedTask(App.main, new Runnable() {
                public void run() {
                    Block doorBlock = door;
                    Block other = null;
                    Door d1 = (Door) doorBlock.getBlockData();
                    Door d2 = null;

                    for (BlockFace f : DOORFACES) {
                        if (doorBlock.getRelative(f).getBlockData() != null
                                && doorBlock.getRelative(f).getBlockData() instanceof Door) {
                            other = doorBlock.getRelative(f);
                            d2 = (Door) doorBlock.getRelative(f).getBlockData();
                            if (d2 != null) {
                                if (d1.isOpen()) {
                                    d2.setOpen(true);
                                } else {
                                    d2.setOpen(false);
                                }
                                other.setBlockData(d2);
                            }
                            return;
                        }
                    }

                }
            }, 0L);

        }//////
        if (door.getBlockData() instanceof Gate) {

            Bukkit.getScheduler().scheduleSyncDelayedTask(App.main, new Runnable() {
                public void run() {
                    Block doorBlock = door;
                    Block other = null;
                    Gate d1 = (Gate) doorBlock.getBlockData();
                    Gate d2 = null;




                    for (BlockFace f : FENCEFACES) {
                        if (doorBlock.getRelative(f).getBlockData() != null
                                && doorBlock.getRelative(f).getBlockData() instanceof Gate) {
                            other = doorBlock.getRelative(f);
                            d2 = (Gate) doorBlock.getRelative(f).getBlockData();
                            if (d2 != null) {
                                if (d1.isOpen()) {
                                    d2.setOpen(true);
                                } else {
                                    d2.setOpen(false);
                                }
                                other.setBlockData(d2);
                            }
                            return;
                        }
                    }

                }
            }, 0L);

        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerInteract(final PlayerInteractEvent e) {
        if (e.getAction() != Action.RIGHT_CLICK_BLOCK) {
            return;
        }
        doStuff(e.getClickedBlock());
    }

    @EventHandler
    public void onDoorRedstoneChange(BlockRedstoneEvent e) {
        if (e.getBlock().getBlockData() instanceof Door || e.getBlock().getBlockData() instanceof Gate) {
            doStuff(e.getBlock());
        }
    }
/*
    @EventHandler
    public void blockPace(BlockPlaceEvent e) {
        if(e.getBlock().getBlockData() instanceof Fence){
            for (BlockFace f : DOORFACES) {
                if (e.getBlock().getRelative(f).getBlockData() != null && e.getBlock().getRelative(f).getBlockData() instanceof Wall) {
                  
                    Fence placedFence = (Fence) e.getBlock().getState();
                    placedFence.
                }
            }
        }
    }
*/
}
