package org.robolectric.internal;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.robolectric.annotation.Config;
import org.robolectric.res.Fs;
import org.robolectric.res.FsFile;
import org.robolectric.util.Util;

public class BuckManifestFactory implements ManifestFactory {

  private static final String BUCK_ROBOLECTRIC_RES_DIRECTORIES = "buck.robolectric_res_directories";
  private static final String BUCK_ROBOLECTRIC_ASSETS_DIRECTORIES = "buck.robolectric_assets_directories";
  private static final String BUCK_ROBOLECTRIC_MANIFEST = "buck.robolectric_manifest";

  @Override
  public ManifestIdentifier identify(Config config) {
    String buckManifest = System.getProperty(BUCK_ROBOLECTRIC_MANIFEST);
    FsFile manifestFile = Fs.fileFromPath(buckManifest);

    String buckResDirs = System.getProperty(BUCK_ROBOLECTRIC_RES_DIRECTORIES);
    String buckAssetsDirs = System.getProperty(BUCK_ROBOLECTRIC_ASSETS_DIRECTORIES);
    String packageName = config.packageName();

    final List<FsFile> buckResources = getDirectoriesFromProperty(buckResDirs);
    final List<FsFile> buckAssets = getDirectoriesFromProperty(buckAssetsDirs);

    if (buckResources.size() != buckAssets.size()) {
      throw new IllegalArgumentException("number of resources and assets do not match match: "
          + buckResources.size() + " != " + buckAssets.size());
    }

    int pathCount = buckResources.size();
    final FsFile resDir;
    final FsFile assetsDir;
    final List<ManifestIdentifier> libraries;
    if (pathCount == 0) {
      resDir = null;
      assetsDir = null;
      libraries = null;
    } else {
      resDir = buckResources.get(pathCount - 1);
      assetsDir = buckAssets.get(pathCount - 1);
      libraries = new ArrayList<>();

      for (int i = 0; i < pathCount - 1; i++) {
        libraries.add(new ManifestIdentifier(null, null,
            buckResources.get(i), buckAssets.get(i), null));
      }
    }

    return new ManifestIdentifier(packageName, manifestFile, resDir, assetsDir, libraries);
  }

  public static boolean isBuck() {
    return System.getProperty(BUCK_ROBOLECTRIC_MANIFEST) != null;
  }

  @Nonnull
  private List<FsFile> getDirectoriesFromProperty(String property) {
    if (property == null) {
      return Collections.emptyList();
    }

    List<String> dirs;
    if (property.startsWith("@")) {
      String filename = property.substring(1);
      try {
        dirs = Arrays.asList(
            new String(Util.readBytes(new FileInputStream(filename)), UTF_8).split("\\n"));
      } catch (IOException e) {
        throw new RuntimeException("Cannot read file " + filename);
      }
    } else {
      dirs = Arrays.asList(property.split(File.pathSeparator));
    }

    return dirs.stream().map(Fs::fileFromPath).collect(Collectors.toList());
  }
}

@@ -1415,321 +1415,212 @@
 s);%0A
-%0A
     
-if (buckResources.size() != buckAssets.size()) %7B%0A      throw new IllegalArgumentException(%22number of resources and assets do not match match: %22%0A          + buckResources.size() + %22 != %22 + buckAssets.size());%0A    %7D%0A%0A    int pathCount = buckResources.size();%0A    final FsFile resDir;%0A    final FsFile assetsDir
+final FsFile resDir = buckResources.size() == 0 ? null : buckResources.get(buckResources.size() - 1);%0A    final FsFile assetsDir = buckAssets.size() == 0 ? null : buckAssets.get(buckAssets.size() - 1)
 ;%0A  
@@ -1663,24 +1663,25 @@
 raries;%0A
+%0A
     if (
 pathCoun
@@ -1676,40 +1676,16 @@
 if (
-pathCount == 0) %7B%0A      
 resDir =
  nul
@@ -1680,28 +1680,25 @@
 resDir =
+=
  null
-;%0A     
+ &&
  assetsD
@@ -1701,22 +1701,25 @@
 etsDir =
+=
  null
-;
+) %7B
 %0A      l
@@ -1758,182 +1758,222 @@
     
-resDir = buckResources.get(pathCount - 1
+libraries = new ArrayList%3C%3E(
 );%0A
+%0A
       
-assetsDir = buckAssets.get(pathCount - 1);%0A      libraries = new ArrayList%3C%3E();%0A%0A      for (int i = 0; i %3C pathCount - 1; i++
+for (FsFile buckResource: buckResources) %7B%0A        libraries.add(new ManifestIdentifier(null, null, buckResource, null, null));%0A      %7D%0A%0A      for (FsFile buckAsset: buckAssets
 ) %7B%0A
@@ -2032,41 +2032,13 @@
 ull,
-%0A            buckResources.get(i)
+ null
 , bu
@@ -2048,16 +2048,8 @@
 sset
-s.get(i)
 , nu

Remove IllegalStateException in BuckManifestFactory