package de.beyondsoft.ownchat.di;

import de.beyondsoft.ownchat.BewoplanerApplication;
import de.beyondsoft.ownchat.data.firebase.DaggerMessagingComponent;
import de.beyondsoft.ownchat.data.firebase.MessagingComponent;
import de.beyondsoft.ownchat.page.chat.ChatComponent;
import de.beyondsoft.ownchat.page.chat.DaggerChatComponent;
import de.beyondsoft.ownchat.page.contacts.ContactsComponent;
import de.beyondsoft.ownchat.page.contacts.DaggerContactsComponent;
import de.beyondsoft.ownchat.page.fileconfirmation.DaggerFileConfirmationComponent;
import de.beyondsoft.ownchat.page.fileconfirmation.FileConfirmationComponent;
import de.beyondsoft.ownchat.page.login.DaggerLoginComponent;
import de.beyondsoft.ownchat.page.login.LoginComponent;
import de.beyondsoft.ownchat.page.main.DaggerMainComponent;
import de.beyondsoft.ownchat.page.main.MainComponent;
import de.beyondsoft.ownchat.page.profile.DaggerProfileComponent;
import de.beyondsoft.ownchat.page.profile.ProfileComponent;
import de.beyondsoft.ownchat.page.sharing.DaggerSharingComponent;
import de.beyondsoft.ownchat.page.sharing.SharingComponent;
import de.beyondsoft.ownchat.page.splash.DaggerSplashComponent;
import de.beyondsoft.ownchat.page.splash.SplashComponent;

import android.content.Context;

/**
 * Created by imarneanu on 2/9/17.
 */
public class InjectionHelper {

    private static SplashComponent sSplashComponent;
    private static ContactsComponent sContactsComponent;
    private static LoginComponent sLoginComponent;
    private static MainComponent sMainComponent;
    private static MessagingComponent sMessagingComponent;
    private static ChatComponent sChatComponent;
    private static ProfileComponent sProfileComponent;
    private static FileConfirmationComponent sFileConfirmationComponent;
    private static SharingComponent sSharingComponent;

    public static ApplicationComponent getApplicationComponent(Context context) {
        return ((BewoplanerApplication) context.getApplicationContext()).getApplicationComponent();
    }

    public static SharingComponent getSharingComponent(Context context) {
        if(sSharingComponent == null) {
            sSharingComponent = DaggerSharingComponent.builder()
                    .applicationComponent(getApplicationComponent(context))
                    .build();
        }

        return sSharingComponent;
    }

    public static SplashComponent getSplashComponent(Context context) {
        if (sSplashComponent == null) {
            sSplashComponent = DaggerSplashComponent.builder()
                    .applicationComponent(getApplicationComponent(context))
                    .build();
        }

        return sSplashComponent;
    }

    public static LoginComponent getLoginComponent(Context context) {
        if (sLoginComponent == null) {
            sLoginComponent = DaggerLoginComponent.builder()
                    .applicationComponent(getApplicationComponent(context))
                    .build();
        }

        return sLoginComponent;
    }

    public static MainComponent getMainComponent(Context context) {
        if (sMainComponent == null) {
            sMainComponent = DaggerMainComponent.builder()
                    .applicationComponent(getApplicationComponent(context))
                    .build();
        }

        return sMainComponent;
    }

    public static ContactsComponent getContactsComponent(Context context) {
        if (sContactsComponent == null) {
            sContactsComponent = DaggerContactsComponent.builder()
                    .applicationComponent(getApplicationComponent(context))
                    .build();
        }

        return sContactsComponent;
    }

    public static MessagingComponent getMessagingComponent(Context context) {
        if (sMessagingComponent == null) {
            sMessagingComponent = DaggerMessagingComponent.builder()
                    .applicationComponent(getApplicationComponent(context))
                    .build();
        }

        return sMessagingComponent;
    }

    public static ProfileComponent getProfileComponent(Context context) {
        if (sProfileComponent == null) {
            sProfileComponent = DaggerProfileComponent.builder()
                    .applicationComponent(getApplicationComponent(context))
                    .build();
        }

        return sProfileComponent;
    }

    public static ChatComponent getChatComponent(Context context) {
        if (sChatComponent == null) {
            sChatComponent = DaggerChatComponent.builder()
                    .applicationComponent(getApplicationComponent(context))
                    .build();
        }

        return sChatComponent;
    }

    public static FileConfirmationComponent getFileConfirmationComponent(Context context) {
        if(sFileConfirmationComponent == null) {
            sFileConfirmationComponent = DaggerFileConfirmationComponent.builder()
                    .applicationComponent(getApplicationComponent(context))
                    .build();
        }

        return sFileConfirmationComponent;
    }

    public static void destroySplashComponent() {
        sSplashComponent = null;
    }

    public static void destroyMainComponents() {
        sContactsComponent = null;
        sMainComponent = null;
        sContactsComponent = null;
        sProfileComponent = null;
        sMessagingComponent = null;
    }

    public static void destroyChatComponent() {
        sChatComponent = null;
    }

    public static void destroyLoginComponent() {
        sLoginComponent = null;
    }
}