package com.averi.worldscribe.utilities;

import android.content.Context;

import com.averi.worldscribe.Category;
import com.averi.worldscribe.Connection;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by mark on 14/06/16.
 */
public class ExternalReader {

    public static final int TEXT_FILE_EXTENSION_LENGTH = 4;

    public static ArrayList<String> getWorldList() {
        ArrayList<String> worldNames = new ArrayList<>();
        File worldsFolder = FileRetriever.getAppDirectory();
        File[] listOfFiles = worldsFolder.listFiles();

        for (File file : listOfFiles) {
            if (file.isDirectory()) {
                worldNames.add(file.getName());
            }
        }

        Collections.sort(worldNames, String.CASE_INSENSITIVE_ORDER);

        return worldNames;
    }

    public static boolean worldListIsEmpty() {
        ArrayList<String> worldList = getWorldList();
        return worldList.isEmpty();
    }

    public static boolean worldAlreadyExists(String worldName) {
        File worldsFolder = FileRetriever.getAppDirectory();
        return (new File(worldsFolder, worldName).exists());
    }

    public static ArrayList<String> getArticleNamesInCategory(Context context, String worldName,
                                                              Category category) {
        ArrayList<String> articleNames = new ArrayList<String>();
        File categoryFolder = FileRetriever.getCategoryDirectory(context, worldName, category);
        File[] listOfArticles = categoryFolder.listFiles();

        for (File articleFolder : listOfArticles) {
            if (articleFolder.isDirectory()) {
                articleNames.add(articleFolder.getName());
            }
        }

        Collections.sort(articleNames, String.CASE_INSENSITIVE_ORDER);

        return articleNames;
    }

    public static ArrayList<Connection> getConnections(Context context, String worldName,
                                                       Category category, String articleName) {
        ArrayList<Connection> connections = new ArrayList<>();

        for (Category connectionCategory : Category.values()) {
            connections.addAll(getConnectionsInCategory(context, worldName, category, articleName,
                    connectionCategory));
        }

        return connections;
    }

    private static ArrayList<Connection> getConnectionsInCategory(Context context, String worldName,
            Category category, String articleName, Category connectionCategory) {
        ArrayList<Connection> connections = new ArrayList<>();
        File connectionCategoryFolder = FileRetriever.getConnectionCategoryDirectory(context,
                worldName, category, articleName, connectionCategory);

        for (File connectionFile : connectionCategoryFolder.listFiles()) {
            Connection connection = makeConnectionFromFile(connectionCategory, connectionFile);
            if (connection != null) {
                connections.add(connection);
            }
        }

        return connections;
    }

    private static Connection makeConnectionFromFile(Category connectionCategory,
                                                     File connectionFile) {
        Connection connection;

        try {
            FileInputStream inputStream = new FileInputStream(connectionFile);
            connection = new Connection();

            String line;
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = reader.readLine()) != null) {
                connection.articleRole = line;
            }

            connection.connectedArticleCategory = connectionCategory;

            String connectionFileName = connectionFile.getName();
            connection.connectedArticleName = connectionFileName.substring(0,
                    connectionFileName.length() - TEXT_FILE_EXTENSION_LENGTH);

            inputStream.close();
        }
        catch (java.io.IOException e) {
            connection = null;
        }

        return connection;
    }

}

@@ -397,16 +397,83 @@
 ader %7B%0A%0A
+    public static final String TEXT_FIELD_FILE_EXTENSION = %22.txt%22;%0A
     publ
@@ -2032,32 +2032,1461 @@
 leNames;%0A    %7D%0A%0A
+    /**%0A     * Given the name of a text field in the current Article, return the String data entered for%0A     * that field.%0A     * @param context The Context calling this method.%0A     * @param worldName The name of the current World.%0A     * @param category The %7B@link Category%7D of the current Article.%0A     * @param articleName The name of the current Article.%0A     * @param textFieldName The name of the text field from the current Article.%0A     * @return The data stored in the text field for this Article, if a file for that field exists%0A     * and it can be read; empty String otherwise.%0A     */%0A    public static String getArticleTextFieldData(Context context, String worldName,%0A            Category category, String articleName, String textFieldName) %7B%0A        String textFieldData;%0A        File textFieldFile = FileRetriever.getArticleFile(context, worldName, category, articleName,%0A                textFieldName + TEXT_FIELD_FILE_EXTENSION);%0A%0A        if (textFieldFile.exists()) %7B%0A            try %7B%0A                FileInputStream inputStream = new FileInputStream(textFieldFile);%0A                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));%0A                textFieldData = reader.readLine();%0A            %7D catch (java.io.IOException e) %7B%0A                textFieldData = %22%22;%0A            %7D%0A        %7D else %7B%0A            textFieldData = %22%22;%0A        %7D%0A%0A        return textFieldData;%0A    %7D%0A%0A
     public stati

Add getArticleTextFieldData to ExternalReader