package com.atlassian.bitbucket.installer.scripts;

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

import static com.atlassian.bitbucket.installer.InstallerConstants.*;

/**
 * SummaryScreen is the last screen of the installation before executing the real installation action script.
 * It summaries all selections made by the user and offer a last chance for the user to alter anything that's wrong.
 */
public class SummaryScreen extends AbstractInstallerScript {

    /*
     * check_for_user.sh and check_for_user.bat return 0 exit code if user exists
     */
    private static final int USER_EXISTS = 0;

    public SummaryScreen(InstallerContext context, InstallerUtils utils) {
        super(context, utils);
    }

    /**
     * The script is triggered when the user clicks "Next" on summary screen.
     *
     * @return {@code true} if user wants to continue installing. {@code false} if user stops at the console mode
     * or at the Git warning. NOTE that unattended mode will always return true.
     */
    public boolean validationScriptAtSummaryScreen() {
        if (getContext().isUnattended()) {
            // Don't bother showing either of the confirm dialogs if they're running unattended
            return true;
        }

        if (getContext().isConsole()) {
            boolean confirmed = getUtils().showConfirmDialog("", getContext().getMessage(I18N_WARNING_MESSAGE_INSTALL),
                    getContext().getMessage(I18N_WARNING_MESSAGE_EXIT), getContext().isUnattended());
            if (!confirmed) {
                // if we are on console and user chooses not to proceed then we exit
                getContext().finish(0);
                return true;
            }
        }

        if (!getInstallationDetails().isGitFound() || !getInstallationDetails().isGitSupported()) {
            return getUtils().showConfirmDialog(getContext().getMessage(I18N_ERROR_STASH_GIT_IGNORE_MESSAGE, PRODUCT_NAME_ARRAY_WRAP),
                    getContext().getMessage(I18N_MESSAGE_BUTTON_YES), getContext().getMessage(I18N_MESSAGE_BUTTON_NO),
                    getContext().isUnattended());
        }

        return true;
    }

    /**
     * This script is triggered before the summary screen is shown.
     * It will set a number of variables to {@link InstallationDetails} and the current
     * context so that the installation can use later.
     */
    public void preSummaryScreen() {

        getContext().getWizardContext().setNextButtonText(getContext().getMessage(I18N_SUMMARY_INSTALL_NEXT_BUTTON_TEXT));

        if (getInstallationDetails().isNewInstall()) {
            if (getInstallationDetails().isInstallService() && getContext().getBooleanVariable(KEY_APP_SERVICE_EXISTS)) {
                boolean confirmed = getUtils().showConfirmDialog(getContext().getMessage(I18N_WARNING_SERVICE_OVERRIDE, PRODUCT_NAME_ARRAY_WRAP),
                        getContext().getMessage(I18N_MESSAGE_BUTTON_YES),
                        getContext().getMessage(I18N_MESSAGE_BUTTON_NO),
                        getContext().isUnattended());
                if (!confirmed) {
                    getContext().goBack(1);
                    return;
                }
            }
        }

        // Update the context with the configuration provided by the user
        getInstallationDetails().updateContext(getContext());

        if (getUtils().isMacOS()) {
            hideInstallServiceLabel();
        } else if (getInstallationDetails().isNewInstall()) {
            showInstallServiceLabel(getContext().getMessage(I18N_SUMMARY_SERVICE_LABEL_INSTALL),
                    getContext().getBooleanVariable(KEY_APP_INSTALL_SERVICE));
        } else {
            // if it is upgrade workflow, we set the serivce install as true by default except when the service doesn't exist
            // or the user is not an admin.
            if (getInstallationDetails().isAdmin() && getContext().getBooleanVariable(KEY_APP_SERVICE_EXISTS)) {
                showInstallServiceLabel(getContext().getMessage(I18N_SUMMARY_SERVICE_LABEL_UPGRADE), true);
            } else {
                hideInstallServiceLabel();
            }
        }

        if (((Integer) getContext().getVariable(KEY_STASH_USER_QUERY)) == USER_EXISTS) {
            getContext().setVariable(KEY_SUMMARY_DELETE_STASH_USER_TEXT, getContext().getBooleanVariable(KEY_DELETE_STASH_USER) ?
                    getContext().getMessage(I18N_GENERIC_TEXT_YES) : getContext().getMessage(I18N_GENERIC_TEXT_NO));
        }

        getContext().setVariable(KEY_SUMMARY_SERVICE_TEXT, getContext().getBooleanVariable(KEY_APP_INSTALL_SERVICE) ?
                getContext().getMessage(I18N_GENERIC_TEXT_YES) : getContext().getMessage(I18N_GENERIC_TEXT_NO));

        if (getContext().isConsole()) {
            getUtils().showMessage(getContext().getMessage(I18N_SUMMARY_SCREEN_SUBTITLE_CONSOLE, new String[]{getContext().getCompilerVariable("sys.shortName")}));
        }

        // Installer pingback
        InstallerFactory.getTracker().track(getContext(), "installer-summary");
    }

    private void showInstallServiceLabel(String labelText, boolean installService) {
        getContext().setVariable(KEY_INSTALLER_SUMMARY_SERVICE_LABEL, labelText);
        getContext().setVariable(KEY_APP_SERVICE_LABEL_VISIBLE, true);
        getContext().setVariable(KEY_APP_INSTALL_SERVICE, installService);
    }

    private void hideInstallServiceLabel() {
        getContext().setVariable(KEY_APP_SERVICE_LABEL_VISIBLE, false);
        getContext().setVariable(KEY_APP_INSTALL_SERVICE, false);
    }
}
