/*
 * Copyright 2015 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.model.internal.registry;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import org.gradle.model.internal.core.ModelNode;
import org.gradle.model.internal.core.ModelPath;
import org.gradle.model.internal.type.ModelType;

import java.util.*;

class RuleBindings {
    private final NodeAtStateIndex rulesBySubject;
    private final NodeAtStateIndex rulesByInput;
    private final PathPredicateIndex untypedPathReferences = new PathPredicateIndex();
    private final PathPredicateIndex typedPathReferences = new PathPredicateIndex();
    private final TypePredicateIndex scopeReferences = new TypePredicateIndex();

    public RuleBindings() {
        rulesBySubject = new NodeAtStateIndex("rulesBySubject");
        rulesByInput = new NodeAtStateIndex("rulesByInput");
    }

    public void nodeCreated(ModelNodeInternal node) {
        untypedPathReferences.addNode(node);
    }

    public void nodeDiscovered(ModelNodeInternal node) {
        typedPathReferences.addNode(node);
        scopeReferences.addNodeToScope(node.getPath(), node);
        scopeReferences.addNodeToScope(node.getPath().getParent(), node);
    }

    private void bound(Reference reference, ModelNodeInternal node) {
        ModelBinding binding = reference.binding;
        binding.onBind(node);
        reference.index.put(new NodeAtState(node.getPath(), binding.predicate.getState()), reference.owner);
    }

    public void remove(ModelNodeInternal node) {
        untypedPathReferences.removeNode(node);
        typedPathReferences.removeNode(node);
        scopeReferences.removeNodeFromScope(node.getPath(), node);
        scopeReferences.removeNodeFromScope(node.getPath().getParent(), node);
        rulesBySubject.nodeRemoved(node);
        rulesByInput.nodeRemoved(node);
    }

    public void add(RuleBinder ruleBinder) {
        addRule(ruleBinder, rulesBySubject, ruleBinder.getSubjectBinding());
        for (ModelBinding binding : ruleBinder.getInputBindings()) {
            addRule(ruleBinder, rulesByInput, binding);
        }
    }

    private void addRule(RuleBinder rule, NodeAtStateIndex index, ModelBinding binding) {
        Reference reference = new Reference(rule, index, binding);
        BindingPredicate predicate = binding.getPredicate();
        if (predicate.getPath() != null) {
            if (predicate.getScope() != null) {
                throw new UnsupportedOperationException("Currently not implemented");
            }
            if (reference.binding.canBindInState(ModelNode.State.Registered)) {
                untypedPathReferences.addReference(reference);
            } else {
                typedPathReferences.addReference(reference);
            }
        } else if (predicate.getScope() != null) {
            scopeReferences.addReference(reference);
        } else {
            throw new UnsupportedOperationException("Currently not implemented");
        }
    }

    private static void unbind(RuleBinder rule, ModelNodeInternal node) {
        rule.getSubjectBinding().onUnbind(node);
        for (ModelBinding binding : rule.getInputBindings()) {
            binding.onUnbind(node);
        }
    }

    /**
     * Returns the set of rules with the given target as their subject.
     */
    public Collection<RuleBinder> getRulesWithSubject(NodeAtState target) {
        return rulesBySubject.get(target);
    }

    /**
     * Returns the set of rules with the given input.
     */
    public Collection<RuleBinder> getRulesWithInput(NodeAtState input) {
        return rulesByInput.get(input);
    }

    private static class Reference {
        final ModelBinding binding;
        final NodeAtStateIndex index;
        final RuleBinder owner;

        public Reference(RuleBinder owner, NodeAtStateIndex index, ModelBinding binding) {
            this.owner = owner;
            this.index = index;
            this.binding = binding;
        }

        @Override
        public String toString() {
            return binding + " in " + index.name;
        }
    }

    private class PredicateMatches {
        final List<Reference> references = new ArrayList<Reference>();
        ModelNodeInternal match;

        void match(ModelNodeInternal node) {
            for (Reference reference : references) {
                bound(reference, node);
            }
            match = node;
        }

        void add(Reference reference) {
            references.add(reference);
            if (match != null) {
                bound(reference, match);
            }
        }

        public void remove(ModelNodeInternal node) {
            match = null;
        }
    }

    private class PathPredicateIndex {
        final Map<ModelPath, PredicateMatches> predicates = Maps.newLinkedHashMap();

        public void addNode(ModelNodeInternal node) {
            predicatesForPath(node.getPath()).match(node);
        }

        public void addReference(Reference reference) {
            ModelPath path = reference.binding.getPredicate().getPath();
            predicatesForPath(path).add(reference);
        }

        private PredicateMatches predicatesForPath(ModelPath path) {
            PredicateMatches predicatesForReference = predicates.get(path);
            if (predicatesForReference == null) {
                predicatesForReference = new PredicateMatches();
                predicates.put(path, predicatesForReference);
            }
            return predicatesForReference;
        }

        public void removeNode(ModelNodeInternal node) {
            predicatesForPath(node.getPath()).remove(node);
        }
    }

    private class ScopeIndex {
        final Map<ModelType<?>, PredicateMatches> types = Maps.newLinkedHashMap();
        final List<ModelNodeInternal> nodes = Lists.newArrayList();

        public void addNode(ModelNodeInternal node) {
            nodes.add(node);
            for (Map.Entry<ModelType<?>, PredicateMatches> entry : types.entrySet()) {
                if (node.canBeViewedAs(entry.getKey())) {
                    entry.getValue().match(node);
                }
            }
        }

        public void removeNode(ModelNodeInternal node) {
            nodes.remove(node);
            for (PredicateMatches matches : types.values()) {
                if (matches.match == node) {
                    matches.remove(node);
                }
            }
        }

        public void addReference(Reference reference) {
            ModelType<?> type = reference.binding.getPredicate().getType();
            PredicateMatches predicateMatches = types.get(type);
            boolean newType = predicateMatches == null;
            if (predicateMatches == null) {
                predicateMatches = new PredicateMatches();
                types.put(type, predicateMatches);
            }
            predicateMatches.add(reference);
            if (newType) {
                for (ModelNodeInternal node : nodes) {
                    if (node.canBeViewedAs(type)) {
                        predicateMatches.match(node);
                    }
                }
            }
        }
    }

    private class TypePredicateIndex {
        final Map<ModelPath, ScopeIndex> scopes = Maps.newLinkedHashMap();

        public void addNodeToScope(ModelPath path, ModelNodeInternal node) {
            scopeForPath(path).addNode(node);
        }

        public void removeNodeFromScope(ModelPath path, ModelNodeInternal node) {
            scopeForPath(path).removeNode(node);
        }

        public void addReference(Reference reference) {
            ModelPath path = reference.binding.getPredicate().getScope();
            scopeForPath(path).addReference(reference);
        }

        private ScopeIndex scopeForPath(ModelPath path) {
            ScopeIndex scope = scopes.get(path);
            if (scope == null) {
                scope = new ScopeIndex();
                scopes.put(path, scope);
            }
            return scope;
        }
    }

    private static class NodeAtStateIndex {
        private final Multimap<NodeAtState, RuleBinder> boundAtState = LinkedHashMultimap.create();

        private final String name;

        private NodeAtStateIndex(String name) {
            this.name = name;
        }

        public void nodeRemoved(ModelNodeInternal node) {
            // This could be more efficient; assume that removal happens much less often than addition
            for (ModelNode.State state : ModelNode.State.values()) {
                for (RuleBinder rule : boundAtState.removeAll(new NodeAtState(node.getPath(), state))) {
                    unbind(rule, node);
                }
            }
        }

        public void put(NodeAtState nodeAtState, RuleBinder binder) {
            boundAtState.put(nodeAtState, binder);
        }

        /**
         * Returns rules for given target at state.
         */
        public Collection<RuleBinder> get(NodeAtState nodeAtState) {
            Collection<RuleBinder> result = boundAtState.get(nodeAtState);
            return result == null ? Collections.<RuleBinder>emptyList() : result;
        }

        public void remove(ModelNodeInternal node, RuleBinder ruleBinder) {
            unbind(ruleBinder, node);
            boundAtState.values().remove(ruleBinder);
        }

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