/**
 *  Copyright 2014 Ryszard Wiśniewski <brut.alll@gmail.com>
 *  Copyright 2014 Connor Tumbleson <connor.tumbleson@gmail.com>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package brut.androlib;

import brut.androlib.res.util.ExtFile;
import brut.common.BrutException;
import brut.directory.DirectoryException;
import brut.util.OS;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.Assert.assertTrue;

public class SharedLibraryTest {

    @BeforeClass
    public static void beforeClass() throws BrutException {
        sTmpDir = new ExtFile(OS.createTempDirectory());
        TestUtils.copyResourceDir(SharedLibraryTest.class, "brut/apktool/shared_libraries/", sTmpDir);
    }

    @AfterClass
    public static void afterClass() throws BrutException {
        OS.rmdir(sTmpDir);
    }

    @Test
    public void isFrameworkTaggingWorking() throws AndrolibException {
        String apkName = "library.apk";

        ApkOptions apkOptions = new ApkOptions();
        apkOptions.frameworkFolderLocation = sTmpDir.getAbsolutePath();
        apkOptions.frameworkTag = "building";

        new Androlib(apkOptions).installFramework(new File(sTmpDir + File.separator + apkName));

        assertTrue(fileExists("2-building.apk"));
    }

    @Test
    public void isFrameworkInstallingWorking() throws AndrolibException {
        String apkName = "library.apk";

        ApkOptions apkOptions = new ApkOptions();
        apkOptions.frameworkFolderLocation = sTmpDir.getAbsolutePath();

        new Androlib(apkOptions).installFramework(new File(sTmpDir + File.separator + apkName));

        assertTrue(fileExists("2.apk"));
    }

    @Test
    public void isSharedResourceDecodingWorking() throws IOException, BrutException {
        String framework = "library.apk";
        String client = "client.apk";


        ApkOptions apkOptions = new ApkOptions();
        apkOptions.frameworkFolderLocation = sTmpDir.getAbsolutePath();
        apkOptions.frameworkTag = "shared";

        new Androlib(apkOptions).installFramework(new File(sTmpDir + File.separator + framework));

        assertTrue(fileExists("2-shared.apk"));

        ApkDecoder apkDecoder = new ApkDecoder(new File(sTmpDir + File.separator + client));
        apkDecoder.setOutDir(new File(sTmpDir + File.separator + client + ".out"));
        apkDecoder.setFrameworkDir(apkOptions.frameworkFolderLocation);
        apkDecoder.setFrameworkTag(apkOptions.frameworkTag);
        apkDecoder.decode();

        ExtFile testApk = new ExtFile(sTmpDir, client + ".out");
        new Androlib(apkOptions).build(testApk, null);

        assertTrue(fileExists(client + ".out" + File.separator + "dist" + File.separator + client));
    }

    private boolean fileExists(String filepath) {
        return Files.exists(Paths.get(sTmpDir.getAbsolutePath() + File.separator + filepath));
    }

    private static ExtFile sTmpDir;
}

@@ -2364,16 +2364,29 @@
 Decoding
+AndRebuilding
 Working(
@@ -2438,25 +2438,23 @@
  String 
-framework
+library
  = %22libr
@@ -2502,16 +2502,43 @@
 .apk%22;%0A%0A
+        // setup apkOptions
 %0A       
@@ -2693,24 +2693,61 @@
  %22shared%22;%0A%0A
+        // install library/framework%0A
         new 
@@ -2824,22 +2824,19 @@
 r + 
-framework
+library
 ));%0A
-%0A
     
@@ -2876,24 +2876,53 @@
 ed.apk%22));%0A%0A
+        // decode client.apk%0A
         ApkD
@@ -3261,19 +3261,441 @@
     
-ExtFile tes
+// decode library.apk%0A        ApkDecoder libraryDecoder = new ApkDecoder(new File(sTmpDir + File.separator + library));%0A        libraryDecoder.setOutDir(new File(sTmpDir + File.separator + library + %22.out%22));%0A        libraryDecoder.setFrameworkDir(apkOptions.frameworkFolderLocation);%0A        libraryDecoder.setFrameworkTag(apkOptions.frameworkTag);%0A        libraryDecoder.decode();%0A%0A        // build client.apk%0A        ExtFile clien
 tApk
@@ -3779,11 +3779,13 @@
 ild(
-tes
+clien
 tApk
@@ -3793,17 +3793,16 @@
  null);%0A
-%0A
         
@@ -3889,24 +3889,301 @@
  + client));
+%0A%0A        // build library.apk (shared library)%0A        ExtFile libraryApk = new ExtFile(sTmpDir, library + %22.out%22);%0A        new Androlib(apkOptions).build(libraryApk, null);%0A        assertTrue(fileExists(library + %22.out%22 + File.separator + %22dist%22 + File.separator + library));
 %0A    %7D%0A%0A    

add test for shared library rebuild