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

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableView<CodeCadet> cadetList;

    @FXML
    private TableColumn<CodeCadet, String> name;

    @FXML
    private TableColumn<CodeCadet, CodeCadet.Gender> gender;

    @FXML
    private TableColumn<CodeCadet, String> address;

    @FXML
    private TableColumn<CodeCadet, String> city;

    @FXML
    private TableColumn<CodeCadet, String> phone;

    @FXML
    private TableColumn<CodeCadet, String> birthdate;

    private ObservableList<CodeCadet> codeCadets;
    private BootcampService bootcampService;

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

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

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

    @FXML
    void initialize() {

        bootcampService = ServiceRegistry.getInstance().getService("BootcampService");

        codeCadets = FXCollections.observableArrayList(bootcampService.listCodeCadets(bootcampService.getSelectedBootcamp()));

        cadetList.setItems(codeCadets);

        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<>("Birthdate"));

    }
}
