package org.implab.gradle.common.sources;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.gradle.api.Action;
import org.gradle.api.Task;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.attributes.HasConfigurableAttributes;
import org.implab.gradle.common.core.lang.Closures;

import groovy.lang.Closure;
import groovy.lang.DelegatesTo;

@NonNullByDefault
public final class OutgoingArtifactSlotPublication {
    private final String slotName;
    private final boolean primary;
    private final VariantArtifactSlot slot;
    private final ArtifactAssembly assembly;
    private final HasConfigurableAttributes<?> attributesCarrier;

    OutgoingArtifactSlotPublication(
            String slotName,
            boolean primary,
            VariantArtifactSlot slot,
            ArtifactAssembly assembly,
            HasConfigurableAttributes<?> attributesCarrier) {
        this.slotName = slotName;
        this.primary = primary;
        this.slot = slot;
        this.assembly = assembly;
        this.attributesCarrier = attributesCarrier;
    }

    public String slotName() {
        return slotName;
    }

    public boolean primary() {
        return primary;
    }

    public VariantArtifactSlot slot() {
        return slot;
    }

    public void configureTask(Action<? super Task> action) {
        assembly.task().configure(action::execute);
    }

    public void configureTask(
            @DelegatesTo(value = Task.class, strategy = Closure.DELEGATE_FIRST) Closure<?> action) {
        configureTask(Closures.action(action));
    }

    public void configureArtifactAttributes(Action<? super AttributeContainer> action) {
        attributesCarrier.attributes(action);
    }

    public void configureArtifactAttributes(
            @DelegatesTo(value = AttributeContainer.class, strategy = Closure.DELEGATE_FIRST) Closure<?> action) {
        configureArtifactAttributes(Closures.action(action));
    }
}
