You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

176 lines
5.8 KiB

package app.views.match.components;
import app.data.bean.CalculatedMatch;
import app.data.entity.Game;
import app.data.entity.Match;
import app.data.service.ChessComService;
import app.data.service.MatchService;
import app.gameimage.GameImageService;
import app.navigation.match.MatchNavigation;
import app.utils.ComponentUtils;
import app.utils.StringUtils;
import app.views.navigation.interfaces.ContentConfigurable;
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.FlexLayout;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.NoSuchElementException;
public class MatchComponent extends Div implements ContentConfigurable {
private final MatchNavigation matchNavigation;
private final MatchService matchService;
private final GameImageService gameImageService;
private final VerticalLayout headerLayout = new VerticalLayout();
private final HorizontalLayout headerPlayersLayout = new HorizontalLayout();
private final Label headerResultLabel = new Label();
private final FlexLayout gamesLayout = new FlexLayout();
private final VerticalLayout noGamesLayout = new VerticalLayout();
private final FlexLayout editLayout = new FlexLayout();
private final EditMatchCard editMatchCard;
private Match match;
private CalculatedMatch calculatedMatch;
public MatchComponent(MatchNavigation matchNavigation,
@Autowired ChessComService chessComService,
@Autowired GameImageService gameImageService) {
this.matchNavigation = matchNavigation;
this.matchService = matchNavigation.getMatchService();
this.gameImageService = gameImageService;
this.editMatchCard = new EditMatchCard(matchNavigation, chessComService, gameImageService);
defineLayout();
}
////////////
// LAYOUT //
////////////
private void defineLayout() {
add(headerLayout);
defineHeaderLayout();
defineGamesLayout();
defineNoGamesLayout();
defineEditLayout();
}
private void defineHeaderLayout() {
headerLayout.add(headerPlayersLayout, headerResultLabel);
headerPlayersLayout.addClassName("match-header-players");
headerResultLabel.addClassName("match-header-result");
headerResultLabel.addClassName("bold-header");
}
private void defineGamesLayout() {
gamesLayout.setFlexWrap(FlexLayout.FlexWrap.WRAP);
gamesLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
}
private void defineNoGamesLayout() {
noGamesLayout.add(createNoGamesLabel(), createAddGamesButton());
noGamesLayout.setAlignItems(FlexComponent.Alignment.CENTER);
}
private Label createNoGamesLabel() {
return new Label("No games for this match in the database.");
}
private Button createAddGamesButton() {
Button button = new Button("Add games", new Icon(VaadinIcon.PLUS));
button.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
button.addClickListener(event -> {
matchNavigation.setEditFlag(true);
remove(noGamesLayout);
});
return button;
}
private void defineEditLayout() {
editLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
editLayout.add(editMatchCard);
}
/////////////
// CONTENT //
/////////////
@Override
public void configureContent() {
try {
match = matchNavigation.getSelectedMatch().orElseThrow();
calculatedMatch = matchService.getCalculatedMatch(match);
configureHeaderContent();
configureMainContent();
} catch (NoSuchElementException e) {
gamesLayout.removeAll();
add(matchNavigation.getValidationLabel());
}
}
private void configureHeaderContent() {
configureHeaderPlayersLayout();
configureHeaderResultLabel();
}
private void configureHeaderPlayersLayout() {
HorizontalLayout player1 = ComponentUtils.getPlayerLabel(calculatedMatch.getPlayer1());
HorizontalLayout player2 = ComponentUtils.getPlayerLabel(calculatedMatch.getPlayer2());
Label vs = new Label("vs.");
headerPlayersLayout.removeAll();
headerPlayersLayout.add(player1, vs, player2);
}
private void configureHeaderResultLabel() {
if (match.getGames().isEmpty() || matchNavigation.editFlag()) {
headerLayout.remove(headerResultLabel);
} else {
headerLayout.add(headerResultLabel);
headerResultLabel.setText(String.format("%s",
StringUtils.getResultString("-", calculatedMatch.getScore1(), calculatedMatch.getScore2())));
}
}
private void configureMainContent() {
if (matchNavigation.editFlag()) {
remove(gamesLayout);
add(editLayout);
editMatchCard.configureContent(match);
return;
}
remove(editLayout);
configureGamesContent();
}
private void configureGamesContent() {
if (match.getGames().isEmpty()) {
remove(gamesLayout);
add(noGamesLayout);
return;
}
remove(noGamesLayout);
add(gamesLayout);
gamesLayout.removeAll();
for (Game game : match.getGames()) {
gamesLayout.add(new GameCard(game, gameImageService));
}
}
}