package de.beyondsoft.ownchat;

import com.crashlytics.android.Crashlytics;
import de.beyondsoft.ownchat.data.firebase.MessagingInstanceIdService;
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.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();
    }

}
