/*
 * Copyright 2018 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.api.internal.artifacts.ivyservice.resolveengine.graph.builder;

import com.google.common.collect.ImmutableList;
import org.gradle.api.artifacts.VersionConstraint;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ComponentSelector;
import org.gradle.api.artifacts.component.ModuleComponentSelector;
import org.gradle.internal.component.external.model.ModuleDependencyMetadata;
import org.gradle.internal.component.model.DependencyMetadata;
import org.gradle.internal.component.model.ExcludeMetadata;
import org.gradle.internal.component.model.ForcingDependencyMetadata;
import org.gradle.internal.component.model.IvyArtifactName;

/**
 * Dependency metadata for edges involving a virtual platform. Used in two cases:
 * <ol>
 *     <li>A hard edge from a node to its owning virtual platform.</li>
 *     <li>A constraint edge from a virtual platform to a node of a participating module.</li>
 * </ol>
 */
class LenientPlatformDependencyMetadata implements ModuleDependencyMetadata, ForcingDependencyMetadata {

    private final ModuleComponentSelector selector;
    private final ComponentIdentifier platformId; // just for reporting
    private final boolean force;
    private final boolean constraint;

    LenientPlatformDependencyMetadata(
        ModuleComponentSelector selector,
        ComponentIdentifier platformId,
        boolean force,
        boolean constraint
    ) {
        this.selector = selector;
        this.platformId = platformId;
        this.force = force;
        this.constraint = constraint;
    }

    @Override
    public ModuleComponentSelector getSelector() {
        return selector;
    }

    @Override
    public ModuleDependencyMetadata withRequestedVersion(VersionConstraint requestedVersion) {
        throw new UnsupportedOperationException("Applying component metadata rules to lenient platform dependencies is not supported.");
    }

    @Override
    public ModuleDependencyMetadata withReason(String reason) {
        throw new UnsupportedOperationException("Applying component metadata rules to lenient platform dependencies is not supported.");
    }

    @Override
    public ModuleDependencyMetadata withEndorseStrictVersions(boolean endorse) {
        throw new UnsupportedOperationException("Applying component metadata rules to lenient platform dependencies is not supported.");
    }

    @Override
    public ImmutableList<ExcludeMetadata> getExcludes() {
        return ImmutableList.of();
    }

    @Override
    public ImmutableList<IvyArtifactName> getArtifacts() {
        return ImmutableList.of();
    }

    @Override
    public DependencyMetadata withTarget(ComponentSelector target) {
        // TODO: This gets called when performing substitutions.
        //       We probably shouldn't ignore this.
        return this;
    }

    @Override
    public DependencyMetadata withTargetAndArtifacts(ComponentSelector target, ImmutableList<IvyArtifactName> artifacts) {
        // TODO: This gets called when performing substitutions.
        //       We probably shouldn't ignore this.
        return this;
    }

    @Override
    public boolean isChanging() {
        return false;
    }

    @Override
    public boolean isTransitive() {
        return !constraint;
    }

    @Override
    public boolean isConstraint() {
        return constraint;
    }

    @Override
    public boolean isEndorsingStrictVersions() {
        return false;
    }

    @Override
    public String getReason() {
        return "belongs to platform " + platformId;
    }

    @Override
    public String toString() {
        return selector.getDisplayName();
    }

    @Override
    public boolean isForce() {
        return force;
    }

    @Override
    public ForcingDependencyMetadata forced() {
        return new LenientPlatformDependencyMetadata(selector, platformId, true, constraint);
    }

}
