package visualiser.Controllers;


import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import visualiser.model.VisualiserBoat;

import java.net.URL;
import java.util.ResourceBundle;


/**
 * Finish Screen for when the race finishes.
 */
public class FinishController extends Controller {

    @FXML
    AnchorPane finishWrapper;

    @FXML
    TableView<VisualiserBoat> boatInfoTable;

    @FXML
    TableColumn<VisualiserBoat, String> boatRankColumn;

    @FXML
    TableColumn<VisualiserBoat, String> boatNameColumn;

    @FXML
    Label raceWinnerLabel;


    /**
     * The boats to display on the table.
     */
    private ObservableList<VisualiserBoat> boats;


    /**
     * Ctor.
     */
    public FinishController() {
    }


    @Override
    public void initialize(URL location, ResourceBundle resources){
    }



    /**
     * Sets up the finish table
     * @param boats Boats to display
     */
    private void setFinishTable(ObservableList<VisualiserBoat> boats) {
        this.boats = boats;

        //Set contents.
        boatInfoTable.setItems(boats);

        //Name.
        boatNameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());

        //Rank/position.
        boatRankColumn.setCellValueFactory(cellData -> cellData.getValue().positionProperty());


        //Winner label.
        raceWinnerLabel.setText("Winner: "+ boatNameColumn.getCellObservableValue(0).getValue());
        raceWinnerLabel.setWrapText(true);

    }



    /**
     * Display the table
     * @param boats boats to display on the table.
     */
    public void enterFinish(ObservableList<VisualiserBoat> boats){
        finishWrapper.setVisible(true);
        setFinishTable(boats);
    }

}
