package com.atlassian.bitbucket.installer.scripts;

import com.atlassian.bitbucket.installer.InstallationDetails;
import com.atlassian.bitbucket.installer.InstallationType;
import com.atlassian.bitbucket.installer.utils.InstallerUtils;
import com.install4j.api.context.InstallerContext;
import com.install4j.api.context.WizardContext;

import static com.atlassian.bitbucket.installer.InstallerConstants.KEY_INSTALLATION_DETAILS;
import static org.mockito.Mockito.*;

public abstract class BaseInstallerTest {

    private InstallerContext context = mock(InstallerContext.class);
    private InstallationDetails installationDetails = createInstallationDetails();
    private InstallerUtils utils = mock(InstallerUtils.class);


    private InstallationDetails createInstallationDetails() {
        InstallationDetails installationDetails = mock(InstallationDetails.class);

        when(getContext().getVariable(KEY_INSTALLATION_DETAILS)).thenReturn(installationDetails);

        return installationDetails;
    }

    InstallerContext getContext() {
        return context;
    }

    InstallationDetails getInstallationDetails() {
        return installationDetails;
    }

    void setDefaultInstall() {
        when(getInstallationDetails().getInstallType()).thenReturn(InstallationType.INSTALL);
        when(getInstallationDetails().isNewInstall()).thenReturn(true);
        when(getInstallationDetails().isUpgrade()).thenReturn(false);
    }

    void setDataCenterInstall() {
        when(getInstallationDetails().getInstallType()).thenReturn(InstallationType.DATA_CENTER_INSTALL);
        when(getInstallationDetails().isNewInstall()).thenReturn(true);
        when(getInstallationDetails().isUpgrade()).thenReturn(false);
    }

    void setMirrorInstall() {
        when(getInstallationDetails().getInstallType()).thenReturn(InstallationType.MIRROR_INSTALL);
        when(getInstallationDetails().isNewInstall()).thenReturn(true);
        when(getInstallationDetails().isUpgrade()).thenReturn(false);
    }

    void setUpgrade() {
        when(getInstallationDetails().getInstallType()).thenReturn(InstallationType.UPGRADE);
        when(getInstallationDetails().isNewInstall()).thenReturn(false);
        when(getInstallationDetails().isUpgrade()).thenReturn(true);
    }

    void setDataCenterUpgrade() {
        when(getInstallationDetails().getInstallType()).thenReturn(InstallationType.DATA_CENTER_UPGRADE);
        when(getInstallationDetails().isNewInstall()).thenReturn(false);
        when(getInstallationDetails().isUpgrade()).thenReturn(true);
    }

    void setMirrorUpgrade() {
        when(getInstallationDetails().getInstallType()).thenReturn(InstallationType.MIRROR_UPGRADE);
        when(getInstallationDetails().isNewInstall()).thenReturn(false);
        when(getInstallationDetails().isUpgrade()).thenReturn(true);
    }

    void setI18nMessageReturn(String i18nKey, String i18nText) {
        when(getContext().getMessage(i18nKey)).thenReturn(i18nText);
        when(getContext().getMessage(eq(i18nKey), any(Object[].class))).thenReturn(i18nText);
    }

    WizardContext createWizardContext(InstallerContext context) {
        WizardContext wizardContext = mock(WizardContext.class);
        when(context.getWizardContext()).thenReturn(wizardContext);
        return wizardContext;
    }

    InstallerUtils getUtils() {
        return utils;
    }
}
