package com.matthias.android.amginori;

import android.content.Context;

import com.matthias.android.amginori.persistence.Anki2DbHelper;
import com.matthias.android.amginori.persistence.Anki2DbSchema.NotesTable;

import java.util.ArrayList;
import java.util.List;

public final class CardLibrary {

    private static CardLibrary sCardLibrary;

    private List<Card> mCards = new ArrayList<>();

    private int mSize = 0;

    private Context mContext;

    private CardLibrary(Context context) {
        mContext = context.getApplicationContext();
    }

    public static CardLibrary get(Context context) {
        if (sCardLibrary == null) {
            sCardLibrary = new CardLibrary(context);
        }
        return sCardLibrary;
    }

    public ArrayList<Card> getRandomCards(int size) {
        ArrayList<Card> result = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            Card card = mCards.get((int) (mCards.size() * Math.random()));
            result.add(card);
            result.add(card.reversedCopy());
        }
        return result;
    }

    public void refresh() {
        Anki2DbHelper database = new Anki2DbHelper(mContext);
        mCards = new ArrayList<>();
        List<String> cards = database.getAllCards();
        if (cards.isEmpty()) {
            mCards.add(new Card("NO CARDS", "AVAILABLE"));
            mSize = 0;
        } else {
            for (String card : cards) {
                String[] fields = card.split(NotesTable.FIELD_SEPARATOR, -1);
                mCards.add(new Card(fields[0].trim(), fields[1].trim()));
            }
            mSize = mCards.size();
        }
    }

    public int size() {
        return mSize;
    }
}

@@ -209,31 +209,215 @@
 le;%0A
-%0Aimport java.util.Array
+import com.matthias.android.amginori.persistence.SharedPreferencesHelper;%0Aimport com.matthias.android.amginori.utils.Base64;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.Linked
 List
@@ -476,16 +476,67 @@
 rary %7B%0A%0A
+    private static final int CARD_POOL_SIZE = 12;%0A%0A
     priv
@@ -623,16 +623,77 @@
 ist%3C%3E();
+%0A    private LinkedList%3CCard%3E mCardPool = new LinkedList%3C%3E();
 %0A%0A    pr
@@ -1027,32 +1027,688 @@
 Library;%0A    %7D%0A%0A
+    public Card nextCard(List%3CCard%3E cards) %7B%0A        if (mCardPool.isEmpty()) %7B%0A            Card%5B%5D newCards = new Card%5BCARD_POOL_SIZE%5D;%0A            // Insert cards in this order: %5Bc_1, r_n, c_2, r_n-1, ... c_n-1, r_2, c_n, r_1%5D%0A            for (int i = 0; i %3C CARD_POOL_SIZE / 2; i++) %7B%0A                newCards%5Bi * 2%5D = cards.get((int) (cards.size() * Math.random())).copy();%0A            %7D%0A            for (int i = 0; i %3C CARD_POOL_SIZE / 2; i++) %7B%0A                newCards%5BCARD_POOL_SIZE - 1 - (i * 2)%5D = newCards%5Bi * 2%5D.reversedCopy();%0A            %7D%0A            Collections.addAll(mCardPool, newCards);%0A        %7D%0A        return mCardPool.poll();%0A    %7D%0A%0A
     public Array
@@ -2591,32 +2591,405 @@
        %7D%0A    %7D%0A%0A
+    public void persist() %7B%0A        SharedPreferencesHelper.get(mContext).putString(%22CardPool%22, Base64.encodeObject(mCardPool));%0A    %7D%0A%0A    public void restore() %7B%0A        String cardPool = SharedPreferencesHelper.get(mContext).getString(%22CardPool%22, %22%22);%0A        mCardPool = Base64.decodeString(cardPool);%0A    %7D%0A%0A    public void reset() %7B%0A        mCardPool.clear();%0A    %7D%0A%0A
     public int s

Add method for better card distribution