package com.atlassian.bitbucket.installer.scripts;

import com.atlassian.bitbucket.installer.InstallationDetails;
import com.atlassian.bitbucket.installer.utils.InstallerUtils;
import com.atlassian.bitbucket.installer.validation.HardValidationException;
import com.atlassian.bitbucket.installer.validation.SoftValidationException;
import com.install4j.api.Util;
import com.install4j.api.context.InstallerContext;

import java.io.File;
import java.io.IOException;

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

/**
 * Home location screen refers to the screen that expects the user to set the home location of Stash.
 */
public class HomeLocationScreen extends AbstractInstallerScript {

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

    /**
     * This method does the following things.
     * 1) The label text is set according to the installation type (either new install or upgrade)
     * This text will be shown above the file input on the screen.
     * 2) Tries to guess the home directory if its a upgrade.
     */
    public void initHomeDirectoryScreen() {
        InstallationDetails installationDetails = getInstallationDetails();
        boolean newInstall = installationDetails.isNewInstall();

        getContext().setVariable(KEY_HOME_DIRECTORY_INFO_TEXT, newInstall ?
                getContext().getMessage(I18N_CONFIGURE_INSTALLATION_HOME_DIRECTORY_INFO_NEW_INSTALL_TEXT, PRODUCT_NAME_ARRAY_WRAP) :
                getContext().getMessage(I18N_CONFIGURE_INSTALLATION_HOME_DIRECTORY_INFO_UPGRADE_TEXT));

       installationDetails.initHomeDirectory();
    }

    /**
     * This method validates the home directory before setting it to context.
     * It is used when "Next" button is clicked on the Screen Home Location.
     *
     * @return {@code true} if no hard exception or user accepts the soft warning. The installation will then proceed to the next screen.
     * {@code false} otherwise, and the installation will remain on the same screen.
     */
    public boolean validateHomeDirectory() {
        File stashHome = new File((String) getContext().getVariable(KEY_APP_BITBUCKET_HOME));

        return validateHomeDirectory(stashHome, false);
    }

    private boolean validateHomeDirectory(File stashHome, boolean skipUsedDirectorySoftWarning) {
        String homeDirectoryName = getContext().getMessage(I18N_ERROR_DIRECTORY_USED_HOME);

        try {
            getInstallationDetails().setHomeDirectory(stashHome, skipUsedDirectorySoftWarning);
        } catch (SoftValidationException e) {
            String errorMessage = getContext().getMessage(e.getMessageKey(), new String[]{homeDirectoryName});
            getUtils().logInfo(errorMessage);

            String warningMessage = String.format("%s %s",
                    errorMessage,
                    getContext().getMessage(I18N_WARNING_MESSAGE_CONTINUE));

            boolean confirmed = getUtils().showConfirmDialog(warningMessage,
                    getContext().getMessage(I18N_GENERIC_TEXT_YES), getContext().getMessage(I18N_GENERIC_TEXT_NO),
                    getContext().isUnattended());
            return confirmed && validateHomeDirectory(stashHome, true);
        } catch (HardValidationException e) {
            String errorMessage = getContext().getMessage(e.getMessageKey(), new String[]{homeDirectoryName});
            getUtils().logInfo(errorMessage);
            Util.showErrorMessage(errorMessage);
            return false;
        }

        return true;
    }
}
