package com.nbusy.app;

import android.app.Application;
import android.content.Context;

import com.nbusy.app.data.Config;
import com.nbusy.app.data.DB;
import com.nbusy.app.data.InMemDB;
import com.nbusy.app.data.Profile;
import com.nbusy.app.data.sqldb.SQLDB;
import com.nbusy.app.worker.ConnManager;
import com.nbusy.app.worker.Worker;
import com.nbusy.app.worker.eventbus.EventBus;
import com.nbusy.sdk.Client;
import com.nbusy.sdk.ClientImpl;

/**
 * Creator and keeper of all the instances.
 * Values are initialized on first request.
 * All instances are singletons unless otherwise mentioned.
 */
public class InstanceProvider extends Application {
    private static final Config config = new Config();
    private static Context appContext;
    private static Worker worker;
    private static ConnManager connManager;
    private static Client client;
    private static EventBus eventBus;
    private static DB db;

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = getApplicationContext();
    }

    public static Context getAppContext() {
        return appContext;
    }

    public static synchronized Worker getWorker() {
        if (worker == null) {
            worker = new Worker(getClient(), getEventBus(), getDB());
        }

        return worker;
    }

    public static synchronized ConnManager getConnManager(Profile userProfile) {
        if (connManager == null) {
            if (userProfile == null) {
                throw new IllegalArgumentException("userProfile cannot be null when ConnManager is not initialized");
            }

            connManager = new ConnManager(getClient(), getEventBus(), getDB(), getAppContext(), userProfile);
        }

        return connManager;
    }

    public static synchronized Client getClient() {
        if (client == null) {
            if (config.serverUrl != null) {
                client = new ClientImpl(config.serverUrl, false);
            } else {
                client = new ClientImpl();
            }
        }

        return client;
    }

    public static synchronized EventBus getEventBus() {
        if (eventBus == null) {
            eventBus = new EventBus();
        }

        return eventBus;
    }

    public static synchronized DB getDB() {
        if (db == null) {
            if (config.env == Config.Env.PRODUCTION) {
                db = new SQLDB();
            } else {
                db = new InMemDB();
            }
        }

        return db;
    }
}

@@ -673,42 +673,26 @@
 tic 
-final Config config = new Config()
+Context appContext
 ;%0A  
@@ -707,39 +707,34 @@
 e static Con
-text appContext
+fig config
 ;%0A    privat
@@ -925,16 +925,29 @@
  public 
+synchronized 
 void onC
@@ -1042,24 +1042,37 @@
 %0A    public 
+synchronized 
 static Conte
@@ -1118,32 +1118,190 @@
 Context;%0A    %7D%0A%0A
+    public static synchronized Config getConfig() %7B%0A        if (config == null) %7B%0A            config = new Config();%0A        %7D%0A%0A        return config;%0A    %7D%0A%0A
     public stati
@@ -2019,22 +2019,27 @@
     if (
-c
+getC
 onfig
+()
 .serverU
@@ -2092,22 +2092,27 @@
 entImpl(
-c
+getC
 onfig
+()
 .serverU
@@ -2498,22 +2498,27 @@
     if (
-c
+getC
 onfig
+()
 .env == 

initialize config on first request