package org.robospock.internal;

import org.junit.runners.model.InitializationError;
import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.FileFsFile;
import org.robolectric.util.Logger;
import org.robolectric.util.ReflectionHelpers;

public class GradleRoboSputnik extends RoboSputnik {

    // Copied from org.robolectric.RobolectricGradleTestRunner
    private static final String BUILD_OUTPUT = "build/intermediates";

    public GradleRoboSputnik(Class<?> clazz) throws InitializationError {
        super(clazz);
    }

    @Override
    protected AndroidManifest getAppManifest(Config config) {
        // Copied from org.robolectric.RobolectricGradleTestRunner
        if (config.constants() == Void.class) {
            Logger.error("Field 'constants' not specified in @Config annotation");
            Logger.error("This is required when using RobolectricGradleTestRunner!");
            throw new RuntimeException("No 'constants' field in @Config annotation!");
        }

        final String type = getType(config);
        final String flavor = getFlavor(config);
        final String packageName = getPackageName(config);

        final FileFsFile res;
        final FileFsFile assets;
        final FileFsFile manifest;

        // res/merged added in Android Gradle plugin 1.3-beta1
        if (FileFsFile.from(BUILD_OUTPUT, "res", "merged").exists()) {
            res = FileFsFile.from(BUILD_OUTPUT, "res", "merged", flavor, type);
        } else if (FileFsFile.from(BUILD_OUTPUT, "res").exists()) {
            res = FileFsFile.from(BUILD_OUTPUT, "res", flavor, type);
        } else {
            res = FileFsFile.from(BUILD_OUTPUT, "bundles", flavor, type, "res");
        }

        if (FileFsFile.from(BUILD_OUTPUT, "assets").exists()) {
            assets = FileFsFile.from(BUILD_OUTPUT, "assets", flavor, type);
        } else {
            assets = FileFsFile.from(BUILD_OUTPUT, "bundles", flavor, type, "assets");
        }

        if (FileFsFile.from(BUILD_OUTPUT, "manifests").exists()) {
            manifest = FileFsFile.from(BUILD_OUTPUT, "manifests", "full", flavor, type, "AndroidManifest.xml");
        } else {
            manifest = FileFsFile.from(BUILD_OUTPUT, "bundles", flavor, type, "AndroidManifest.xml");
        }

        Logger.debug("Robolectric assets directory: " + assets.getPath());
        Logger.debug("   Robolectric res directory: " + res.getPath());
        Logger.debug("   Robolectric manifest path: " + manifest.getPath());
        Logger.debug("    Robolectric package name: " + packageName);
        return new AndroidManifest(manifest, res, assets, packageName);
    }

    private static String getType(Config config) {
        // Copied from org.robolectric.RobolectricGradleTestRunner
        try {
            return ReflectionHelpers.getStaticField(config.constants(), "BUILD_TYPE");
        } catch (Throwable e) {
            return null;
        }
    }

    private static String getFlavor(Config config) {
        // Copied from org.robolectric.RobolectricGradleTestRunner
        try {
            return ReflectionHelpers.getStaticField(config.constants(), "FLAVOR");
        } catch (Throwable e) {
            return null;
        }
    }

    private static String getPackageName(Config config) {
        // Copied from org.robolectric.RobolectricGradleTestRunner
        try {
            final String packageName = config.packageName();
            if (packageName != null && !packageName.isEmpty()) {
                return packageName;
            } else {
                return ReflectionHelpers.getStaticField(config.constants(), "APPLICATION_ID");
            }
        } catch (Throwable e) {
            return null;
        }
    }
}
@@ -2093,107 +2093,421 @@
     
-manifest = FileFsFile.from(BUILD_OUTPUT, %22manifests%22, %22full%22, flavor, type, %22AndroidManifest.xml%22);
+if(FileFsFile.from(BUILD_OUTPUT, %22manifests%22, %22full%22).exists())%7B%0A                // pre Android Gradle 2.2%0A                manifest = FileFsFile.from(BUILD_OUTPUT, %22manifests%22, %22full%22, flavor, type, %22AndroidManifest.xml%22);%0A            %7D else %7B%0A                // Android Gradle 2.2.+%0A                manifest = FileFsFile.from(BUILD_OUTPUT, %22manifests%22, %22aapt%22, flavor, type, %22AndroidManifest.xml%22);%0A            %7D
 %0A   

Read AndroidManifest from new location.