|
|
@ -7,23 +7,27 @@ import app.data.service.MatchService; |
|
|
|
import app.gameimage.GameImageService; |
|
|
|
import app.navigation.Navigation; |
|
|
|
import app.utils.ChessComUtils; |
|
|
|
import app.utils.MatchUtils; |
|
|
|
import app.utils.TimeControl; |
|
|
|
import com.vaadin.flow.component.ClickEvent; |
|
|
|
import com.vaadin.flow.component.ComponentEventListener; |
|
|
|
import com.vaadin.flow.component.UI; |
|
|
|
import com.vaadin.flow.component.button.Button; |
|
|
|
import com.vaadin.flow.component.button.ButtonVariant; |
|
|
|
import com.vaadin.flow.component.formlayout.FormLayout; |
|
|
|
import com.vaadin.flow.component.formlayout.FormLayout.ResponsiveStep; |
|
|
|
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 com.vaadin.flow.component.orderedlayout.VerticalLayout; |
|
|
|
import com.vaadin.flow.component.textfield.TextField; |
|
|
|
import com.vaadin.flow.shared.Registration; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
import static app.utils.TimeControl.*; |
|
|
|
import static com.vaadin.flow.component.button.ButtonVariant.LUMO_PRIMARY; |
|
|
|
import static com.vaadin.flow.component.formlayout.FormLayout.ResponsiveStep.LabelsPosition.ASIDE; |
|
|
|
|
|
|
|
public class EditMatchCard extends VerticalLayout { |
|
|
|
|
|
|
@ -32,7 +36,10 @@ public class EditMatchCard extends VerticalLayout { |
|
|
|
private final ChessComService chessComService; |
|
|
|
private final GameImageService gameImageService; |
|
|
|
|
|
|
|
private final ArrayList<TextField> textFields = new ArrayList<>(); |
|
|
|
private final VerticalLayout formLayout = new VerticalLayout(); |
|
|
|
private final List<TextField> tenMinuteTextFields = new ArrayList<>(); |
|
|
|
private final List<TextField> fiveMinuteTextFields = new ArrayList<>(); |
|
|
|
private final List<TextField> threeMinuteTextFields = new ArrayList<>(); |
|
|
|
// TODO: autocorrect "/live/game" to "/game/live/" |
|
|
|
private final Button submitButton = new Button("Submit", new Icon(VaadinIcon.CHECK)); |
|
|
|
// TODO: disable when no 6 values in form, and make sure you don't freeze forever when there are wrong entries |
|
|
@ -40,7 +47,7 @@ public class EditMatchCard extends VerticalLayout { |
|
|
|
private Registration editSubmitButtonRegistration; |
|
|
|
private final Button editCancelButton = new Button("Cancel", new Icon(VaadinIcon.CLOSE)); |
|
|
|
private Registration editCancelButtonRegistration; |
|
|
|
private final Button chessComButton = new Button("Autofill with the latest 6 games between the players", new Icon(VaadinIcon.MAGIC)); |
|
|
|
private final Button chessComButton = new Button("Autofill with the latest games between the players", new Icon(VaadinIcon.MAGIC)); |
|
|
|
private Registration chessComButtonButtonRegistration; |
|
|
|
|
|
|
|
private Match match; |
|
|
@ -59,7 +66,7 @@ public class EditMatchCard extends VerticalLayout { |
|
|
|
//////////// |
|
|
|
|
|
|
|
private void defineLayout() { |
|
|
|
setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
|
setAlignItems(Alignment.CENTER); |
|
|
|
setWidth(""); |
|
|
|
addClassName("card"); |
|
|
|
add(chessComButton); |
|
|
@ -70,25 +77,48 @@ public class EditMatchCard extends VerticalLayout { |
|
|
|
} |
|
|
|
|
|
|
|
private void defineTextFields() { |
|
|
|
for (int i = 0; i < 6; i++) { |
|
|
|
add(formLayout); |
|
|
|
// formLayout.setSpacing(false); |
|
|
|
formLayout.setPadding(false); |
|
|
|
// List<ResponsiveStep> responsiveSteps = new ArrayList<>(); |
|
|
|
// for (int i = 0; i < 6; i++) { |
|
|
|
// responsiveSteps.add(new ResponsiveStep("40em", 1, ASIDE)); |
|
|
|
// } |
|
|
|
// formLayout.setResponsiveSteps(responsiveSteps); |
|
|
|
defineTextFields(tenMinuteTextFields, TEN_MINUTES, 1); |
|
|
|
defineTextFields(fiveMinuteTextFields, FIVE_MINUTES, 3); |
|
|
|
defineTextFields(threeMinuteTextFields, THREE_MINUTES, 5); |
|
|
|
} |
|
|
|
|
|
|
|
private void defineTextFields(List<TextField> list, TimeControl timeControl, int startGameNumber) { |
|
|
|
for (int i = 0; i < 2; i++) { |
|
|
|
TextField textField = new TextField(); |
|
|
|
textField.setWidth("23em"); |
|
|
|
HorizontalLayout horizontalLayout = new HorizontalLayout(new Label(String.format("Game %d:", i + 1)), textField); |
|
|
|
horizontalLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
|
horizontalLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
|
add(horizontalLayout); |
|
|
|
textFields.add(textField); |
|
|
|
textField.setPlaceholder("Please enter URL"); |
|
|
|
Label gameLabel = new Label(String.format("Game %d", startGameNumber + i)); |
|
|
|
gameLabel.addClassName("bold-label"); |
|
|
|
Label timeControlLabel = new Label(String.format("(%s):", timeControl.toPresentationString())); |
|
|
|
timeControlLabel.addClassName("nickname-label"); |
|
|
|
HorizontalLayout labelLayout = new HorizontalLayout(gameLabel, timeControlLabel); |
|
|
|
labelLayout.setSpacing(false); |
|
|
|
labelLayout.setAlignItems(Alignment.CENTER); |
|
|
|
labelLayout.setJustifyContentMode(JustifyContentMode.START); |
|
|
|
HorizontalLayout row = new HorizontalLayout(labelLayout, textField); |
|
|
|
row.setWidthFull(); |
|
|
|
row.setJustifyContentMode(JustifyContentMode.BETWEEN); |
|
|
|
formLayout.add(row); |
|
|
|
list.add(textField); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void defineSubmitButton() { |
|
|
|
submitButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY); |
|
|
|
submitButton.addThemeVariants(LUMO_PRIMARY); |
|
|
|
} |
|
|
|
|
|
|
|
private void addSubmitAndCancelButtons() { |
|
|
|
HorizontalLayout buttonLayout = new HorizontalLayout(); |
|
|
|
buttonLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
|
buttonLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
|
buttonLayout.setAlignItems(Alignment.CENTER); |
|
|
|
buttonLayout.setJustifyContentMode(JustifyContentMode.CENTER); |
|
|
|
buttonLayout.add(submitButton, editCancelButton); |
|
|
|
add(buttonLayout); |
|
|
|
} |
|
|
@ -99,10 +129,9 @@ public class EditMatchCard extends VerticalLayout { |
|
|
|
|
|
|
|
void configureContent(Match match) { |
|
|
|
this.match = match; |
|
|
|
List<Game> games = new ArrayList<>(match.getGames()); |
|
|
|
|
|
|
|
clearTextFields(); |
|
|
|
fillTextFieldsWithURLs(games); |
|
|
|
fillTextFieldsWithURLs(MatchUtils.getGamesMap(match)); |
|
|
|
|
|
|
|
configureChessComButton(); |
|
|
|
configureEditSubmitButton(); |
|
|
@ -110,12 +139,20 @@ public class EditMatchCard extends VerticalLayout { |
|
|
|
} |
|
|
|
|
|
|
|
private void clearTextFields() { |
|
|
|
textFields.forEach(textField -> textField.setValue("")); |
|
|
|
for (List<TextField> list : Set.of(tenMinuteTextFields, fiveMinuteTextFields, threeMinuteTextFields)) { |
|
|
|
list.forEach(textField -> textField.setValue("")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void fillTextFieldsWithURLs(List<Game> games) { |
|
|
|
for (int i = 0; i < Math.min(games.size(), 6); i++) |
|
|
|
textFields.get(i).setValue(ChessComUtils.getGameURL(games.get(i))); |
|
|
|
private void fillTextFieldsWithURLs(Map<TimeControl, List<Game>> games) { |
|
|
|
for (int i = 0; i < 2; i++) { |
|
|
|
if (games.get(TEN_MINUTES).size() > i) |
|
|
|
tenMinuteTextFields.get(i).setValue(ChessComUtils.getGameURL(games.get(TEN_MINUTES).get(i))); |
|
|
|
if (games.get(FIVE_MINUTES).size() > i) |
|
|
|
fiveMinuteTextFields.get(i).setValue(ChessComUtils.getGameURL(games.get(FIVE_MINUTES).get(i))); |
|
|
|
if (games.get(THREE_MINUTES).size() > i) |
|
|
|
threeMinuteTextFields.get(i).setValue(ChessComUtils.getGameURL(games.get(THREE_MINUTES).get(i))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void configureChessComButton() { |
|
|
@ -125,7 +162,7 @@ public class EditMatchCard extends VerticalLayout { |
|
|
|
|
|
|
|
private ComponentEventListener<ClickEvent<Button>> createChessComButtonClickListener() { |
|
|
|
return event -> { |
|
|
|
List<Game> gamesBetweenPlayers = chessComService.getLatestGamesBetweenPlayers(match, 6, 2); |
|
|
|
Map<TimeControl, List<Game>> gamesBetweenPlayers = chessComService.getLatestGamesBetweenPlayers(match, 2); |
|
|
|
fillTextFieldsWithURLs(gamesBetweenPlayers); |
|
|
|
}; |
|
|
|
} |
|
|
@ -147,7 +184,7 @@ public class EditMatchCard extends VerticalLayout { |
|
|
|
} |
|
|
|
|
|
|
|
private void addGamesToMatchFromFieldValues() { |
|
|
|
for (TextField textField : textFields) { |
|
|
|
for (TextField textField : tenMinuteTextFields) { |
|
|
|
Optional<Game> game = chessComService.getGame(textField.getValue(), match); // TODO: handle this when Optional is empty! |
|
|
|
game.ifPresent(value -> match.getGames().add(value)); |
|
|
|
} |
|
|
|