/*
 * Copyright 2015 Lukas Tenbrink
 *
 *    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 ivorius.ivtoolkit.maze.components;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by lukas on 15.04.15.
 */
public class MazeComponents
{
    public static <M extends MazeComponent<C>, C> Function<M, ShiftedMazeComponent<M, C>> shiftFunction(final MazeRoom shift)
    {
        return new Function<M, ShiftedMazeComponent<M, C>>()
        {
            @Nullable
            @Override
            public ShiftedMazeComponent<M, C> apply(@Nullable M input)
            {
                return shift(input, shift);
            }
        };
    }

    public static <M extends MazeComponent<C>, C> Function<M, Iterable<ShiftedMazeComponent<M, C>>> shiftAllFunction(final MazeRoomConnection connection, final C connector, final ConnectionStrategy<C> strategy)
    {
        return new Function<M, Iterable<ShiftedMazeComponent<M, C>>>()
        {
            @Nullable
            @Override
            public Iterable<ShiftedMazeComponent<M, C>> apply(@Nullable M input)
            {
                Set<ShiftedMazeComponent<M, C>> set = new HashSet<>();

                if (input != null)
                {
                    for (Map.Entry<MazeRoomConnection, C> entry : input.exits().entrySet())
                    {
                        MazeRoom dist = entry.getKey().distance(connection);
                        if (dist != null) set.add(shift(input, dist));
                    }
                }

                return set;
            }
        };
    }

    public static <M extends MazeComponent<C>, C> ShiftedMazeComponent<M, C> shift(final M component, final MazeRoom shift)
    {
        return new ShiftedMazeComponent<>(component, shift, FluentIterable.from(component.rooms()).transform(new Function<MazeRoom, MazeRoom>()
        {
            @Nullable
            @Override
            public MazeRoom apply(MazeRoom input)
            {
                return input != null ? input.add(shift) : null;
            }
        }).toSet(), FluentIterable.from(component.exits().keySet()).transform(new Function<MazeRoomConnection, MazeRoomConnection>()
        {
            @Nullable
            @Override
            public MazeRoomConnection apply(@Nullable MazeRoomConnection input)
            {
                return input != null ? input.add(shift) : null;
            }
        }).toMap(new Function<MazeRoomConnection, C>()
        {
            @Nullable
            @Override
            public C apply(@Nullable MazeRoomConnection input)
            {
                return component.exits().get(input != null ? input.sub(shift) : null);
            }
        }));
    }

    public static <M extends MazeComponent<C>, C> Predicate<ShiftedMazeComponent<M, C>> compatibilityPredicate(final MazeComponent<C> component, final ConnectionStrategy<C> strategy)
    {
        return new Predicate<ShiftedMazeComponent<M, C>>()
        {
            @Override
            public boolean apply(@Nullable ShiftedMazeComponent<M, C> input)
            {
                return componentsCompatible(component, input, strategy);
            }
        };
    }

    public static <C> boolean componentsCompatible(final MazeComponent<C> left, final MazeComponent<C> right, final ConnectionStrategy<C> strategy)
    {
        return !overlap(left, right) && allExitsCompatible(left, right, strategy);
    }

    public static boolean overlap(MazeComponent<?> left, MazeComponent<?> right)
    {
        return Sets.intersection(left.rooms(), right.rooms()).size() > 0;
    }

    public static <C> boolean allExitsCompatible(final MazeComponent<C> left, final MazeComponent<C> right, final ConnectionStrategy<C> strategy)
    {
        return Iterables.all(Sets.union(left.exits().keySet(), right.exits().keySet()), new Predicate<MazeRoomConnection>()
        {
            @Override
            public boolean apply(@Nullable MazeRoomConnection input)
            {
                return input == null || strategy.connect(input, left.exits().get(input), right.exits().get(input));
            }
        });
    }
}

@@ -4093,36 +4093,40 @@
 azeComponent%3CC%3E 
-left
+existing
 , final MazeComp
@@ -4126,37 +4126,35 @@
 azeComponent%3CC%3E 
-right
+add
 , final Connecti
@@ -4207,27 +4207,29 @@
 overlap(
-left, right
+existing, add
 ) && all
@@ -4248,19 +4248,21 @@
 ble(
-left, right
+existing, add
 , st
@@ -4517,20 +4517,24 @@
 nent%3CC%3E 
-left
+existing
 , final 
@@ -4550,21 +4550,19 @@
 nent%3CC%3E 
-right
+add
 , final 
@@ -4632,65 +4632,30 @@
 all(
-Sets.union(left.exits().keySet(), right.exits().ke
+add.exits().entr
 ySet()
-)
 , ne
@@ -4666,16 +4666,26 @@
 edicate%3C
+Map.Entry%3C
 MazeRoom
@@ -4686,32 +4686,36 @@
 zeRoomConnection
+, C%3E
 %3E()%0A        %7B%0A  
@@ -4769,32 +4769,42 @@
 apply(@Nullable 
+Map.Entry%3C
 MazeRoomConnecti
@@ -4797,32 +4797,36 @@
 zeRoomConnection
+, C%3E
  input)%0A        
@@ -4897,58 +4897,72 @@
 nput
-, left.exits().get(input), right.exits().get(input
+.getKey(), existing.exits().get(input.getKey()), input.getValue(
 ));%0A

Make adding a component only check the new component's exits.