package com.atlassian.stash.internal.scm.git.upgrade.sal;

import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.upgrade.PluginUpgradeTask;
import com.atlassian.sal.core.upgrade.PluginUpgrader;

import java.util.List;

/**
 * Extends SAL's {@code PluginUpgrader} because it has no public methods.
 *
 * @since 2.9
 */
public class SalPluginUpgrader extends PluginUpgrader {

    public SalPluginUpgrader(PluginSettings pluginSettings, List<PluginUpgradeTask> upgradeTasks) {
        super(SalPlugin.create(), pluginSettings, upgradeTasks);
    }

    /**
     * @return {@code true} if at least one {@code PluginUpgradeTask} was applied; otherwise, {@code false} if all
     *         tasks had already been applied
     */
    public boolean apply() { //This is a replacement for upgrade() on the base class
        boolean needed = needUpgrade();
        if (needed) {
            doUpgrade();
        }

        return needed;
    }

    /**
     * @return {@code true} if at least one {@code PluginUpgradeTask} needs to be applied; otherwise, {@code false}
     *         if all tasks have already been applied
     * @since 3.5
     */
    public boolean isNeeded() {
        return needUpgrade();
    }

    /**
     * @return {@code true} if all {@code PluginUpgradeTask}s were applied without errors; otherwise, {@code false}
     *         if at least one error was encountered while upgrading
     */
    public boolean isSuccessful() {
        return getErrors().isEmpty();
    }
}
