package com.whippy.sponge.whipconomy.orchestrator;

import java.util.UUID;

import org.spongepowered.api.Game;
import org.spongepowered.api.Server;
import org.spongepowered.api.entity.player.Player;
import org.spongepowered.api.text.Text.Literal;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.format.TextColors;

import com.google.common.base.Optional;
import com.whippy.sponge.whipconomy.beans.Payment;
import com.whippy.sponge.whipconomy.beans.StaticsHandler;
import com.whippy.sponge.whipconomy.cache.EconomyCache;
import com.whippy.sponge.whipconomy.cache.PendingNotificaitions;

public class PlayerNotifier {

	public static boolean notify(Payment payment, String playerName){
		Game game = StaticsHandler.getGame();
		Server server = game.getServer();
		Optional<Player> optionalPlayer =  server.getPlayer(UUID.fromString(EconomyCache.getId(playerName)));
		if(optionalPlayer.isPresent()){
			Player player = optionalPlayer.get();
			String receiver = payment.getPlayerNameReceiver();
			String sender = payment.getPlayerNamePayer();
			double amount = payment.getAmount();
			Literal message;
			if(receiver!=null && receiver.equals(playerName)){
				message = Texts.builder("Received " + amount + " from " + sender).color(TextColors.GREEN).build();
			}else if(receiver==null){
				message = Texts.builder("Charged " + amount + " by the server").color(TextColors.GREEN).build();				
			}else if(sender!=null && sender.equals(playerName)){
				message = Texts.builder("Paid " + amount + " to " + receiver).color(TextColors.RED).build();				
			}else{
				message = Texts.builder("Received " + amount + " from the server").color(TextColors.GREEN).build();								
			}
			player.sendMessage(message);
			return true;
		}
		else{
			return false;
		}

	}

	public static void notifyEvenIfOffline(Payment payment){
		if(payment.getPlayerNamePayer()!=null){			
			if(!notify(payment, EconomyCache.getId(payment.getPlayerNamePayer()))){
				PendingNotificaitions.addPayment(EconomyCache.getId(payment.getPlayerNamePayer()), payment);
			}
		}
		if(payment.getPlayerNameReceiver()!=null){			
			if(!notify(payment, EconomyCache.getId(payment.getPlayerNameReceiver()))){
				PendingNotificaitions.addPayment(EconomyCache.getId(payment.getPlayerNameReceiver()), payment);
			}
		}
	}

}

@@ -1909,35 +1909,16 @@
 ayment, 
-EconomyCache.getId(
 payment.
@@ -1935,25 +1935,24 @@
 amePayer()))
-)
 %7B%0A%09%09%09%09Pendin
@@ -2118,35 +2118,16 @@
 ayment, 
-EconomyCache.getId(
 payment.
@@ -2151,17 +2151,16 @@
 iver()))
-)
 %7B%0A%09%09%09%09Pe

Fix bug in offline notification