package com.carrot.carrotshop.shop;

import java.math.BigDecimal;

import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.service.economy.account.UniqueAccount;
import org.spongepowered.api.service.economy.transaction.ResultType;
import org.spongepowered.api.service.economy.transaction.TransactionResult;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import com.carrot.carrotshop.CarrotShop;

import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;

@ConfigSerializable
public class Heal extends Shop {
	@Setting
	private int price;
	
	public Heal() {
	}

	public Heal(Player player, Location<World> sign) throws ExceptionInInitializerError {
		super(sign);
		if (!player.hasPermission("carrotshop.admin.heal"))
			throw new ExceptionInInitializerError("You don't have perms to build a heal sign");
		
		price = getPrice(sign);
		if (price < 0)
			throw new ExceptionInInitializerError("bad price");
		
		player.sendMessage(Text.of(TextColors.DARK_GREEN, "You have setup a heal sign"));
	}

	@Override
	public void info(Player player) {

		player.sendMessage(Text.of("Heal for ", formatPrice(price), "?"));
		update();
		
	}
	
	@Override
	public boolean trigger(Player player) {
		UniqueAccount buyerAccount = CarrotShop.getEcoService().getOrCreateAccount(player.getUniqueId()).get();
		TransactionResult result = buyerAccount.withdraw(CarrotShop.getEcoService().getDefaultCurrency(), BigDecimal.valueOf(price), Cause.source(this).build());
		if (result.getResult() != ResultType.SUCCESS) {
			player.sendMessage(Text.of(TextColors.DARK_RED, "You don't have enough money!"));
			return false;
		}

	    player.offer(Keys.HEALTH, player.get(Keys.MAX_HEALTH).get());
		
		player.sendMessage(Text.of("You healed for ", formatPrice(price)));
		
		return true;
	}

}

@@ -887,17 +887,16 @@
 price;%0D%0A
-%09
 %0D%0A%09publi
@@ -1160,22 +1160,66 @@
 ign%22);%0D%0A
-%09%09
+%0D%0A%09%09if (CarrotShop.getEcoService() != null) %7B
 %0D%0A%09%09
+%09
 price = 
@@ -1233,24 +1233,25 @@
 e(sign);%0D%0A%09%09
+%09
 if (price %3C 
@@ -1254,16 +1254,17 @@
 e %3C 0)%0D%0A
+%09
 %09%09%09throw
@@ -1309,24 +1309,27 @@
 price%22);%0D%0A%09%09
+%7D%0D%0A
 %0D%0A%09%09player.s
@@ -1546,17 +1546,14 @@
 );%0D%0A
-%09%09
 %0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09@
@@ -1604,16 +1604,62 @@
 yer) %7B%0D%0A
+%09%09if (CarrotShop.getEcoService() != null) %7B%0D%0A%09
 %09%09Unique
@@ -1757,16 +1757,17 @@
 get();%0D%0A
+%09
 %09%09Transa
@@ -1911,24 +1911,25 @@
 .build());%0D%0A
+%09
 %09%09if (result
@@ -1970,16 +1970,17 @@
 ) %7B%0D%0A%09%09%09
+%09
 player.s
@@ -2057,16 +2057,17 @@
 ));%0D%0A%09%09%09
+%09
 return f
@@ -2077,87 +2077,100 @@
 e;%0D%0A
+%09
 %09%09%7D%0D%0A
-%0D%0A%09    player.offer(Keys.HEALTH, player.get(Keys.MAX_HEALTH).get(
+%09%09%09player.sendMessage(Text.of(%22You healed for %22, formatPrice(price)
 ));%0D%0A%09%09
+%7D
 %0D%0A
+%09%09else%0D%0A%09
 %09%09pl
@@ -2209,41 +2209,81 @@
 aled
- for %22, formatPrice(price)
+%22));%0D%0A%0D%0A%09%09player.offer(Keys.HEALTH, player.get(Keys.MAX_HEALTH).get(
 ));%0D%0A
-%09%09
 %0D%0A%09%09

Fix problem with Heal sign when there is no economy on the server