/**
 * Copyright (C) 2013 Max Planck Institute for Psycholinguistics
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package nl.mpi.arbil.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import nl.mpi.arbil.userstorage.SessionStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Peter Withers
 * @author Twan Goosen <twan.goosen@mpi.nl>
 */
public abstract class DocumentationLanguages {

    private final static Logger logger = LoggerFactory.getLogger(DocumentationLanguages.class);
    private final String selectedLanguagesKey;
    private final SessionStorage sessionStorage;
    private final static String ALL_LANGUAGES = "_ALL_LANGUAGES_";

    public DocumentationLanguages(String selectedLanguagesKey, SessionStorage sessionStorage) {
	this.selectedLanguagesKey = selectedLanguagesKey;
	this.sessionStorage = sessionStorage;
    }

    private List<String> getDefaultLanguages() {
	final List<ArbilVocabularyItem> imdiLanguages = getAllLanguages();
	List<String> selectedLanguages = new ArrayList<String>(imdiLanguages.size());
	for (ArbilVocabularyItem currentTemplate : imdiLanguages) {
	    selectedLanguages.add(currentTemplate.itemDisplayName);
	}
	return selectedLanguages;
    }

    public abstract List<ArbilVocabularyItem> getAllLanguages();

    protected synchronized List<ArbilVocabularyItem> getLanguageListSubset(List<ArbilVocabularyItem> allLanguages) {
	final List<ArbilVocabularyItem> languageListSubset = new ArrayList<ArbilVocabularyItem>();
	if (allLanguages != null) {
	    final List<String> selectedLanguages = getSelectedLanguagesOrDefault();
	    for (ArbilVocabularyItem currentVocabItem : allLanguages) {
		if (selectedLanguages.contains(currentVocabItem.itemDisplayName)) {
		    languageListSubset.add(currentVocabItem);
		}
	    }
	}
	return languageListSubset;
    }

    /**
     *
     * @return Sorted selection of documentation languages
     */
    public abstract List<ArbilVocabularyItem> getSortedLanguageListSubset();

    /**
     *
     * @return a newly instantiated <em>mutable</em> list of the stored selected languages, null if nothing was stored
     */
    private List<String> getSelectedLanguages() {
	try {
	    final String[] storedLanguages = sessionStorage.loadStringArray(selectedLanguagesKey);
	    if (storedLanguages != null) {
		return new ArrayList<String>(Arrays.asList(storedLanguages));
	    } else {
		logger.info("No selectedLanguages file, will create one now.");
	    }
	} catch (Exception e) {
	    // Most likely an IOException
	    logger.error("Error while trying to read language file, will try to create new one.", e);
	}
	return null;
    }

    public synchronized List<String> getSelectedLanguagesOrDefault() {
	final List<String> storedLanguages = getSelectedLanguages();
	if (storedLanguages != null) {
	    return storedLanguages;
	} else {
	    // save default languages
	    saveSelectedLanguages(getDefaultLanguages());
	    return getSelectedLanguages();
	}
    }

    public synchronized void addselectedLanguage(String templateString) {
	final List<String> selectedLanguages = getSelectedLanguages();
	if (selectedLanguages == null) {
	    saveSelectedLanguages(Collections.singletonList(templateString));
	} else {
	    selectedLanguages.add(templateString);
	    saveSelectedLanguages(selectedLanguages);
	}
    }

    public synchronized void removeselectedLanguages(String templateString) {
	final List<String> selectedLanguages = getSelectedLanguages();
	while (selectedLanguages.contains(templateString)) {
	    selectedLanguages.remove(templateString);
	}
	saveSelectedLanguages(selectedLanguages);
    }

    protected void saveSelectedLanguages(List<String> selectedLanguages) {
	try {
	    sessionStorage.saveStringArray(selectedLanguagesKey, selectedLanguages.toArray(new String[]{}));
	} catch (IOException ex) {
	    logger.error("Could not store language selection", ex);
	}
    }
}

@@ -3086,16 +3086,183 @@
 null) %7B%0A
+%09%09if (storedLanguages.length == 1 && storedLanguages%5B0%5D.equals(ALL_LANGUAGES)) %7B%0A%09%09    // wildcard for all available languages%0A%09%09    return getDefaultLanguages();%0A%09%09%7D%0A
 %09%09return
@@ -3313,24 +3313,24 @@
 anguages));%0A
-
 %09    %7D else 
@@ -3796,118 +3796,36 @@
     
-// save default languages%0A%09    saveSelectedLanguages(getDefaultLanguages());%0A%09    return getSelectedLanguages(
+return(getDefaultLanguages()
 );%0A%09
@@ -4321,24 +4321,33 @@
 tedLanguages
+OrDefault
 ();%0A%09while (
@@ -4579,16 +4579,215 @@
  %7B%0A%09    
+if (selectedLanguages.containsAll(getDefaultLanguages())) %7B%0A%09%09// Store wildcard for all languages%0A%09%09sessionStorage.saveStringArray(selectedLanguagesKey, new String%5B%5D%7BALL_LANGUAGES%7D);%0A%09    %7D else %7B%0A%09%09
 sessionS
@@ -4875,16 +4875,23 @@
 %5B%5D%7B%7D));%0A
+%09    %7D%0A
 %09%7D catch

Store wildcard if ALL language have been selected. Fixes #2317