/*
 * Copyright 2015 Michael Rozumyanskiy
 *
 * 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 com.michaelrocks.lightsaber.internal;

import com.michaelrocks.lightsaber.ConfigurationException;
import com.michaelrocks.lightsaber.Injector;
import com.michaelrocks.lightsaber.InstanceProvider;

import javax.inject.Provider;
import java.util.IdentityHashMap;
import java.util.Map;

public class LightsaberInjector implements Injector {
    private final Injector parentInjector;
    private final Map<Class<?>, Provider<?>> providers = new IdentityHashMap<Class<?>, Provider<?>>();

    public LightsaberInjector() {
        this(null);
    }

    public LightsaberInjector(final Injector parentInjector) {
        this.parentInjector = parentInjector;
        registerProvider(Injector.class, new InstanceProvider<Injector>(this));
    }

    @Override
    public void injectMembers(final Object target) {
        throw new IllegalStateException(
                "This method must not be called. It must be substituted with a valid injector instead.");
    }

    @Override
    public <T> T getInstance(final Class<T> type) {
        return getProvider(type).get();
    }

    @Override
    public <T> Provider<T> getProvider(final Class<T> type) {
        // noinspection unchecked
        final Provider<T> provider = (Provider<T>) providers.get(type);
        if (provider == null) {
            if (parentInjector == null) {
                throw new ConfigurationException("Provider for " + type + " not found");
            } else {
                return parentInjector.getProvider(type);
            }
        }
        return provider;
    }

    @Override
    public Map<Class<?>, Provider<?>> getAllProviders() {
        return new IdentityHashMap<Class<?>, Provider<?>>(providers);
    }

    public <T> void registerProvider(final Class<T> type, final Provider<T> provider) {
        final Provider<?> oldProvider = providers.put(type, provider);
        if (oldProvider != null) {
            throw new ConfigurationException("Provider for " + type + " already registered");
        }
    }
}

@@ -704,16 +704,69 @@
 eption;%0A
+import com.michaelrocks.lightsaber.CopyableProvider;%0A
 import c
@@ -1394,16 +1394,605 @@
 this));%0A
+        if (parentInjector != null) %7B%0A            copyParentProviders();%0A        %7D%0A    %7D%0A%0A    private void copyParentProviders() %7B%0A        for (final Map.Entry%3CClass%3C?%3E, Provider%3C?%3E%3E entry : parentInjector.getAllProviders().entrySet()) %7B%0A            if (entry.getKey() != Injector.class && entry.getValue() instanceof CopyableProvider) %7B%0A                // noinspection unchecked%0A                registerProvider(%0A                        (Class%3CObject%3E) entry.getKey(),%0A                        ((CopyableProvider%3CObject%3E) entry.getValue()).copyWithInjector(this));%0A            %7D%0A        %7D%0A
     %7D%0A%0A 

Copy providers from parent injector to child