package com.github.averyregier.club.application;

import com.github.averyregier.club.broker.ConfiguredConnector;
import com.github.averyregier.club.broker.Connector;
import com.github.averyregier.club.domain.UserManager;
import com.github.averyregier.club.domain.club.Program;
import com.github.averyregier.club.domain.club.adapter.ProgramAdapter;
import com.github.averyregier.club.rest.RestAPI;
import com.github.averyregier.club.view.*;
import spark.servlet.SparkApplication;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

import static spark.Spark.exception;
import static spark.SparkBase.setPort;

/**
 * Created by avery on 8/30/14.
 */
public class ClubApplication implements SparkApplication {
    public static void main(String... args) throws SQLException, ClassNotFoundException {
        if(args.length > 0) {
            setPort(Integer.parseInt(args[0]));
        }
        new ClubApplication().init();
    //    connect();

    }

    private final UserManager userManager = new UserManager();
    private Program program;
    private Connector connector;

    @Override
    public void init() {
        loadConfig();

        exception(Exception.class, (e, request, response) -> {
            response.status(404);
            response.body(e.getLocalizedMessage());
            e.printStackTrace();
        });

        spark.Spark.staticFileLocation("/public");

        new FastSetup().init(this);
        new Login().init(this);
        new SetupController().init(this);
        new RegistrationController().init(this);
        new RestAPI().init(this);
        new ClubController().init(this);
    }

    private void loadConfig() {
        try {
            Properties config = new Properties();
            config.load(getClass().getResourceAsStream("config.properties"));
            connector = new ConfiguredConnector(config);
        } catch (IOException|ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public Program setupProgram(String organizationName, String curriculum, String acceptLanguage) {
        program = new ProgramAdapter(acceptLanguage, organizationName, curriculum);
        program.setPersonManager(userManager.getPersonManager());
        return program;
    }

    public Program getProgram() {
        return program;
    }

    public Connector getConnector() {
        return connector;
    }
}

@@ -493,32 +493,60 @@
 io.IOException;%0A
+import java.io.InputStream;%0A
 import java.sql.
@@ -1798,20 +1798,29 @@
     
-config.load(
+InputStream stream = 
 getC
@@ -1866,19 +1866,92 @@
 erties%22)
-)
 ;%0A
+            if(stream != null) %7B%0A                config.load(stream);%0A    
         
@@ -1999,16 +1999,30 @@
 onfig);%0A
+            %7D%0A
         

Allow the application to start when the config can't be found.