/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.internal.component.external.model;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import org.gradle.api.artifacts.VersionConstraint;
import org.gradle.api.artifacts.component.ComponentSelector;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.artifacts.component.ModuleComponentSelector;
import org.gradle.api.artifacts.component.ProjectComponentSelector;
import org.gradle.api.internal.attributes.AttributesSchemaInternal;
import org.gradle.api.internal.attributes.ImmutableAttributes;
import org.gradle.internal.component.local.model.DefaultProjectDependencyMetadata;
import org.gradle.internal.component.model.AttributeConfigurationSelector;
import org.gradle.internal.component.model.ComponentResolveMetadata;
import org.gradle.internal.component.model.ConfigurationMetadata;
import org.gradle.internal.component.model.DependencyMetadata;
import org.gradle.internal.component.model.ExcludeMetadata;
import org.gradle.internal.component.model.IvyArtifactName;

import java.util.List;

/**
 * A `ModuleDependencyMetadata` implementation that is backed by an `ExternalDependencyDescriptor` bound to a particular
 * source `ConfigurationMetadata`. The reason for this is that the Ivy and Maven dependency descriptors resolve target components
 * differently based on the configuration that they are sourced from.
 */
public class ConfigurationBoundExternalDependencyMetadata implements ModuleDependencyMetadata {
    private final ConfigurationMetadata configuration;
    private final ModuleComponentIdentifier componentId;
    private final ExternalDependencyDescriptor dependencyDescriptor;
    private final String reason;

    private ConfigurationBoundExternalDependencyMetadata(ConfigurationMetadata configuration, ModuleComponentIdentifier componentId, ExternalDependencyDescriptor dependencyDescriptor, String reason) {
        this.configuration = configuration;
        this.componentId = componentId;
        this.dependencyDescriptor = dependencyDescriptor;
        this.reason = reason;
    }

    public ConfigurationBoundExternalDependencyMetadata(ConfigurationMetadata configuration, ModuleComponentIdentifier componentId, ExternalDependencyDescriptor dependencyDescriptor) {
        this(configuration, componentId, dependencyDescriptor, null);
    }

    /**
     * Choose a set of target configurations based on: a) the consumer attributes, b) the fromConfiguration and c) the target component.
     *
     * Use attribute matching to choose a single variant when:
     *   - The target component has variants AND
     *   - Either: we have consumer attributes OR the target component is from an external repository (not a Gradle project).
     *
     * Otherwise, revert to legacy selection of target configurations.
     */
    @Override
    public List<ConfigurationMetadata> selectConfigurations(ImmutableAttributes consumerAttributes, ComponentResolveMetadata targetComponent, AttributesSchemaInternal consumerSchema) {
        if (!targetComponent.getVariantsForGraphTraversal().isEmpty()) {
            // This condition shouldn't be here, and attribute matching should always be applied when the target has variants
            // however, the schemas and metadata implementations are not yet set up for this, so skip this unless:
            // - the consumer has asked for something specific (by providing attributes), as the other metadata types are broken for the 'use defaults' case
            // - or the target is a component from a Maven/Ivy repo as we can assume this is well behaved
            if (!consumerAttributes.isEmpty() || targetComponent instanceof ModuleComponentResolveMetadata) {
                return ImmutableList.of(AttributeConfigurationSelector.selectConfigurationUsingAttributeMatching(consumerAttributes, targetComponent, consumerSchema));
            }
        }
        return dependencyDescriptor.selectLegacyConfigurations(componentId, configuration, targetComponent);
    }

    @Override
    public List<IvyArtifactName> getArtifacts() {
        return dependencyDescriptor.getConfigurationArtifacts(configuration);
    }

    @Override
    public List<ExcludeMetadata> getExcludes() {
        return dependencyDescriptor.getConfigurationExcludes(configuration.getHierarchy());
    }

    @Override
    public DependencyMetadata withTarget(ComponentSelector target) {
        if (target instanceof ModuleComponentSelector) {
            ModuleComponentSelector moduleTarget = (ModuleComponentSelector) target;
            ModuleComponentSelector newSelector = DefaultModuleComponentSelector.newSelector(moduleTarget.getGroup(), moduleTarget.getModule(), moduleTarget.getVersionConstraint());
            if (newSelector.equals(getSelector())) {
                return this;
            }
            return withRequested(newSelector);
        } else if (target instanceof ProjectComponentSelector) {
            ProjectComponentSelector projectTarget = (ProjectComponentSelector) target;
            return new DefaultProjectDependencyMetadata(projectTarget, this);
        } else {
            throw new IllegalArgumentException("Unexpected selector provided: " + target);
        }
    }

    @Override
    public ModuleDependencyMetadata withRequestedVersion(VersionConstraint requestedVersion) {
        ModuleComponentSelector selector = getSelector();
        if (requestedVersion.equals(selector.getVersionConstraint())) {
            return this;
        }
        ModuleComponentSelector newSelector = DefaultModuleComponentSelector.newSelector(selector.getGroup(), selector.getModule(), requestedVersion);
        return withRequested(newSelector);
    }

    @Override
    public ModuleDependencyMetadata withReason(String reason) {
        if (Objects.equal(reason, this.getReason())) {
            return this;
        }
        return new ConfigurationBoundExternalDependencyMetadata(configuration, componentId, dependencyDescriptor, reason);
    }

    private ModuleDependencyMetadata withRequested(ModuleComponentSelector newSelector) {
        ExternalDependencyDescriptor newDelegate = dependencyDescriptor.withRequested(newSelector);
        return new ConfigurationBoundExternalDependencyMetadata(configuration, componentId, newDelegate);
    }

    @Override
    public ModuleComponentSelector getSelector() {
        return dependencyDescriptor.getSelector();
    }

    @Override
    public boolean isChanging() {
        return dependencyDescriptor.isChanging();
    }

    @Override
    public boolean isTransitive() {
        return dependencyDescriptor.isTransitive();
    }

    @Override
    public boolean isPending() {
        return dependencyDescriptor.isOptional();
    }

    @Override
    public String getReason() {
        return reason;
    }
}

@@ -3668,691 +3668,373 @@
     
-if (!targetComponent.getVariantsForGraphTraversal().isEmpty()) %7B%0A            // This condition shouldn't be here, and attribute matching should always be applied when the target has variants%0A            // however, the schemas and metadata implementations are not yet set up for this, so skip this unless:%0A            // - the consumer has asked for something specific (by providing attributes), as the other metadata types are broken for the 'use defaults' case%0A            // - or the target is a component from a Maven/Ivy repo as we can assume this is well behaved%0A            if (!consumerAttributes.isEmpty() %7C%7C targetComponent instanceof ModuleComponentResolveMetadata) %7B%0A    
+// This is a slight different condition than that used for a dependency declared in a Gradle project,%0A        // which is (targetHasVariants %7C%7C consumerHasAttributes), relying on the fallback to 'default' for consumer attributes without any variants.%0A        if (hasVariants(targetComponent) && (hasAttributes(consumerAttributes) %7C%7C isExternal(targetComponent))) %7B%0A
     
@@ -4193,30 +4193,16 @@
 hema));%0A
-            %7D%0A
         
@@ -4311,32 +4311,458 @@
 ponent);%0A    %7D%0A%0A
+    private boolean hasVariants(ComponentResolveMetadata targetComponent) %7B%0A        return !targetComponent.getVariantsForGraphTraversal().isEmpty();%0A    %7D%0A%0A    private boolean hasAttributes(ImmutableAttributes attributes) %7B%0A        return !attributes.isEmpty();%0A    %7D%0A%0A    private boolean isExternal(ComponentResolveMetadata targetComponent) %7B%0A        return targetComponent instanceof ModuleComponentResolveMetadata;%0A    %7D%0A%0A
     @Override%0A  

Simplify condition that checks to do attribute matching