import java.util.List;

public class OnlinePlayer_1_1_0 extends OnlinePlayer {
	public OnlinePlayer_1_1_0(Integer size, AsyncSocket socket) {
		super(size, socket);
	}



	@Override
	public void receive(String message) {
		SocketPackage p = new SocketPackage(message);

		List<String> data = p.splitData();
		switch (p.getName()) {
			case "IAM":
				if (data.size() < 2) break;
				if (this.board != null) break;

				int semester = Integer.parseInt(data.get(0));
				String username = p.getData().substring(data.get(0).length() + 1);

				int usedBoardSize = Math.min(GameController.semesterToBoardSize(semester), this.wantedBoardSize);
				
				this.setName(username);
				this.createBoard(usedBoardSize);
				this.enemy.createBoard(usedBoardSize);

				break;

			// TODO: IAMU

			case "COIN":
				if(!this.hasReceivedCoinPackage && (p.getData().equals("1") || p.getData().equals("0"))){
                    this.myCoin = p.getData().equals("1");
					enemy.receiveCoin(this.myCoin);
					this.hasReceivedCoinPackage = true;
				}
				break;


			case "SHOOT":
				if (Point.isValidSyntax(p.getData())){
					Point point = new Point(p.getData());
					this.enemy.receiveShoot(point);
				}
				break;

			case "HIT":
				if (data.size()==2){
					Point point = new Point(data.get(0));
					int typeIndex = Integer.parseInt(data.get(1));
					if (Point.isValidSyntax(data.get(0)) && typeIndex >= 0 && typeIndex < HitResponseType.values().length){
						this.enemy.receiveHit(new HitResponse(typeIndex, point));
					}
				}
				break;

			case "CHAT":
				//TODO CHAT
				break;

			default:
				//nichts passier da Paket ungültig
				break;
		}
	}

	@Override
	public synchronized void receiveShoot(Point point){
		super.socket.send(new SocketPackage("SHOOT",point.toString()));
	}

	@Override
	public synchronized void receiveHit(HitResponse hitResponse) {
		super.socket.send(new SocketPackage("HIT", hitResponse.toString()));
	}

	@Override
	public synchronized void receiveCoin(boolean coin) {
		if (!this.haseReceivedCoin) {
			super.socket.send(new SocketPackage("COIN", String.valueOf(coin ? 1 : 0)));
			this.haseReceivedCoin = true;
		}
	}

	@Override
	public synchronized void shoot(Point point) {
		super.socket.send(new SocketPackage("SHOOT", point.toString()));
	}
}