/*
 * Copyright 2000-2015 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 git4idea.rebase;

import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;

class GitSuccessfulRebase extends GitRebaseStatus {
  private final SuccessType mySuccessType;

  private GitSuccessfulRebase(@NotNull SuccessType successType, @NotNull Collection<GitRebaseUtils.CommitInfo> skippedCommits) {
    super(Type.SUCCESS, skippedCommits);
    mySuccessType = successType;
  }

  @NotNull
  public SuccessType getSuccessType() {
    return mySuccessType;
  }

  @NotNull
  static GitSuccessfulRebase parseFromOutput(@NotNull List<String> output, @NotNull Collection<GitRebaseUtils.CommitInfo> skippedCommits) {
    return new GitSuccessfulRebase(SuccessType.fromOutput(output), skippedCommits);
  }

  enum SuccessType {
    REBASED {
      @NotNull
      @Override
      public String formatMessage(@Nullable String currentBranch, @NotNull String baseBranch, boolean withCheckout) {
        if (withCheckout) {
          return "Checked out" + mention(currentBranch) + " and rebased it on " + baseBranch;
        }
        else {
          return "Rebased" + mention(currentBranch) + " on " + baseBranch;
        }
      }
    },
    UP_TO_DATE {
      @NotNull
      @Override
      public String formatMessage(@Nullable String currentBranch, @NotNull String baseBranch, boolean withCheckout) {
        String msg = currentBranch != null ? currentBranch + " is up-to-date" : "Up-to-date";
        msg += " with " + baseBranch;
        return msg;
      }
    },
    FAST_FORWARDED {
      @NotNull
      @Override
      public String formatMessage(@Nullable String currentBranch, @NotNull String baseBranch, boolean withCheckout) {
        if (withCheckout) {
          return "Checked out" + mention(currentBranch) + " and fast-forwarded it to " + baseBranch;
        }
        else {
          return "Fast-forwarded" + mention(currentBranch) + " to " + baseBranch;
        }
      }
    };

    @NotNull
    private static String mention(@Nullable String currentBranch) {
      return currentBranch != null ? " " + currentBranch : "";
    }

    @NotNull
    abstract String formatMessage(@Nullable String currentBranch, @NotNull String baseBranch, boolean withCheckout);

    @NotNull
    public static SuccessType fromOutput(@NotNull List<String> output) {
      for (String line : output) {
        if (StringUtil.containsIgnoreCase(line, "Fast-forwarded")) {
          return FAST_FORWARDED;
        }
        if (StringUtil.containsIgnoreCase(line, "is up to date")) {
          return UP_TO_DATE;
        }
      }
      return REBASED;
    }
  }
}

@@ -1561,38 +1561,39 @@
 urrentBranch, @N
-otN
 ull
+able
  String baseBran
@@ -1612,32 +1612,52 @@
 withCheckout) %7B%0A
+        String msg;%0A
         if (with
@@ -1670,38 +1670,37 @@
 ut) %7B%0A          
-return
+msg =
  %22Checked out%22 +
@@ -1741,34 +1741,17 @@
 based it
- on %22 + baseBranch
+%22
 ;%0A      
@@ -1779,22 +1779,21 @@
         
-return
+msg =
  %22Rebase
@@ -1819,18 +1819,66 @@
 tBranch)
+;%0A        %7D%0A        if (baseBranch != null) msg
  +
+=
  %22 on %22 
@@ -1891,33 +1891,43 @@
 Branch;%0A        
-%7D
+return msg;
 %0A      %7D%0A    %7D,%0A
@@ -2034,38 +2034,39 @@
 urrentBranch, @N
-otN
 ull
+able
  String baseBran
@@ -2190,24 +2190,48 @@
 te%22;%0A       
+ if (baseBranch != null)
  msg += %22 wi
@@ -2396,38 +2396,39 @@
 urrentBranch, @N
-otN
 ull
+able
  String baseBran
@@ -2447,32 +2447,52 @@
 withCheckout) %7B%0A
+        String msg;%0A
         if (with
@@ -2513,22 +2513,21 @@
         
-return
+msg =
  %22Checke
@@ -2583,34 +2583,17 @@
 arded it
- to %22 + baseBranch
+%22
 ;%0A      
@@ -2621,22 +2621,21 @@
         
-return
+msg =
  %22Fast-f
@@ -2668,18 +2668,66 @@
 tBranch)
+;%0A        %7D%0A        if (baseBranch != null) msg
  +
+=
  %22 to %22 
@@ -2740,33 +2740,43 @@
 Branch;%0A        
-%7D
+return msg;
 %0A      %7D%0A    %7D;%0A
@@ -3008,22 +3008,23 @@
 anch, @N
-otN
 ull
+able
  String 

fix NPE if base branch is undefined EA-78554