package com.averi.worldscribe.utilities;

import android.content.Context;
import android.os.Environment;

import com.averi.worldscribe.Category;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOError;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by mark on 08/06/16.
 */
public final class ExternalWriter {
//    public static boolean createAppDirectory() {
//        boolean directoryExists = true;
//
//        File appDirectory = new File(Environment.getExternalStorageDirectory(), APP_DIRECTORY_NAME);
//        Log.d("WorldScribe", appDirectory.getAbsolutePath());
//
//        if (externalStorageIsWritable()) {
//            if (!(appDirectory.exists())) {
//                directoryExists = appDirectory.mkdirs();
//            }
//        }
//
//        return directoryExists;
//    }

    private static boolean externalStorageIsWritable() {
        String state = Environment.getExternalStorageState();
        return (Environment.MEDIA_MOUNTED.equals(state));
    }

    public static boolean createWorldDirectory(Context context, String worldName) {
        boolean directoryWasCreated = true;

        File worldDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/" + FileRetriever.APP_DIRECTORY_NAME + "/", worldName);

        directoryWasCreated = worldDirectory.mkdirs();

        if (directoryWasCreated) {
            directoryWasCreated = createArticleTypeDirectories(context, worldDirectory);

            // If one or more subfolders couldn't be created, delete the World folder so that
            // subsequent attempts can start building the World folder again from scratch.
            if (!(directoryWasCreated)) {
                // Delete the World directory.
            }
        }

        return directoryWasCreated;
    }

    private static boolean createArticleTypeDirectories(Context context, File worldDirectory) {
        boolean directoriesWereCreated = true;

        for (Category category : Category.values()) {
            File articleFolder = new File(worldDirectory.getAbsolutePath(), category.pluralName(context));

            if (!(articleFolder.mkdirs())) {
                directoriesWereCreated = false;
            }
        }

        return directoriesWereCreated;
    }

    /**
     * Saves a String to a text file within an Article's directory.
     * @param context The Context calling this method.
     * @param worldName The name of the world the Article belongs to.
     * @param category The name of the Category the Article belongs to.
     * @param articleName The name of the Article whose directory will possess the text file.
     * @param fileName The name of the text file that will be written to, with the file extension
     *                 omitted.
     * @param contents The String that will be saved in a text file.
     * @return True if the String was saved successfully; false if an I/O error occurs.
     */
    public static boolean writeStringToArticleFile(Context context, String worldName,
                                                   Category category, String articleName,
                                                   String fileName, String contents) {
        Boolean result = true;

        try {
            PrintWriter writer = new PrintWriter(FileRetriever.getArticleFile(context, worldName,
                    category, articleName, fileName + ExternalReader.TEXT_FIELD_FILE_EXTENSION));
            writer.println(contents);
            writer.close();
        } catch (IOException error) {
            result = false;
        }

        return result;
    }
}

@@ -67,16 +67,40 @@
 ontext;%0A
+import android.net.Uri;%0A
 import a
@@ -162,36 +162,173 @@
 tegory;%0A
-%0Aimport java.io.File
+import com.averi.worldscribe.R;%0A%0Aimport java.io.BufferedInputStream;%0Aimport java.io.BufferedOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileInputStream
 ;%0Aimport
@@ -351,32 +351,65 @@
 FoundException;%0A
+import java.io.FileOutputStream;%0A
 import java.io.I
@@ -549,16 +549,70 @@
 riter %7B%0A
+%0A    public static final int IMAGE_BYTE_SIZE = 1024;%0A%0A
 //    pu
@@ -3877,32 +3877,2021 @@
 lse;%0A        %7D%0A%0A
+        return result;%0A    %7D%0A%0A    /**%0A     * Saves an image to a specific Article's directory.%0A     * If the Article already has an image, it will be overwritten.%0A     * @param context The Context calling this method.%0A     * @param worldName The name of the world the Article belongs to.%0A     * @param category The name of the Category the Article belongs to.%0A     * @param articleName The name of the Article whose image will be saved.%0A     * @param imageUri The URI to the new image's original location.%0A     * @return True if the image was saved successfully; false otherwise.%0A     */%0A    public static boolean saveArticleImage(Context context, String worldName, Category category,%0A                                           String articleName, Uri imageUri) %7B%0A        Boolean result = true;%0A%0A        String sourceFilename= imageUri.getPath();%0A        String destinationFilename = FileRetriever.getArticleFile(context, worldName, category,%0A                articleName, context.getString(R.string.imageFileName)).getAbsolutePath();%0A        destinationFilename += ExternalReader.IMAGE_FILE_EXTENSION;%0A%0A        BufferedInputStream bufferedInputStream = null;%0A        BufferedOutputStream bufferedOutputStream = null;%0A%0A        try %7B%0A            bufferedInputStream = new BufferedInputStream(new FileInputStream(sourceFilename));%0A            bufferedOutputStream = new BufferedOutputStream(%0A                    new FileOutputStream(destinationFilename, false));%0A            byte%5B%5D buf = new byte%5BIMAGE_BYTE_SIZE%5D;%0A            bufferedInputStream.read(buf);%0A            do %7B%0A                bufferedOutputStream.write(buf);%0A            %7D while (bufferedInputStream.read(buf) != -1);%0A        %7D catch (IOException e) %7B%0A            result = false;%0A        %7D finally %7B%0A            try %7B%0A                if (bufferedInputStream != null) bufferedInputStream.close();%0A                if (bufferedOutputStream != null) bufferedOutputStream.close();%0A            %7D catch (IOException e) %7B %7D%0A        %7D%0A%0A
         return r

Add saveArticleImage to ExternalWriter