package com.atlassian.stash.internal.hook.repository;

import com.atlassian.bitbucket.hook.repository.RepositoryHookDetails;
import com.atlassian.bitbucket.hook.repository.RepositoryHookType;
import com.atlassian.stash.internal.plugin.RepositoryHookModuleDescriptor;
import com.google.common.base.Function;

import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkNotNull;

public class SimpleRepositoryHookDetails implements RepositoryHookDetails {

    private final String configFormKey;
    private final String description;
    private final String iconPath;
    private final String key;
    private final String name;
    private final RepositoryHookType type;
    private final String version;

    public SimpleRepositoryHookDetails(
            String key,
            String name,
            RepositoryHookType type,
            String description,
            String version,
            String iconPath,
            String configFormKey) {
        this.key = checkNotNull(key, "key");
        this.name = checkNotNull(name, "name");
        this.type = checkNotNull(type, "type");
        this.description = description;
        this.version = version;
        this.iconPath = iconPath;
        this.configFormKey = configFormKey;
    }

    @Nonnull
    @Override
    public String getKey() {
        return key;
    }

    @Nonnull
    @Override
    public String getName() {
        return name;
    }

    @Nonnull
    @Override
    public RepositoryHookType getType() {
        return type;
    }

    @Override
    public String getDescription() {
        return description;
    }

    // Internal only - for getAvatar
    protected String getIconPath() {
        return iconPath;
    }

    @Override
    public String getConfigFormKey() {
        return configFormKey;
    }

    @Nonnull
    @Override
    public String getVersion() {
        return version;
    }

    public static final Function<RepositoryHookModuleDescriptor, SimpleRepositoryHookDetails> FROM_MODULE_DESCRIPTOR = new Function<RepositoryHookModuleDescriptor, SimpleRepositoryHookDetails>() {
        public SimpleRepositoryHookDetails apply(RepositoryHookModuleDescriptor descriptor) {
            return new SimpleRepositoryHookDetails(
                    descriptor.getCompleteKey(),
                    descriptor.getI18nName(),
                    descriptor.getType(),
                    descriptor.getI18nDescription(),
                    descriptor.getPlugin().getPluginInformation().getVersion(),
                    descriptor.getIconPath(),
                    descriptor.getConfigFormKey()
            );
        }
    };
}
