/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * 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 jetbrains.buildServer.agent.android.tools;

import com.intellij.execution.ExecutionException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import jetbrains.buildServer.agent.BuildRunnerContext;
import jetbrains.buildServer.agent.android.NonZeroExitCodeException;
import jetbrains.buildServer.util.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class AaptTool extends AbstractTool {
  @NotNull private final String myAndroidJarPath;

  public AaptTool(@NotNull final String toolPath, @NotNull final String androidJarPath, @NotNull final BuildRunnerContext context) {
    super(toolPath, context);
    myAndroidJarPath = androidJarPath;
  }

  @NotNull
  public File packageResources(@NotNull final File tempDir,
                               @NotNull final String manifestFile,
                               @NotNull final String[] resourcesDirs,
                               @Nullable final String assetsDir,
                               @NotNull final String additionalOptions) throws IOException, ExecutionException, NonZeroExitCodeException {
    final File tempFile = FileUtil.createTempFile(tempDir, "", ".apk", true);

    final List<String> argsBefore = new ArrayList<String>();
    Collections.addAll(argsBefore, "package", "-f", "-M", manifestFile);
    addCommonArgsBefore(argsBefore, resourcesDirs);

    final List<String> argsAfter = new ArrayList<String>();
    addCommonArgsAfter(argsAfter, resourcesDirs, assetsDir);
    Collections.addAll(argsAfter, "-F", tempFile.getAbsolutePath());

    execute(argsBefore, argsAfter, additionalOptions);

    return tempFile;
  }

  @NotNull
  public File generateRFile(@NotNull final File tempDir,
                            @NotNull final String manifestFile,
                            @NotNull final String[] resourcesDirs,
                            @Nullable final String assetsDir,
                            @NotNull final String additionalOptions) throws IOException, ExecutionException, NonZeroExitCodeException {
    final File tempRFileRootDir = FileUtil.createTempDirectory("___", "", tempDir);

    List<String> argsBefore = new ArrayList<String>();
    Collections.addAll(argsBefore, "package", "-m", "-J", tempRFileRootDir.getAbsolutePath(), "-M", manifestFile);
    addCommonArgsBefore(argsBefore, resourcesDirs);

    List<String> argsAfter = new ArrayList<String>();
    addCommonArgsAfter(argsBefore, resourcesDirs, assetsDir);

    execute(argsBefore, argsAfter, additionalOptions);

    return tempRFileRootDir;
  }

  private void addCommonArgsBefore(@NotNull final List<String> args, @NotNull final String[] resourcesDirs) {
    if (isDebug()) {
      args.add("-v");
    }
    if (resourcesDirs.length > 1) {
      args.add("--auto-add-overlay");
    }
  }

  private void addCommonArgsAfter(@NotNull final List<String> args,
                                  @NotNull final String[] resourcesDirs,
                                  @Nullable final String assetsDir) {
    for (final String resourcesDir : resourcesDirs) {
      Collections.addAll(args, "-S", resourcesDir);
    }
    if (assetsDir != null) {
      Collections.addAll(args, "-A", assetsDir);
    }
    Collections.addAll(args, "-I", myAndroidJarPath);
  }
}
