/*
 * Copyright 2016-present Facebook, Inc.
 *
 * 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.facebook.buck.rage;

import static com.facebook.buck.util.versioncontrol.VersionControlStatsGenerator.TRACKED_BOOKMARKS;

import com.facebook.buck.util.immutables.BuckStyleImmutable;
import com.facebook.buck.util.versioncontrol.VersionControlCmdLineInterface;
import com.facebook.buck.util.versioncontrol.VersionControlCommandFailedException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import org.immutables.value.Value;

import java.util.Map;
import java.util.Optional;

/**
 * Responsible foe getting information out of the version control system.
 */
public class VcsInfoCollector {

  private final VersionControlCmdLineInterface vcCmdLineInterface;

  private VcsInfoCollector(VersionControlCmdLineInterface vcCmdLineInterface) {
    this.vcCmdLineInterface = vcCmdLineInterface;
  }

  public static Optional<VcsInfoCollector> create(
      VersionControlCmdLineInterface vcCmdLineInterface) {
    if (!vcCmdLineInterface.isSupportedVersionControlSystem()) {
      return Optional.empty();
    }
    return Optional.of(new VcsInfoCollector(vcCmdLineInterface));
  }

  public SourceControlInfo gatherScmInformation()
      throws InterruptedException, VersionControlCommandFailedException {
    String currentRevisionId = vcCmdLineInterface.currentRevisionId();
    ImmutableSet<String> changedFiles = vcCmdLineInterface.changedFiles(".");
    ImmutableMap<String, String> bookmarksRevisionIds =
        vcCmdLineInterface.bookmarksRevisionsId(TRACKED_BOOKMARKS);

    ImmutableSet.Builder<String> baseBookmarksBuilder = ImmutableSet.builder();
    for (Map.Entry<String, String> bookmark : bookmarksRevisionIds.entrySet()) {
      if (bookmark.getValue().startsWith(currentRevisionId)) {
        baseBookmarksBuilder.add(bookmark.getKey());
      }
    }
    ImmutableSet<String> baseBookmarks = baseBookmarksBuilder.build();
    Optional<String> baseRevisionId = Optional.empty();
    Optional<String> diff = Optional.empty();
    if (!baseBookmarks.isEmpty()) {
      baseRevisionId = Optional.of(bookmarksRevisionIds.get(baseBookmarks.iterator().next()));
      diff = Optional.of(
          vcCmdLineInterface.diffBetweenRevisions(currentRevisionId, baseRevisionId.get()));
    }

    return SourceControlInfo.builder()
        .setCurrentRevisionId(currentRevisionId)
        .setRevisionIdOffTracked(baseRevisionId)
        .setBasedOffWhichTracked(baseBookmarks)
        .setDiff(diff)
        .setDirtyFiles(changedFiles)
        .build();
  }

  @Value.Immutable
  @BuckStyleImmutable
  interface AbstractSourceControlInfo {
    String getCurrentRevisionId();
    ImmutableSet<String> getBasedOffWhichTracked();
    Optional<String> getRevisionIdOffTracked();
    @JsonIgnore
    Optional<String> getDiff();
    ImmutableSet<String> getDirtyFiles();
  }
}

@@ -2001,136 +2001,300 @@
 able
-Set%3CString%3E changedFiles = vcCmdLineInterface.changedFiles(%22.%22);%0A    ImmutableMap%3CString, String%3E bookmarksRevisionIds =%0A       
+Map%3CString, String%3E bookmarksRevisionIds =%0A        vcCmdLineInterface.bookmarksRevisionsId(TRACKED_BOOKMARKS);%0A    String baseRevisionId = vcCmdLineInterface.commonAncestor(%0A        currentRevisionId,%0A        bookmarksRevisionIds.get(%22remote/master%22));%0A    ImmutableSet%3CString%3E changedFiles =
  vcC
@@ -2309,33 +2309,41 @@
 terface.
-bookmarks
+changedFiles(base
 Revision
 sId(TRAC
@@ -2334,37 +2334,18 @@
 Revision
-s
 Id
-(TRACKED_BOOKMARKS
 );%0A%0A    
@@ -2386,23 +2386,16 @@
 ookmarks
-Builder
  = Immut
@@ -2535,23 +2535,20 @@
 rtsWith(
-current
+base
 Revision
@@ -2575,23 +2575,16 @@
 ookmarks
-Builder
 .add(boo
@@ -2618,340 +2618,50 @@
   %7D%0A
-    ImmutableSet%3CString%3E baseBookmarks = baseBookmarksBuilder.build();%0A    Optional%3CString%3E baseRevisionId = Optional.empty();%0A    Optional%3CString%3E diff = Optional.empty();%0A    if (!baseBookmarks.isEmpty()) %7B%0A      baseRevisionId = Optional.of(bookmarksRevisionIds.get(baseBookmarks.iterator().next()));%0A      diff = Optional.of(%0A  
+%0A    Optional%3CString%3E diff = Optional.of(%0A
     
@@ -2741,23 +2741,11 @@
 onId
-.get()));%0A    %7D
+));
 %0A%0A  
@@ -2925,16 +2925,24 @@
 ookmarks
+.build()
 )%0A      

fix baseRevision for Rage's VcsInfoCollector