package org.academiadecodigo.bootcamp;

import javafx.application.Application;
import javafx.stage.Stage;
import org.academiadecodigo.bootcamp.model.Bootcamp;
import org.academiadecodigo.bootcamp.model.CodeCadet;
import org.academiadecodigo.bootcamp.model.User;
import org.academiadecodigo.bootcamp.persistence.ConnectionManager;
import org.academiadecodigo.bootcamp.service.*;
import org.academiadecodigo.bootcamp.service.bootcamp.BootcampService;
import org.academiadecodigo.bootcamp.service.bootcamp.MockBootcampService;
import org.academiadecodigo.bootcamp.service.user.JdbcUserService;
import org.academiadecodigo.bootcamp.service.user.UserService;
import org.academiadecodigo.bootcamp.utils.Navigation;
import org.academiadecodigo.bootcamp.utils.Security;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() {

        // Add a test user
        UserService userService = new JdbcUserService();
        userService.addUser(new User("Rui Ferrao", "rui.ferrao@academiadecodigo", Security.getHashed("codigoergosum")));
        userService.addUser(new User("Joana", "rui.ferrao@academiadecodigo", Security.getHashed("a")));
        userService.addUser(new User("Ricardo", "rui.ferrao@academiadecodigo", Security.getHashed("a")));

        User ferrao = userService.findByName("Rui Ferrao");
        System.out.println(ferrao);
        User ricardo = userService.findByName("Ricardo");
        System.out.println(ricardo);

        System.out.println(userService.count() + " users in this service.");

        ServiceRegistry.getInstance().addService(userService);

        BootcampService bootcampService = new MockBootcampService();

        bootcampService.createBootcamp(1, Bootcamp.BootcampLocation.LISBOA, null, null);
        bootcampService.createBootcamp(2, Bootcamp.BootcampLocation.FUNDÃO, null, null);
        bootcampService.createBootcamp(11, Bootcamp.BootcampLocation.FUNDÃO, "2017/09/04", "2017/12/07");

        CodeCadet ferraoCadet = new CodeCadet(userService.findByName("Rui Ferrao"));
        CodeCadet joanaCadet = new CodeCadet(userService.findByName("Joana"));
        CodeCadet ricardoCadet = new CodeCadet(userService.findByName("Ricardo"));


        bootcampService.addCodeCadets(1, ferraoCadet, joanaCadet);
        bootcampService.addCodeCadet(2, ricardoCadet);

        ServiceRegistry.getInstance().addService(bootcampService);

    }

    @Override
    public void start(Stage primaryStage) {

        // set the primary stage
        Navigation.getInstance().setStage(primaryStage);
        primaryStage.setTitle("Academia Código");

        Navigation.getInstance().loadScreen("login");
    }

    @Override
    public void stop() throws Exception {
        ConnectionManager.getInstance().close();
    }
}

