package app.views.match.components.utils; import app.data.entity.Game; import app.data.entity.Player; import app.gameimage.GameImageUtils; import app.utils.ChessComUtils; import app.utils.EntityComponentUtils; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.ButtonVariant; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.Label; import com.vaadin.flow.component.icon.Icon; import com.vaadin.flow.component.icon.VaadinIcon; import com.vaadin.flow.component.orderedlayout.FlexComponent; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import java.util.ArrayList; import java.util.List; public class GameComponentUtils { private GameComponentUtils() { } public static int getGameNumber(Game game) { List games = new ArrayList<>(game.getMatch().getGames()); int index = games.indexOf(game); if (index < 0) throw new IllegalStateException("The match associated with the Game does not contain the Game!"); return games.indexOf(game) + 1; } public static String getImagePath(Game game) { String[] urlParts = game.getGameInfo().getChessComId().split("/"); return GameImageUtils.IMAGE_PATH_CLIENT + urlParts[urlParts.length - 1] + GameImageUtils.EXTENSION; } public static Player getWhitePlayer(Game game) { return game.getPlayer1IsWhite() ? game.getMatch().getPlayer1() : game.getMatch().getPlayer2(); } public static Player getBlackPlayer(Game game) { return game.getPlayer1IsWhite() ? game.getMatch().getPlayer2() : game.getMatch().getPlayer1(); } public static Div getPlayerDiv(Game game, boolean white) { Player player = white ? getWhitePlayer(game) : getBlackPlayer(game); Div div = new Div(); div.addClassName("player"); div.addClassName(white ? "white-player" : "black-player"); HorizontalLayout playerLabel = EntityComponentUtils.getPlayerLabel(player); playerLabel.setWidthFull(); playerLabel.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); Label resultLabel = getResultLabel(game, white); playerLabel.add(resultLabel); div.add(playerLabel); return div; } private static Label getResultLabel(Game game, boolean white) { String resultString; switch (game.getResult()) { case -1: resultString = white ? "0" : "1"; break; case 0: resultString = "0.5"; break; case 1: resultString = white ? "1" : "0"; break; default: throw new IllegalStateException(String.format("Game result must be either -1, 0, or 1, but it is %d!", game.getResult())); } Label resultLabel = new Label(resultString); resultLabel.addClassName("bold-label"); return resultLabel; } public static Div getTimeControlDiv(Game game) { String timeControl = game.getGameInfo().getTimeControl(); Icon icon; String string; switch (timeControl) { case "600": icon = new Icon(VaadinIcon.STOPWATCH); string = "10 min"; break; case "300": icon = new Icon(VaadinIcon.BOLT); string = "5 min"; break; case "180": icon = new Icon(VaadinIcon.BOMB); string = "3 min"; break; default: icon = new Icon(VaadinIcon.QUESTION); string = String.format("Unknown Time Control: %s", timeControl); break; } Div div = new Div(); div.addClassName("time-control"); HorizontalLayout horizontalLayout = new HorizontalLayout(); horizontalLayout.setWidthFull(); horizontalLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); horizontalLayout.add(icon, new Label(string)); div.add(horizontalLayout); return div; } public static Label getHeader(Game game) { Label label = new Label(); label.setText(String.format("Game %d", getGameNumber(game))); label.addClassName("game-header"); return label; } public static Button getChessComButton(Game game) { String string = "Watch on Chess.com"; Icon icon = new Icon(VaadinIcon.EYE); Button button = new Button(string, icon); button.setWidthFull(); button.addThemeVariants(ButtonVariant.LUMO_PRIMARY); button.addClickListener(event -> button.getUI().ifPresent(ui -> ui.getPage().open(ChessComUtils.getGameURL(game), "_blank"))); return button; } public static Label getVsLabel() { Label label = new Label("vs."); label.addClassName("vs-label"); return label; } }