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.Cadet;
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 BootcampCadetListController implements Controller {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableView<Cadet> cadetList;

    @FXML
    private TableColumn<Cadet, String> name;

    @FXML
    private TableColumn<Cadet, String> gender;

    @FXML
    private TableColumn<Cadet, String> address;

    @FXML
    private TableColumn<Cadet, String> city;

    @FXML
    private TableColumn<Cadet, String> phone;

    @FXML
    private TableColumn<Cadet, String> birthdate;

    private BootcampService bootcampService;

    private Navigation navigation;

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

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

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

    @FXML
    void showCadetDetails(MouseEvent event) {
        System.out.println(cadetList.getSelectionModel().getSelectedItem());
    }

    @FXML
    void initialize() {
        ObservableList<Cadet> cadets = FXCollections.observableArrayList(bootcampService.listCodeCadets(bootcampService.getSelectedBootcamp()));

        cadetList.setItems(cadets);

        name.setCellValueFactory(new PropertyValueFactory<>("Name"));
        gender.setCellValueFactory(new PropertyValueFactory<>("Gender"));
        address.setCellValueFactory(new PropertyValueFactory<>("Address"));
        city.setCellValueFactory(new PropertyValueFactory<>("City"));
        phone.setCellValueFactory(new PropertyValueFactory<>("Phone"));
        birthdate.setCellValueFactory(new PropertyValueFactory<>("BirthdateString"));

    }

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

}
