package fr.aumgn.diamondrush.stage;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.potion.PotionEffect;

import fr.aumgn.diamondrush.DiamondRush;
import fr.aumgn.diamondrush.game.Team;
import fr.aumgn.diamondrush.stage.listeners.StaticListener;

public class StaticStage extends Stage {

    public static class PlayerStatus {

        private Location location;
        private Material material;
        private byte data;
        private Collection<PotionEffect> potionEffects;
        private float fallDistance;
        private int remainingAir;
        private int fireTicks;

        public PlayerStatus(Player player) {
            Location loc = player.getLocation();
            location = new Location(loc.getWorld(), loc.getBlockX() + 0.5, 
                    loc.getBlockY(), loc.getBlockZ() + 0.5,
                    loc.getYaw(), loc.getPitch());
            Block block = location.getBlock().getRelative(BlockFace.DOWN);
            material = block.getType();
            data = block.getData();
            block.setType(Material.GLASS);
            restorePosition(player);

            this.potionEffects = player.getActivePotionEffects();
            for (PotionEffect activeEffect : potionEffects) {
                player.removePotionEffect(activeEffect.getType());
            }

            this.fallDistance = player.getFallDistance();
            this.remainingAir = player.getRemainingAir();
            this.fireTicks = player.getFireTicks();
        }

        public void restorePosition(Player player) {
            player.teleport(location);
        }

        public void restore(Player player) {
            Block block = location.getBlock().getRelative(BlockFace.DOWN);
            block.setTypeIdAndData(material.getId(), data, true);

            for (PotionEffect potionEffect : potionEffects) {
                player.addPotionEffect(potionEffect, true);
            }

            player.setFallDistance(fallDistance);
            player.setRemainingAir(remainingAir);
            player.setFireTicks(fireTicks);
        }
    }

    private StaticListener listener;
    private long time;
    private Map<Player, PlayerStatus> status;

    public StaticStage(DiamondRush dr) {
        super(dr);
        this.listener = new StaticListener(this, dr.getGame());
        this.status = new HashMap<Player, PlayerStatus>();
    }

    @Override
    public List<Listener> getListeners() {
        return Collections.<Listener>singletonList(listener);
    }

    @Override
    public void start() {
        this.time = dr.getGame().getWorld().getTime();
        for (Team team : dr.getGame().getTeams()) {
            for (Player player : team.getPlayers()) {
                status.put(player, new PlayerStatus(player));
            }
        }
    }

    @Override
    public void stop() {
        dr.getGame().getWorld().setTime(time);
        for (Map.Entry<Player, PlayerStatus> playerStatus : status.entrySet()) {
            Player player = playerStatus.getKey();
            playerStatus.getValue().restore(player);
        }
    }

    public PlayerStatus getPlayerStatus(Player player) {
        return status.get(player);
    }
}

@@ -346,24 +346,147 @@
 t.Listener;%0A
+import org.bukkit.inventory.Inventory;%0Aimport org.bukkit.inventory.InventoryHolder;%0Aimport org.bukkit.inventory.ItemStack;%0A
 import org.b
@@ -512,16 +512,16 @@
 Effect;%0A
-
 %0Aimport 
@@ -833,16 +833,106 @@
 e data;%0A
+        private boolean isInventoryHolder;%0A        private ItemStack%5B%5D inventoryContents;%0A
         
@@ -1509,24 +1509,844 @@
 .getData();%0A
+            isInventoryHolder = block.getState() instanceof InventoryHolder;%0A            if (isInventoryHolder) %7B%0A                Inventory inventory = ((InventoryHolder) block.getState()).getInventory();%0A                inventoryContents = inventory.getContents();%0A            %7D%0A%0A            this.potionEffects = player.getActivePotionEffects();%0A            this.fallDistance = player.getFallDistance();%0A            this.remainingAir = player.getRemainingAir();%0A            this.fireTicks = player.getFireTicks();%0A        %7D%0A%0A        public void init(Player player) %7B%0A            Block block = location.getBlock().getRelative(BlockFace.DOWN);%0A            if (isInventoryHolder) %7B%0A                Inventory inventory = ((InventoryHolder) block.getState()).getInventory();%0A                inventory.clear();%0A            %7D%0A
             
@@ -2418,74 +2418,8 @@
 );%0A%0A
-            this.potionEffects = player.getActivePotionEffects();%0A
     
@@ -2560,177 +2560,8 @@
    %7D
-%0A%0A            this.fallDistance = player.getFallDistance();%0A            this.remainingAir = player.getRemainingAir();%0A            this.fireTicks = player.getFireTicks();
 %0A   
@@ -2856,16 +2856,216 @@
 , true);
+%0A            if (isInventoryHolder) %7B%0A                Inventory inventory = ((InventoryHolder) block.getState()).getInventory();%0A                inventory.setContents(inventoryContents);%0A            %7D
 %0A%0A      
@@ -3976,24 +3976,24 @@
 layers()) %7B%0A
-
             
@@ -4058,32 +4058,211 @@
     %7D%0A        %7D%0A
+        for (Team team : dr.getGame().getTeams()) %7B%0A            for (Player player : team.getPlayers()) %7B%0A                status.get(player).init(player);%0A            %7D%0A        %7D%0A
     %7D%0A%0A    @Over

Fix issues with static stage glass blocks