/*
 * 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.vcs.internal;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.invocation.Gradle;
import org.gradle.internal.Actions;
import org.gradle.util.CollectionUtils;
import org.gradle.vcs.VcsMapping;
import org.gradle.vcs.VersionControlSpec;

import java.util.Map;
import java.util.Set;

public class DefaultVcsMappingsStore implements VcsMappingsStore {
    private final Set<Action<VcsMapping>> rootVcsMappings = Sets.newLinkedHashSet();
    private final Map<Gradle, Set<Action<VcsMapping>>> vcsMappings = Maps.newHashMap();

    @Override
    public Action<VcsMapping> getVcsMappingRule() {
        return new Action<VcsMapping>() {
            @Override
            public void execute(VcsMapping vcsMapping) {
                VcsMappingInternal vcsMappingInternal = (VcsMappingInternal) vcsMapping;
                Actions.composite(rootVcsMappings).execute(vcsMappingInternal);
                if (!vcsMappingInternal.hasRepository()) {
                    Set<VersionControlSpec> resolutions = Sets.newHashSet();
                    for (Gradle gradle : vcsMappings.keySet()) {
                        Actions.composite(vcsMappings.get(gradle)).execute(vcsMappingInternal);
                        if (vcsMappingInternal.hasRepository()) {
                            resolutions.add(vcsMappingInternal.getRepository());
                        }
                    }
                    if (resolutions.size() > 1) {
                        throw new GradleException("Conflicting external source dependency rules were found in nested builds for " + vcsMappingInternal.getRequested().getDisplayName() + ":\n  " + CollectionUtils.join("\n  ", resolutions));
                    }
                }
            }
        };
    }

    @Override
    public boolean hasRules() {
        return !(vcsMappings.isEmpty() && rootVcsMappings.isEmpty());
    }

    @Override
    public void addRule(Action<VcsMapping> rule, Gradle gradle) {
        if (gradle.getParent() == null) {
            rootVcsMappings.add(rule);
        } else {
            if (!vcsMappings.containsKey(gradle)) {
                vcsMappings.put(gradle, Sets.<Action<VcsMapping>>newLinkedHashSet());
            }
            vcsMappings.get(gradle).add(rule);
        }
    }
}

@@ -787,24 +787,59 @@
 eException;%0A
+import org.gradle.api.Transformer;%0A
 import org.g
@@ -2200,24 +2200,435 @@
 ze() %3E 1) %7B%0A
+                        Set%3CString%3E resolutionDisplayNames = CollectionUtils.collect(resolutions, new Transformer%3CString, VersionControlSpec%3E() %7B%0A                            @Override%0A                            public String transform(VersionControlSpec versionControlSpec) %7B%0A                                return versionControlSpec.getDisplayName();%0A                            %7D%0A                        %7D);%0A
             
@@ -2849,16 +2849,27 @@
 solution
+DisplayName
 s));%0A   

Use resolution display names in conflicting source dependencies error