package com.atlassian.bitbucket.installer.scripts;

import com.atlassian.bitbucket.installer.InstallationDetails;
import com.atlassian.bitbucket.installer.utils.InstallerUtils;
import com.google.common.annotations.VisibleForTesting;
import com.install4j.api.context.InstallerContext;

/**
 * This class is a generic base wrapping some useful common properties and methods for all the screen scripts.
 * The most common used properties are context, installationDetails and utils.
 *
 * Any Screen (or Action) instance in .install4j project should extend this class when the code is migrating from
 * the xml file to the java world.
 */
abstract class AbstractInstallerScript {

    private final InstallerContext context;
    private final InstallerUtils utils;
    private InstallationDetails installationDetails;

    AbstractInstallerScript(InstallerContext context, InstallerUtils utils) {
        this(context, utils, null);
    }

    @VisibleForTesting
    AbstractInstallerScript(InstallerContext context, InstallerUtils utils, InstallationDetails installationDetails) {
        this.context = context;
        this.utils = utils;
        this.installationDetails = installationDetails;
    }

    /**
     *
     * @return The current context of the installation
     */
    InstallerContext getContext() {
        return context;
    }

    /**
     *
     * @return InstallationDetails will be retrieve from the current context if it's not yet assigned.
     */
    InstallationDetails getInstallationDetails() {
        if (installationDetails == null) {
            installationDetails = InstallationDetails.getInstance(context);
        }
        return installationDetails;
    }

    /**
     *
     * @return The helper class that wraps {@link com.atlassian.plugin.manager.PluginPersistentState.Util}
     */
    InstallerUtils getUtils() {
        return utils;
    }
}
