package com.atlassian.bitbucket.internal.scm.git.lfs.store;

import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.internal.scm.git.lfs.embedded.EmbeddedStoreService;
import com.atlassian.bitbucket.internal.scm.git.lfs.jwt.GitLfsJwtHelper;
import com.atlassian.bitbucket.internal.scm.git.lfs.settings.GitLfsSettingsService;
import com.atlassian.bitbucket.nav.NavBuilder;
import com.atlassian.bitbucket.server.ApplicationMode;
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
import org.springframework.beans.factory.config.AbstractFactoryBean;

public class EmbeddedStoreAccessorFactoryBean extends AbstractFactoryBean<StoreAccessor> {

    private final EmbeddedStoreService embeddedStoreService;
    private final I18nService i18nService;
    private final GitLfsJwtHelper jwtHelper;
    private final NavBuilder navBuilder;
    private final ApplicationPropertiesService propertiesService;
    private final GitLfsSettingsService settingsService;

    public EmbeddedStoreAccessorFactoryBean(EmbeddedStoreService embeddedStoreService,
                                            I18nService i18nService,
                                            GitLfsJwtHelper jwtHelper,
                                            NavBuilder navBuilder,
                                            ApplicationPropertiesService propertiesService,
                                            GitLfsSettingsService settingsService) {
        this.embeddedStoreService = embeddedStoreService;
        this.i18nService = i18nService;
        this.jwtHelper = jwtHelper;
        this.navBuilder = navBuilder;
        this.propertiesService = propertiesService;
        this.settingsService = settingsService;
    }

    @Override
    public Class<?> getObjectType() {
        return StoreAccessor.class;
    }

    @Override
    protected StoreAccessor createInstance() throws Exception {
        return ApplicationMode.DEFAULT.equals(propertiesService.getMode()) ?
                new DefaultStoreAccessor(embeddedStoreService, i18nService, jwtHelper, navBuilder, settingsService) :
                new MirrorStoreAccessor(embeddedStoreService, i18nService, jwtHelper, navBuilder, settingsService);
    }
}