package de.beyondsoft.ownchat;

import com.crashlytics.android.Crashlytics;
import de.beyondsoft.ownchat.data.firebase.MessagingInstanceIdService;
import de.beyondsoft.ownchat.data.firebase.NotificationUtils;
import de.beyondsoft.ownchat.di.ApplicationComponent;
import de.beyondsoft.ownchat.di.ApplicationModule;
import de.beyondsoft.ownchat.di.DaggerApplicationComponent;
import de.beyondsoft.ownchat.di.InjectionHelper;

import android.content.Context;
import android.support.multidex.MultiDexApplication;

import io.fabric.sdk.android.Fabric;
import io.realm.Realm;

public class BewoplanerApplication extends MultiDexApplication {

    private ApplicationComponent mApplicationComponent;
    private static Context mContext;

    private static NotificationUtils mNotificationUtils;

    @Override
    public void onCreate() {
        super.onCreate();

        Realm.init(this);

        if(!BuildConfig.DEBUG) {
            Fabric.with(this, new Crashlytics());
        }

        mContext = getApplicationContext();

        buildGraphAndInject();
        MessagingInstanceIdService.setApplicationContext(this);
    }

    private void buildGraphAndInject() {
        final ApplicationModule applicationModule = new ApplicationModule(this);

        mApplicationComponent = DaggerApplicationComponent.builder()
                .applicationModule(applicationModule)
                .build();

        mApplicationComponent.inject(this);
    }

    public ApplicationComponent getApplicationComponent() {
        return mApplicationComponent;
    }

    public void resetApplicationComponent() {
        mApplicationComponent = null;
        InjectionHelper.destroyMainComponents();
        buildGraphAndInject();
    }

    public static Context getAppContext() {
        return mContext;
    }

    public static NotificationUtils getNotificationUtils() {
        if(mNotificationUtils == null) {
            mNotificationUtils = new NotificationUtils(mContext);
        }

        return mNotificationUtils;
    }
}
