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

import com.atlassian.plugin.Plugin;
import com.atlassian.bitbucket.util.ProxyUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Provides very minimal support for the {@code Plugin} interface, based on the methods used by SAL's
 * {@code PluginUpgrader}. The real {@code Plugin} <i>is not available</i> at the point in the plugin
 * framework lifecycle where {@link SalGitUpgradeManager} processes upgrade tasks.
 *
 * @since 2.9
 */
public class SalPlugin implements InvocationHandler {

    public static final String PLUGIN_KEY = "com.atlassian.bitbucket.server.bitbucket-git";

    private SalPlugin() {
    }

    public static Plugin create() {
        return ProxyUtils.createProxy(Plugin.class, new SalPlugin());
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        if ("getKey".equals(methodName) || "toString".equals(methodName)) {
            return PLUGIN_KEY;
        }
        if ("equals".equals(methodName) && args.length == 1) {
            return proxy == args[0];
        }
        if ("hashCode".equals(methodName)) {
            return hashCode();
        }

        throw new UnsupportedOperationException(methodName + " is not supported");
    }
}
