package org.academiadecodigo.bootcamp.fxlogin.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import org.academiadecodigo.bootcamp.fxlogin.model.Bootcamp;
import org.academiadecodigo.bootcamp.fxlogin.service.bootcamp.BootcampService;
import org.academiadecodigo.bootcamp.fxlogin.utils.Navigation;

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


public class BootcampListController implements Controller {

    private final Navigation navigation;
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableView<Bootcamp> bootcampList;

    @FXML
    private TableColumn<Bootcamp, Integer> bootcampId;

    @FXML
    private TableColumn<Bootcamp, String> bootcampLocation;

    @FXML
    private TableColumn<Bootcamp, String> bootcampStart;

    @FXML
    private TableColumn<Bootcamp, String> bootcampEnd;

    private ObservableList<Bootcamp> bootcamps = FXCollections.observableArrayList();
    private BootcampService bootcampService;

    public BootcampListController(Navigation navigation) {
        this.navigation = navigation;
    }

    @FXML
    void initialize() {
        bootcamps.setAll(bootcampService.listBootcamps());

        bootcampList.setItems(bootcamps);

        bootcampId.setCellValueFactory(new PropertyValueFactory<>("Id"));
        bootcampLocation.setCellValueFactory(new PropertyValueFactory<>("Location"));
        bootcampStart.setCellValueFactory(new PropertyValueFactory<>("StartString"));
        bootcampEnd.setCellValueFactory(new PropertyValueFactory<>("EndString"));

    }

    @FXML
    public void logout(ActionEvent event) {
        navigation.back();
    }

    @FXML
    void enterBootcamp(MouseEvent event) {
        Bootcamp selectedBootcamp = (Bootcamp) ((TableView) event.getSource()).getSelectionModel().getSelectedItem();
        if (selectedBootcamp != null) {
            bootcampService.setSelectedBootcamp(selectedBootcamp.getId());
            System.out.println(selectedBootcamp);
            navigation.loadScreen("bootcamp-cadet-list");
        }

    }

    public void setBootcampService(BootcampService bootcampService) {
        this.bootcampService = bootcampService;
    }
}
