package org.academiadecodigo.bootcamp.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.model.Bootcamp;
import org.academiadecodigo.bootcamp.service.bootcamp.BootcampService;
import org.academiadecodigo.bootcamp.service.ServiceRegistry;
import org.academiadecodigo.bootcamp.utils.Navigation;

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


public class BootcampListController implements Controller {

    @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;

    @FXML
    void initialize() {
        bootcampService = ServiceRegistry.getInstance().getService("BootcampService");

        bootcamps.setAll(bootcampService.listBootcamps());

        bootcampList.setItems(bootcamps);

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

    }

    @FXML
    public void logout(ActionEvent event) {
        Navigation.getInstance().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.getInstance().loadScreen("bootcamp-cadet-list");
        }

    }

}
