package com.apps.anker.facepunchdroid;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import static android.text.Html.escapeHtml;

/**
 * Created by Mikkel on 24-02-2016.
 */
public class customCSS {

    public static String cssToString(InputStream asset) {
        // String builder for making nice single line css
        StringBuilder tmp = new StringBuilder();

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                    new InputStreamReader(asset));

            // do reading, usually loop until end of file reading
            String mLine;
            while ((mLine = reader.readLine()) != null) {
                //process line
                tmp.append(mLine);
            }
        } catch (IOException e) {
            //log the exception
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    //log the exception
                }
            }
        }

        return  tmp.toString();
    }

    public static String jsToString(InputStream asset) {
        // String builder for making nice single line css
        StringBuilder tmp = new StringBuilder();

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                    new InputStreamReader(asset));

            // do reading, usually loop until end of file reading
            String mLine;
            while ((mLine = reader.readLine()) != null) {
                //process line
                tmp.append(mLine);
            }
        } catch (IOException e) {
            //log the exception
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    //log the exception
                }
            }
        }

        return tmp.toString();
    }

    public static String readFromSDcard(Activity mActivitym, Uri path) throws FileNotFoundException {
        //Get the text file

        InputStream inputStream = mActivitym.getContentResolver().openInputStream(path);

        //Read text from file
        StringBuilder text = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line;

            while ((line = br.readLine()) != null) {
                text.append(line);
            }
            br.close();
        }
        catch (IOException e) {
            //You'll need to add proper error handling here
            Log.e("FileError", "Something went wrong..", e);
        }

        Log.d("Read from file", text.toString());
        return text.toString();
    }

}

@@ -1183,32 +1183,66 @@
 .append(mLine);%0A
+                tmp.append(%22%5Cn%22);%0A
             %7D%0A  
@@ -1231,32 +1231,32 @@
 ;%0A            %7D%0A
-
         %7D catch 
@@ -2099,32 +2099,66 @@
 .append(mLine);%0A
+                tmp.append(%22%5Cn%22);%0A
             %7D%0A  
@@ -2713,24 +2713,25 @@
 eam(path);%0A%0A
+%0A
         //Re
@@ -2972,32 +2972,32 @@
 e()) != null) %7B%0A
-
                 
@@ -3011,24 +3011,59 @@
 pend(line);%0A
+                text.append(%22%5Cn%22);%0A
             

Add handling newlines in CSS and JS, to support comments