package org.academiadecodigo.bootcamp.fxlogin;

import javafx.application.Application;
import javafx.stage.Stage;
import org.academiadecodigo.bootcamp.fxlogin.model.Bootcamp;
import org.academiadecodigo.bootcamp.fxlogin.model.Cadet;
import org.academiadecodigo.bootcamp.fxlogin.model.User;
import org.academiadecodigo.bootcamp.fxlogin.service.bootcamp.BootcampService;
import org.academiadecodigo.bootcamp.fxlogin.service.user.UserService;
import org.academiadecodigo.bootcamp.fxlogin.utils.Navigation;
import org.academiadecodigo.bootcamp.fxlogin.utils.Security;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main extends Application {

    private ApplicationContext context;

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

    @Override
    public void init() {

        context = new ClassPathXmlApplicationContext("spring/context-config.xml");

        // Add a test user
        UserService userService = context.getBean(UserService.class);
        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")));

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

        BootcampService bootcampService = context.getBean(BootcampService.class);
        bootcampService.addBootcamp(new Bootcamp(1, Bootcamp.Location.LISBOA, null, null));
        bootcampService.addBootcamp(new Bootcamp(2, Bootcamp.Location.FUNDAO, null, null));
        bootcampService.addBootcamp(new Bootcamp(11, Bootcamp.Location.FUNDAO,
                "2017/09/04", "2017/12/07"));

        Cadet ferraoCadet = new Cadet(userService.findByName("Rui Ferrao"));
        Cadet joanaCadet = new Cadet(userService.findByName("Joana"));
        joanaCadet.setGender(Cadet.Gender.FEMALE);
        Cadet ricardoCadet = new Cadet(userService.findByName("Ricardo"));


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

    }

    @Override
    public void start(Stage primaryStage) {

        Navigation navigation = context.getBean("navigation", Navigation.class);

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

        navigation.loadScreen("login");
    }

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

