/*
 * Copyright 2016 The Bazel Authors. All rights reserved.
 *
 * 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 com.google.idea.blaze.android.manifest;

import com.google.common.collect.Maps;
import com.google.idea.blaze.base.model.BlazeProjectData;
import com.google.idea.blaze.base.projectview.ProjectViewSet;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.google.idea.blaze.base.settings.BlazeImportSettings;
import com.google.idea.blaze.base.sync.SyncListener;
import com.google.idea.blaze.base.sync.SyncMode;
import com.google.idea.blaze.base.sync.SyncResult;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.jetbrains.android.dom.manifest.Manifest;
import org.jetbrains.android.util.AndroidUtils;

/** Parses manifests from the project. */
public class ManifestParser {
  private static final Logger LOG = Logger.getInstance(ManifestParser.class);
  private final Project project;
  private Map<File, Manifest> manifestFileMap = Maps.newHashMap();

  public static ManifestParser getInstance(Project project) {
    return ServiceManager.getService(project, ManifestParser.class);
  }

  public ManifestParser(Project project) {
    this.project = project;
  }

  @Nullable
  public Manifest getManifest(File file) {
    if (!file.exists()) {
      return null;
    }
    Manifest manifest = manifestFileMap.get(file);
    // Note: The manifest may be invalid if the underlying VirtualFile is invalidated.
    // Once invalid, it cannot become valid again, and must be reloaded.
    if (manifest != null && isValid(manifest)) {
      return manifest;
    }
    final VirtualFile virtualFile;
    if (ApplicationManager.getApplication().isDispatchThread()) {
      virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
    } else {
      virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file);
    }
    if (virtualFile == null) {
      LOG.error("Could not find manifest: " + file);
      return null;
    }
    manifest = AndroidUtils.loadDomElement(project, virtualFile, Manifest.class);
    manifestFileMap.put(file, manifest);
    return manifest;
  }

  private static boolean isValid(Manifest manifest) {
    return ReadAction.compute(() -> manifest.isValid());
  }

  public void refreshManifests(Collection<File> manifestFiles) {
    List<VirtualFile> manifestVirtualFiles =
        manifestFiles.stream()
            .map(file -> VfsUtil.findFileByIoFile(file, false))
            .filter(Objects::nonNull)
            .collect(Collectors.toList());

    VfsUtil.markDirtyAndRefresh(
        false, false, false, ArrayUtil.toObjectArray(manifestVirtualFiles, VirtualFile.class));
  }

  static class ClearManifestParser implements SyncListener {
    @Override
    public void onSyncComplete(
        Project project,
        BlazeContext context,
        BlazeImportSettings importSettings,
        ProjectViewSet projectViewSet,
        BlazeProjectData blazeProjectData,
        SyncMode syncMode,
        SyncResult syncResult) {
      getInstance(project).manifestFileMap.clear();
    }
  }
}

@@ -1819,16 +1819,19 @@
 ls;%0A%0A/**
+%0A *
  Parses 
@@ -1857,16 +1857,362 @@
 project.
+%0A *%0A * %3Cp%3ENote: Manifests generated from android builds are not tracked by the VFS and are therefore not%0A * guaranteed to be up-to-date when accessed. %7B@link ManifestParser#refreshManifests(Collection)%7D%0A * must be used to keep the VFS up-to-date as soon as changes to generated manifest files occur.%0A * E.g. at the end of a android_binary build.%0A
  */%0Apubl
@@ -2551,29 +2551,30 @@
 s);%0A  %7D%0A%0A  p
-ublic
+rivate
  ManifestPar
@@ -2629,16 +2629,400 @@
 t;%0A  %7D%0A%0A
+  /**%0A   * Returns a possibly out-of-date manifest file.%0A   *%0A   * %3Cp%3EThe returned manifest is not guaranteed to be up-to-date for un-tracked manifest files. E.g.%0A   * A manifest file generated as a part of the build process. %7B@link%0A   * ManifestParser#refreshManifests(Collection)%7D can be used to refresh the files in attempt to%0A   * keep it up-to-date until the last refresh.%0A   */%0A
   @Nulla
@@ -4060,16 +4060,357 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Refresh the VFS for given %7B@code manifestFiles%7D and block until the refresh is complete.%0A   *%0A   * %3Cp%3ENote: This VFS refresh operation could block for a very long time depending on the number of%0A   * queued VFS refreshes. The manifest is not guaranteed to be up-to-date until this method%0A   * returns. %7B@see ManifestParser%7D%0A   */%0A
   public

Document and clean-up ManifestParser.