package cd.casic.plugin.exception;

import org.pf4j.DependencyResolver;
import org.springframework.web.server.ServerWebInputException;

import java.net.URI;
import java.util.List;

/**
 * @description: TODO
 * @author: mianbin
 * @date: 2025/5/14 10:53
 * @version: 1.0
 */

public abstract class PluginDependencyException extends ServerWebInputException {

    public PluginDependencyException(String reason) {
        super(reason);
    }

    public PluginDependencyException(String reason, Throwable cause) {
        super(reason, null, cause);
    }

    protected PluginDependencyException(String reason, Throwable cause,
                                        String messageDetailCode, Object[] messageDetailArguments) {
        super(reason, null, cause, messageDetailCode, messageDetailArguments);
    }

    public static class CyclicException extends PluginDependencyException {

        public static final String TYPE = "https://127.0.0.1:8090/probs/plugin-cyclic-dependency";

        public CyclicException() {
            super("A cyclic dependency was detected.");
            setType(URI.create(TYPE));
        }
    }

    public static class NotFoundException extends PluginDependencyException {

        public static final String TYPE = "https://127.0.0.1:8090/probs/plugin-dependencies-not-found";

        public NotFoundException(List<String> dependencies) {
            super("Dependencies were not found.", null, null, new Object[]{dependencies});
            setType(URI.create(TYPE));
            getBody().setProperty("dependencies", dependencies);
        }

    }

    public static class WrongVersionsException extends PluginDependencyException {

        public static final String TYPE =
                "https://127.0.0.1:8090/probs/plugin-dependencies-with-wrong-versions";

        public WrongVersionsException(List<DependencyResolver.WrongDependencyVersion> versions) {
            super("Dependencies have wrong version.", null, null, new Object[]{versions});
            setType(URI.create(TYPE));
            getBody().setProperty("versions", versions);
        }
    }
}
