package com.p3.bewoplaner;

import com.crashlytics.android.Crashlytics;
import com.p3.bewoplaner.data.firebase.MessagingInstanceIdService;
import com.p3.bewoplaner.di.ApplicationComponent;
import com.p3.bewoplaner.di.ApplicationModule;
import com.p3.bewoplaner.di.DaggerApplicationComponent;
import com.p3.bewoplaner.di.InjectionHelper;

import android.app.Application;

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

/**
 * Created by imarneanu on 2/9/17.
 */
public class BewoplanerApplication extends Application {

    private ApplicationComponent mApplicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this);
        if (!BuildConfig.DEBUG) {
            Fabric.with(this, new Crashlytics());
        }
        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();
    }

}
