/*
 * Copyright 2014 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.runtime.base.library;

import org.gradle.api.Action;
import org.gradle.api.DomainObjectSet;
import org.gradle.api.Incubating;
import org.gradle.api.internal.DefaultDomainObjectSet;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.internal.reflect.ObjectInstantiationException;
import org.gradle.language.base.FunctionalSourceSet;
import org.gradle.language.base.LanguageOutputType;
import org.gradle.language.base.LanguageSourceSet;
import org.gradle.language.base.internal.LanguageSourceSetContainer;
import org.gradle.runtime.base.BinarySpec;
import org.gradle.runtime.base.ComponentSpecIdentifier;
import org.gradle.runtime.base.LibrarySpec;
import org.gradle.runtime.base.ModelInstantiationException;
import org.gradle.runtime.base.internal.ComponentSpecInternal;

import java.util.HashSet;
import java.util.Set;

/**
 * Base class for custom library implementations.
 * A custom implementation of {@link LibrarySpec} must extend this type.
 */
@Incubating
public class DefaultLibrarySpec implements LibrarySpec, ComponentSpecInternal {
    private static ThreadLocal<ComponentInfo> nextComponentInfo = new ThreadLocal<ComponentInfo>();
    private final LanguageSourceSetContainer sourceSets = new LanguageSourceSetContainer();

    private final ComponentSpecIdentifier identifier;
    private final String typeName;
    private final DomainObjectSet<BinarySpec> binaries = new DefaultDomainObjectSet<BinarySpec>(BinarySpec.class);
    private final FunctionalSourceSet mainSourceSet;

    public static <T extends DefaultLibrarySpec> T create(Class<T> type, ComponentSpecIdentifier identifier, FunctionalSourceSet mainSourceSet, Instantiator instantiator) {
        nextComponentInfo.set(new ComponentInfo(identifier, type.getSimpleName(), mainSourceSet));
        try {
            try {
                return instantiator.newInstance(type);
            } catch (ObjectInstantiationException e) {
                throw new ModelInstantiationException(String.format("Could not create library of type %s", type.getSimpleName()), e.getCause());
            }
        } finally {
            nextComponentInfo.set(null);
        }
    }

    public DefaultLibrarySpec() {
        this(nextComponentInfo.get());
    }

    private DefaultLibrarySpec(ComponentInfo info) {
        this.identifier = info.componentIdentifier;
        this.typeName = info.typeName;
        this.mainSourceSet = info.sourceSets;
        this.mainSourceSet.all(new Action<LanguageSourceSet>() {
            public void execute(LanguageSourceSet languageSourceSet) {
                sourceSets.source(languageSourceSet);
            }
        });
    }

    public String getName() {
        return identifier.getName();
    }

    public String getProjectPath() {
        return identifier.getProjectPath();
    }

    public String getDisplayName() {
        return String.format("%s '%s'", typeName, getName());
    }

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

    public DomainObjectSet<LanguageSourceSet> getSource() {
        return sourceSets;
    }

    public void source(Object sources) {
        sourceSets.source(sources);
    }

    public DomainObjectSet<BinarySpec> getBinaries() {
        return binaries;
    }

    public FunctionalSourceSet getMainSource() {
        return mainSourceSet;
    }

    //TODO handle input types for custom libraries
    public Set<Class<? extends LanguageOutputType>> getInputTypes() {
        return new HashSet<Class<? extends LanguageOutputType>>();
    }

    private static class ComponentInfo {
        final ComponentSpecIdentifier componentIdentifier;
        final String typeName;
        final FunctionalSourceSet sourceSets;

        private ComponentInfo(ComponentSpecIdentifier componentIdentifier,
                              String typeName,
                              FunctionalSourceSet sourceSets) {
            this.componentIdentifier = componentIdentifier;
            this.typeName = typeName;
            this.sourceSets = sourceSets;
        }
    }
}
@@ -3997,52 +3997,165 @@
   //
-TODO handle input types for custom libraries
+ To declare a custom spec we currently need to override this method in the Library.%0A    // implementation. We need a more generic way for this in the future.
 %0A   

Add comment to potential open issue when declaring custom component.