package com.wcecil.webservice.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wcecil.beans.dto.GameState;
import com.wcecil.beans.dto.User;
import com.wcecil.beans.gameobjects.Card;
import com.wcecil.beans.gameobjects.CardTemplate;
import com.wcecil.beans.gameobjects.Player;
import com.wcecil.data.repositiories.AuditRepository;
import com.wcecil.data.repositiories.UsersRepository;

@Service
public class MaskingHelper {
	@Autowired
	UsersRepository usersRepo;
	private @Autowired AuditRepository auditRepo;
	
	public GameState maskGame(String gameId, String userId, GameState g) {
		GameState retVal = new GameState();

		if (g != null) {
			retVal.setId(gameId);
			retVal.setName(g.getName());
			// retVal.setAudit(g.getAudit());
			retVal.setCurrentPlayer(maskPlayerDetails(
					g.getCurrentPlayer(), userId));
			retVal.setPlayers(maskPlayersDetails(g, userId));
			retVal.setTicCount(g.getTicCount());
			retVal.setMainDeck(maskCards(g));
			retVal.setStaticCards(g.getStaticCards());
			retVal.setTriggers(g.getTriggers());
			retVal.setAvailable(g.getAvailable());
			retVal.setAuditCount(auditRepo.getCount(gameId, userId));
		}
		return retVal;
	}

	public List<Card> maskCards(GameState g) {
		List<Card> mainDeck = g.getMainDeck();
		return maskCards(mainDeck);
	}

	public List<Card> maskCards(List<Card> mainDeck) {
		List<Card> deck = new ArrayList<>();

		for (@SuppressWarnings("unused")
		Card ignored : mainDeck) {
			deck.add(new CardTemplate());
		}
		return deck;
	}

	public List<Player> maskPlayersDetails(GameState g, String userId) {
		List<Player> players = new ArrayList<>();

		for (Player player : g.getPlayers()) {
			players.add(maskPlayerDetails(player, userId));
		}

		return players;
	}

	public Player maskPlayerDetails(Player p, String userId) {
		Player p2 = new Player();

		if (p != null) {
			p2.setUserId(p.getUserId());
			p2.setUser(p.getUser());
			if (p2.getUser() == null && p2.getUserId() != null) {
				User user = usersRepo.getUser(p2.getUserId());
				if (user != null) {
					user.setGames(null);
					user.setPassword(null);
					if (user.getName() == null) {
						if (user.getLogin() == null
								|| user.getLogin().isEmpty()) {
							user.setLogin("[???]");
						}
						user.setName(user.getLogin());
					}
					p2.setUser(user);
				}
			}

			p2.setId(p.getId());

			p2.setMoney(p.getMoney());

			p2.setDeck(maskCards(p.getDeck()));
			p2.setDiscard(p.getDiscard());
			p2.setInplay(p.getInplay());
			p2.setPlayed(p.getPlayed());

			// TODO MASK HAND OF NOT YOU
			p2.setHand(p.getHand());
		}
		return p2;
	}
}

@@ -1448,24 +1448,21 @@
 t%3CCard%3E 
-mainDeck
+cards
 ) %7B%0A%09%09Li
@@ -1549,24 +1549,21 @@
 nored : 
-mainDeck
+cards
 ) %7B%0A%09%09%09d
@@ -2631,64 +2631,168 @@
 %0A%09%09%09
-// TODO MASK HAND OF NOT YOU%0A%09%09%09p2.setHand(p.getHand());
+if(p.getUserId() == null %7C%7C (userId!=null && userId.equals(p.getUserId())))%7B%0A%09%09%09%09p2.setHand(p.getHand());%0A%09%09%09%7Delse%7B%0A%09%09%09%09p2.setHand(maskCards(p.getHand()));%0A%09%09%09%7D
 %0A%09%09%7D

mask players hands better