package fr.android.pointspermis.tools;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.*;

/**
 * Created by vins on 08/01/15.
 */
public class AssetsTools {

    public static void copyFileOrDir(String path, String dest, AssetManager assetManager) {

        String assets[] = null;
        try {
            Log.i("INFO", "copyFileOrDir() " + path);
            assets = assetManager.list(path);
            if (assets.length == 0) {
                copyFile(path, dest, assetManager);
            } else {
                String fullPath = dest + path;
                File dir = new File(fullPath);
                if (!dir.exists() && !path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit"))
                    if (!dir.mkdirs())
                        Log.i("INFO", "could not create dir "+fullPath);
                for (int i = 0; i < assets.length; ++i) {
                    String p;
                    if (path.equals(""))
                        p = "";
                    else
                        p = path + "/";

                    if (!path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit"))
                        copyFileOrDir(p + assets[i], dest, assetManager);
                }
            }
        } catch (IOException ex) {
            Log.e("ERROR", "I/O Exception", ex);
        }
    }

    private static void copyFile(String filename, String dest, AssetManager assetManager) {

        InputStream in = null;
        OutputStream out = null;
        String newFileName = null;
        try {
            in = assetManager.open(filename);
            if (filename.endsWith(".jpg")) // extension was added to avoid compression on APK file
                newFileName = dest + filename.substring(0, filename.length()-4);
            else
                newFileName = dest + filename;

            if (!new File(newFileName).exists()) {
                Log.i("INFO", "copyFile() "+filename);
                out = new FileOutputStream(newFileName);
                byte[] buffer = new byte[1024];
                int read;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            }
            else {
                Log.i("INFO", filename + " already exists, skipping..");
            }
        } catch (Exception e) {
            Log.e("ERROR", "Exception in copyFile() of "+newFileName);
            Log.e("ERROR", "Exception in copyFile() "+e.toString());
        }
    }

    public static Bitmap getBitmapFromAsset(String filePath, AssetManager assetManager) {

        InputStream istr;
        Bitmap bitmap = null;
        try {
            istr = assetManager.open(filePath);
            bitmap = BitmapFactory.decodeStream(istr);
        } catch (IOException e) {
            Log.e("ERROR", "Unable to get bitmap '" + filePath + "' from assets");
        }

        return bitmap;
    }
}
