package com.tns;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.util.Log;

public class AssetExtractor {
    private native void extractAssets(String apkPath, String input, String outputDir, boolean checkForNewerFiles);
    private final Logger logger;

    public AssetExtractor(File libPath, Logger logger) {
        this.logger = logger;
    }

    public void extractAssets(Context context, String inputPath, String outputPath, ExtractPolicy extractPolicy, boolean shouldCleanUpPreviousAssets) {
        FileExtractor extractor = extractPolicy.extractor();
        if (extractor != null) {
            boolean success = extractor.extract(context);
            if (logger.isEnabled()) {
                logger.write("extract returned " + success);
            }
        } else if (extractPolicy.shouldExtract(context)) {
            if (shouldCleanUpPreviousAssets) {
                try {
                    delete(new File(outputPath + inputPath));
                } catch (IOException e) {
                    Log.d(LogTag, "Problem occurred while deleting assets from previous app version: " + outputPath + inputPath);
                }
            }

            String apkPath = context.getPackageCodePath();

            boolean forceOverwrite = extractPolicy.forceOverwrite();

            extractAssets(apkPath, inputPath, outputPath, forceOverwrite);
        } else {
            if (logger.isEnabled()) {
                logger.write("Skipped extraction of assets in " + inputPath);
            }
        }
    }

    /**
     * Delete a file or a directory and its children.
     * @param file The directory to delete.
     * @throws IOException Exception when problem occurs during deleting the directory.
     */
    private static void delete(File file) throws IOException {
        File[] files = file.listFiles();
        if (files == null) {
            Log.d(LogTag, "Can't remove previously installed assets in " + file.getAbsolutePath());
            return;
        }

        for (File childFile : files) {
            if (childFile.isDirectory()) {
                delete(childFile);
            } else {
                if (!childFile.delete()) {
                    throw new IOException();
                }
            }
        }

        if (!file.delete()) {
            throw new IOException();
        }
    }

    private static String LogTag = "JS: AssetExtraction";
}
@@ -1037,24 +1037,79 @@
 eption e) %7B%0A
+                    String logTag = %22AssetExtraction%22;%0A
             
@@ -1114,33 +1114,33 @@
           Log.d(
-L
+l
 ogTag, %22Problem 
@@ -1967,108 +1967,8 @@
 ) %7B%0A
-            Log.d(LogTag, %22Can't remove previously installed assets in %22 + file.getAbsolutePath());%0A
     
@@ -2350,64 +2350,6 @@
   %7D%0A
-%0A    private static String LogTag = %22JS: AssetExtraction%22;%0A
 %7D
+%0A

remove faulty message when removing previously installed assets from the app directory (#775)