package org.mercycorps.translationcards.service;

import org.mercycorps.translationcards.data.Repository;
import org.mercycorps.translationcards.data.Translation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TranslationService {

    private Repository repository;
    private DictionaryService dictionaryService;
    private List<Translation> currentTranslations;
    private boolean displayCardsWithNoAudio;
    private List<Boolean> expanded;

    public TranslationService(Repository repository, DictionaryService dictionaryService) {
        this.repository = repository;
        this.dictionaryService = dictionaryService;
        currentTranslations = repository.getTranslationsForDictionary(dictionaryService.currentDictionary());
        expanded = new ArrayList<>(Arrays.asList(new Boolean[currentTranslations.size()]));
        Collections.fill(expanded, Boolean.FALSE);
        displayCardsWithNoAudio = true;
    }

    public void initializeCardStates() {
        expanded = new ArrayList<>(
                Arrays.asList(
                        new Boolean[getCurrentTranslations().size()]
                )
        );
        Collections.fill(expanded, Boolean.FALSE);
    }

    public List<Translation> getCurrentTranslations() {
        List<Translation> translations = repository.getTranslationsForDictionary(dictionaryService.currentDictionary());

        return filterTranslationsWithNoAudio(translations);
    }

    private List<Translation> filterTranslationsWithNoAudio(List<Translation> translations) {
        List<Translation> filteredTranslations = new ArrayList<>(translations.size());
        for(int index = 0; index < translations.size(); index++) {
            if(displayCardsWithNoAudio || translations.get(index).isAudioFilePresent()) {
                filteredTranslations.add(translations.get(index));
            }
        }
        return filteredTranslations;
    }

    public void deleteTranslation(String sourcePhrase) {
        repository.deleteTranslationBySourcePhrase(sourcePhrase, dictionaryService.getDictionariesForCurrentDeck());
    }

    public void setDisplayCardsWithNoAudio(boolean displayCardsWithNoAudio) {
        this.displayCardsWithNoAudio = displayCardsWithNoAudio;
    }

    public void expandCard(int position) {
        expanded.set(position, true);
    }

    public void minimizeCard(int position) {
        expanded.set(position, false);
    }

    public boolean cardIsExpanded(int position) {
        return expanded.size() > 0 && expanded.get(position);
    }
}

@@ -387,59 +387,8 @@
 ce;%0A
-    private List%3CTranslation%3E currentTranslations;%0A
     
@@ -424,24 +424,24 @@
 ithNoAudio;%0A
+
     private 
@@ -651,261 +651,8 @@
 ce;%0A
-        currentTranslations = repository.getTranslationsForDictionary(dictionaryService.currentDictionary());%0A        expanded = new ArrayList%3C%3E(Arrays.asList(new Boolean%5BcurrentTranslations.size()%5D));%0A        Collections.fill(expanded, Boolean.FALSE);%0A
     
@@ -687,16 +687,48 @@
 = true;%0A
+        initializeCardStates();%0A
     %7D%0A%0A 

Remove unnecessary field