package com.atlassian.stash.internal.rest.fragment;

import com.atlassian.bitbucket.rest.fragment.RestFragment;
import com.atlassian.bitbucket.scm.BaseWeightedModuleDescriptor;
import com.atlassian.plugin.Plugin;
import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.module.ModuleFactory;
import com.atlassian.plugin.util.validation.ValidationPattern;
import org.dom4j.Element;

import javax.annotation.Nonnull;

import static com.atlassian.plugin.util.validation.ValidationPattern.test;

public class RestFragmentModuleDescriptor extends BaseWeightedModuleDescriptor<RestFragment> {

    private String location = null;

    public RestFragmentModuleDescriptor(ModuleFactory moduleFactory) {
        super(moduleFactory, 100);
    }

    @Nonnull
    public String getLocation() {
        return location;
    }

    @Nonnull
    @Override
    public RestFragment getModule() {
        return moduleFactory.createModule(moduleClassName, this);
    }

    @Override
    public void init(@Nonnull Plugin plugin, @Nonnull Element element) throws PluginParseException {
        super.init(plugin, element);

        location = element.attribute("location").getValue();
    }

    @Override
    protected void provideValidationRules(@Nonnull ValidationPattern pattern) {
        super.provideValidationRules(pattern);

        pattern.rule(test("@class").withError("The class is required"));
        pattern.rule(test("@location").withError("The location is required"));
    }
}