package com.kieferhagin.gravestones;

import java.util.ArrayList;

import net.canarymod.Canary;
import net.canarymod.api.chat.ChatComponent;
import net.canarymod.api.entity.living.humanoid.Player;
import net.canarymod.api.factory.ChatComponentFactory;
import net.canarymod.api.inventory.Item;
import net.canarymod.api.inventory.PlayerInventory;
import net.canarymod.api.world.World;
import net.canarymod.api.world.blocks.BlockType;
import net.canarymod.api.world.blocks.Chest;
import net.canarymod.api.world.blocks.Sign;
import net.canarymod.api.world.position.Location;

public class Gravestone {
	private Location location;
	private Player player;
	
	private Chest chest;
	
	/**
	 * Create a new gravestone container
	 * @param player The player that the gravestone will contain
	 * @param location The location of the gravestone (the stone stair)
	 */
	public Gravestone(Player player, Location location){
		this.location = location;
		this.player = player;
	}
	
	/**
	 * Generate the gravestone in the world and bury the player's items
	 */
	public void buryPlayer(ChatComponent causeOfDeath){
		this.chest = this.generate(causeOfDeath);
		this.addPlayerItemsToChest();
	}
	
	private Chest generate(ChatComponent causeOfDeath){
		World world = this.location.getWorld();
		
		// Place the blocks in such a way where the double chest is parallel with the stone stair
		Location stoneStairLocation = new Location(this.location);
		Location[] chestLocations = {
				new Location(stoneStairLocation.getX(),
						stoneStairLocation.getY()-1, stoneStairLocation.getZ()),
				new Location(stoneStairLocation.getX(),
						stoneStairLocation.getY()-1, stoneStairLocation.getZ() - 1)
		};
		
		Location signLocation = new Location(stoneStairLocation.getX(),
				stoneStairLocation.getY(), stoneStairLocation.getZ() - 2);
		
		this.addSign(signLocation, causeOfDeath);
		
		world.setBlockAt(stoneStairLocation, BlockType.StoneBrickStair);
		
		for (Location chestLoc : chestLocations){
			world.setBlockAt(chestLoc, BlockType.Chest);
		}
		
		return (Chest)(world.getBlockAt(chestLocations[1]).getTileEntity());
	}
	
	private void addSign(Location signLocation, ChatComponent causeOfDeath){
		World world = signLocation.getWorld();
		world.setBlockAt(signLocation, BlockType.SignPost);
			 
		Sign sign = (Sign)(world.getBlockAt(signLocation).getTileEntity());
		
		ChatComponentFactory chatFactory = Canary.factory().getChatComponentFactory();
		
		ChatComponent hereLiesComponent = chatFactory.compileChatComponent("~ Here lies ~");
		ChatComponent nameComponent = chatFactory.compileChatComponent(this.player.getName());
		
		String fullDeathText = causeOfDeath.getFullText().replace(this.player.getName() + " ", "");
		ArrayList<ChatComponent> deathTextLines = new ArrayList<ChatComponent>();
		
		if (fullDeathText.length() > 10){
			deathTextLines.add(chatFactory.compileChatComponent(fullDeathText.substring(0, 9)));
			deathTextLines.add(chatFactory.compileChatComponent(fullDeathText.substring(9)));
		} else {
			deathTextLines.add(chatFactory.compileChatComponent(fullDeathText));
		}
		
		sign.setComponentOnLine(hereLiesComponent, 0);
		sign.setComponentOnLine(nameComponent, 1);
		sign.setComponentOnLine(deathTextLines.get(0), 2);
		
		if (deathTextLines.size() > 1){
			sign.setComponentOnLine(deathTextLines.get(1), 3);
		}
	}
	
	private void addPlayerItemsToChest(){
		PlayerInventory inventory = this.player.getInventory();
		
		// Get the players equipment details, and clear it out to keep it from dropping
		Item boots = inventory.getBootsSlot();
		Item chestplate = inventory.getChestplateSlot();
		Item legs = inventory.getLeggingsSlot();
		Item helmet = inventory.getHelmetSlot();
		
		inventory.setBootsSlot(null);
		inventory.setChestPlateSlot(null);
		inventory.setLeggingsSlot(null);
		inventory.setHelmetSlot(null);
		
		// Add all the equipped and inventory items to the chest
		Item[] equippedItems = {boots, chestplate, legs, helmet};
		Item[] inventoryItems = inventory.clearInventory();
		
		for (Item item : equippedItems){
			if (item != null){
				chest.addItem(item);
			}
		}
		
		for (Item item : inventoryItems){
			if (item != null){
				chest.addItem(item);
			}
		}
	}
}

@@ -47,22 +47,80 @@
 ava.
-util.ArrayList
+text.SimpleDateFormat;%0Aimport java.util.ArrayList;%0Aimport java.util.Date
 ;%0A%0Ai
@@ -2509,24 +2509,20 @@
 mponent 
-hereLies
+name
 Componen
@@ -2562,23 +2562,29 @@
 ent(
-%22~ Here lies ~%22
+this.player.getName()
 );%0A%09
@@ -2590,36 +2590,43 @@
 %09%09ChatComponent 
-name
+timeOfDeath
 Component = chat
@@ -2655,38 +2655,40 @@
 ponent(this.
-player.getName
+getDateNowString
 ());%0A%09%09%0A%09%09St
@@ -3185,16 +3185,12 @@
 ine(
-hereLies
+name
 Comp
@@ -3218,36 +3218,43 @@
 ComponentOnLine(
-name
+timeOfDeath
 Component, 1);%0A%09
@@ -3396,16 +3396,16 @@
 1), 3);%0A
-
 %09%09%7D%0A%09%7D%0A%09
@@ -3401,24 +3401,210 @@
 );%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09private String getDateNowString()%7B%0A%09%09Date nowDate = new Date();%0A%09%09SimpleDateFormat dateFormat = new SimpleDateFormat(%22MM/dd/yyyy hh:mm a%22);%0A%09%09%0A%09%09return dateFormat.format(nowDate);%0A%09%7D%0A%09%0A
 %09private voi

add time of death to sign