/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.apache.polygene.test.indexing;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.polygene.api.entity.EntityReference;
import org.apache.polygene.api.identity.Identity;
import org.apache.polygene.test.model.Nameable;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

public class NameableAssert
{
    // id -> name
    private static final Map<Identity, String> world = new HashMap<>();

    public static void clear()
    {
        world.clear();
    }

    public static void assertNames( Iterable<EntityReference> identitiesIterable, String... expectedNames )
    {
        assertNames( true, identitiesIterable, expectedNames );
    }

    public static void assertNames( boolean sort,
                                    Iterable<EntityReference> identitiesIterable,
                                    String... expectedNames
    )
    {
        final List<EntityReference> references = StreamSupport.stream( identitiesIterable.spliterator(), false )
                                                              .collect( Collectors.toList() );
        assertThat( expectedNames.length + " entries(" + expectedNames.length + ", got " + getNames( references ) + ")",
                    references.size(),
                    equalTo( expectedNames.length ) );
        List<String> sortedNames = getNames( references );
        final List<String> expectedSorted = java.util.Arrays.asList( expectedNames );
        if( sort )
        {
            Collections.sort( sortedNames );
            Collections.sort( expectedSorted );
        }
        assertThat( "names", sortedNames, equalTo( expectedSorted ) );
    }

    public static void trace( Nameable nameable )
    {
        world.put( nameable.identity().get(), nameable.name().get() );
    }

    public static void assertName( String expectedName, EntityReference reference )
    {
        final String existingName = getName( reference );
        assertThat( "Name of " + reference, existingName, equalTo( expectedName ) );
    }

    public static String getName( EntityReference reference )
    {
        return world.get( reference.identity() );
    }

    public static List<String> getNames( List<EntityReference> references )
    {
        List<String> result = new ArrayList<>( references.size() );
        for( EntityReference reference : references )
        {
            final String name = getName( reference );
            assertThat( "Name of " + reference, name, notNullValue() );
            result.add( name );
        }
        return result;
    }

    public static String[] allNames()
    {
        Collection<String> values = world.values();
        return values.toArray( new String[ world.size() ] );
    }

    public static void verifyUnorderedResults( final Iterable<? extends Nameable> results, final String... names )
    {
        List<String> expected = new ArrayList<>( Arrays.asList( names ) );
        List<String> unexpected = new ArrayList<>();
        for( Nameable result : results )
        {
            String name = result.name().get();
            if( !expected.remove( name ) )
            {
                unexpected.add( name );
            }
        }
        if( !unexpected.isEmpty() || !expected.isEmpty() )
        {
            String message = "";
            if( !unexpected.isEmpty() )
            {
                message += unexpected + " returned but not expected\n";
            }
            if( !expected.isEmpty() )
            {
                message += expected + " expected but not returned\n";
            }
            fail( message.substring( 0, message.length() - 1 ) );
        }
    }

    public static void verifyOrderedResults( final Iterable<? extends Nameable> results, final String... names )
    {
        List<String> expected = new ArrayList<>( Arrays.asList( names ) );
        List<String> actual = new ArrayList<>();
        for( Nameable result : results )
        {
            actual.add( result.name().get() );
        }

        assertThat( "Result is incorrect", actual, equalTo( expected ) );
    }

    private NameableAssert()
    {
    }
}
