package sword.langbook3.android.sdb;

import org.junit.Test;

import sword.collections.ImmutableIntArraySet;
import sword.collections.ImmutableIntKeyMap;
import sword.collections.ImmutableIntSet;
import sword.database.MemoryDatabase;
import sword.langbook3.android.db.AgentsManager;
import sword.langbook3.android.db.BunchesManager;
import sword.langbook3.android.db.LangbookDatabaseManager;
import sword.langbook3.android.models.AgentDetails;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static sword.langbook3.android.db.AcceptationsManagerTest.addSimpleAcceptation;
import static sword.langbook3.android.sdb.AcceptationsSerializerTest.cloneBySerializing;
import static sword.langbook3.android.sdb.AcceptationsSerializerTest.findAcceptationsMatchingText;

/**
 * Include all test related to all values that a BunchesSerializer should serialize.
 *
 * Values the the AcceptationsSerializer should serialize are limited to:
 * <li>Bunches</li>
 */
public abstract class AgentsSerializerTest extends BunchesSerializerTest {

    @Override
    abstract AgentsManager createManager(MemoryDatabase db);

    @Test
    public void testSerializeCopyFromSingleSourceToTargetAgentWithoutMatchingAcceptations() {
        final MemoryDatabase inDb = new MemoryDatabase();
        final AgentsManager inManager = createManager(inDb);

        final String languageCode = "es";
        final int inAlphabet = inManager.addLanguage(languageCode).mainAlphabet;

        final int arVerbBunch = inManager.getMaxConcept() + 1;
        final String arVerbBunchText = "verbo de primera conjugación";
        addSimpleAcceptation(inManager, inAlphabet, arVerbBunch, arVerbBunchText);

        final int verbBunch = inManager.getMaxConcept() + 1;
        final String verbBunchText = "verbo";
        addSimpleAcceptation(inManager, inAlphabet, verbBunch, verbBunchText);

        final ImmutableIntSet noBunches = ImmutableIntArraySet.empty();
        final ImmutableIntSet sourceBunches = noBunches.add(arVerbBunch);
        final ImmutableIntKeyMap<String> emptyCorrelation = ImmutableIntKeyMap.empty();
        assertNotNull(inManager.addAgent(verbBunch, sourceBunches, noBunches, emptyCorrelation, emptyCorrelation, emptyCorrelation, emptyCorrelation, 0));

        final MemoryDatabase outDb = cloneBySerializing(inDb);
        final AgentsManager outManager = new LangbookDatabaseManager(outDb);

        final ImmutableIntSet outAgentIds = outManager.getAgentIds();
        assertEquals(1, outAgentIds.size());
        final int outAgentId = outAgentIds.valueAt(0);

        final ImmutableIntSet outArVerbAcceptations = findAcceptationsMatchingText(outDb, arVerbBunchText);
        assertEquals(1, outArVerbAcceptations.size());
        final int outArVerbAcceptation = outArVerbAcceptations.valueAt(0);
        final int outArVerbConcept = outManager.conceptFromAcceptation(outArVerbAcceptation);

        final ImmutableIntSet outVerbAcceptations = findAcceptationsMatchingText(outDb, verbBunchText);
        assertEquals(1, outVerbAcceptations.size());
        final int outVerbAcceptation = outVerbAcceptations.valueAt(0);
        final int outVerbConcept = outManager.conceptFromAcceptation(outVerbAcceptation);
        assertNotEquals(outArVerbConcept, outVerbConcept);

        final AgentDetails outAgentDetails = outManager.getAgentDetails(outAgentId);
        assertEquals(outVerbConcept, outAgentDetails.targetBunch);
        assertEquals(1, outAgentDetails.sourceBunches.size());
        assertEquals(outArVerbConcept, outAgentDetails.sourceBunches.valueAt(0));
        assertTrue(outAgentDetails.diffBunches.isEmpty());
        assertTrue(outAgentDetails.startMatcher.isEmpty());
        assertTrue(outAgentDetails.startAdder.isEmpty());
        assertTrue(outAgentDetails.endMatcher.isEmpty());
        assertTrue(outAgentDetails.endAdder.isEmpty());

        assertTrue(outManager.getAcceptationsInBunch(outArVerbConcept).isEmpty());
        assertTrue(outManager.getAcceptationsInBunch(outVerbConcept).isEmpty());
    }
}

@@ -1234,24 +1234,154 @@
 abase db);%0A%0A
+    private int assertSingleInt(ImmutableIntSet set) %7B%0A        assertEquals(1, set.size());%0A        return set.valueAt(0);%0A    %7D%0A%0A
     @Test%0A  
@@ -1596,50 +1596,8 @@
 );%0A%0A
-        final String languageCode = %22es%22;%0A
     
@@ -1649,20 +1649,12 @@
 age(
-languageCode
+%22es%22
 ).ma
@@ -1665,17 +1665,16 @@
 phabet;%0A
-%0A
         
@@ -1732,79 +1732,8 @@
  1;%0A
-        final String arVerbBunchText = %22verbo de primera conjugaci%C3%B3n%22;%0A
     
@@ -1793,31 +1793,46 @@
 bBunch, 
-arVerbBunchText
+%22verbo de primera conjugaci%C3%B3n%22
 );%0A%0A    
@@ -1892,54 +1892,8 @@
  1;%0A
-        final String verbBunchText = %22verbo%22;%0A
     
@@ -1951,29 +1951,23 @@
 bBunch, 
+%22
 verb
-BunchText
+o%22
 );%0A%0A    
@@ -2503,38 +2503,26 @@
       final 
-ImmutableIntSe
+in
 t outAgentId
@@ -2521,20 +2521,35 @@
 tAgentId
-s
  = 
+assertSingleInt(
 outManag
@@ -2568,107 +2568,8 @@
 ds()
-;%0A        assertEquals(1, outAgentIds.size());%0A        final int outAgentId = outAgentIds.valueAt(0
 );%0A%0A
@@ -2578,38 +2578,26 @@
       final 
-ImmutableIntSe
+in
 t outArVerbA
@@ -2598,36 +2598,51 @@
 rVerbAcceptation
-s
  = 
+assertSingleInt(
 findAcceptations
@@ -2665,153 +2665,39 @@
 Db, 
-arVerbBunchText);%0A        assertEquals(1, outArVerbAcceptations.size());%0A        final int outArVerbAcceptation = outArVerbAcceptations.valueAt(0
+%22verbo de primera conjugaci%C3%B3n%22)
 );%0A 
@@ -2804,30 +2804,18 @@
   final 
-ImmutableIntSe
+in
 t outVer
@@ -2826,20 +2826,35 @@
 eptation
-s
  = 
+assertSingleInt(
 findAcce
@@ -2885,145 +2885,16 @@
 Db, 
+%22
 verb
-BunchText);%0A        assertEquals(1, outVerbAcceptations.size());%0A        final int outVerbAcceptation = outVerbAcceptations.valueAt(0
+o%22)
 );%0A 

Simplify the Agent test