/*
 * Copyright 2016 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
package io.flutter.console;

import com.intellij.execution.ConsoleFolding;
import com.intellij.execution.ExecutionException;
import com.intellij.openapi.util.text.StringUtil;
import io.flutter.sdk.FlutterSdk;
import io.flutter.sdk.FlutterSdkUtil;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.List;

/**
 * Fold lines like
 * '/Users/.../projects/flutter/flutter/bin/flutter --no-color packages get'.
 */
public class FlutterConsoleFolding extends ConsoleFolding {
  private static final String marker = File.separator + "flutter --no-color ";

  @Override
  public boolean shouldFoldLine(String line) {
    if (!line.contains(marker)) return false;

    try {
      final FlutterSdk sdk = FlutterSdk.getGlobalFlutterSdk();
      if (sdk == null) return false;
      final String flutterPath = FlutterSdkUtil.pathToFlutterTool(sdk.getHomePath());
      return line.startsWith(flutterPath);
    }
    catch (ExecutionException e) {
      return false;
    }
  }

  @Nullable
  @Override
  public String getPlaceholderText(List<String> lines) {
    final String fullText = StringUtil.join(lines, "\n");
    int index = fullText.indexOf(marker);
    if (index == -1) {
      return fullText;
    }
    else {
      return "flutter " + fullText.substring(index + marker.length());
    }
  }
}

@@ -264,154 +264,32 @@
 lij.
-execution.ExecutionException;%0Aimport com.intellij.openapi.util.text.StringUtil;%0Aimport io.flutter.sdk.FlutterSdk;%0Aimport io.flutter.sdk.FlutterSdk
+openapi.util.text.String
 Util
@@ -377,16 +377,48 @@
 il.List;
+%0Aimport java.util.regex.Pattern;
 %0A%0A/**%0A *
@@ -656,16 +656,201 @@
 lor %22;%0A%0A
+  private boolean isFolding = false;%0A%0A  // CoreSimulatorBridge: Requesting launch of ... with options: %7B%0A  private static final Pattern iosPattern = Pattern.compile(%22%5E%5C%5Cw+: .* %5C%5C%7B$%22);%0A%0A
   @Overr
@@ -912,9 +912,8 @@
 if (
-!
 line
@@ -931,30 +931,43 @@
 arker)) 
-return
+%7B%0A      isFolding =
  false;%0A
 %0A    try
@@ -962,112 +962,95 @@
 se;%0A
-%0A
+  
     
-try %7B%0A      final FlutterSdk sdk = FlutterSdk.getGlobalFlutterSdk();%0A      if (sdk == null)
+return true;%0A    %7D%0A%0A    if (isFolding && line.startsWith((%22%5Ct%22))) %7B%0A     
  return 
 fals
@@ -1049,150 +1049,117 @@
 urn 
-fals
+tru
 e;%0A    
-  final String flutterPath = FlutterSdkUtil.pathToFlutterTool(sdk.getHomePath());%0A      return line.startsWith(flutterPath)
+%7D%0A%0A    if (iosPattern.matcher(line).matches()) %7B%0A      isFolding = true;%0A      return false
 ;%0A    %7D%0A
     
@@ -1158,45 +1158,33 @@
   %7D%0A
+%0A
     
-catch (ExecutionException e) %7B%0A  
+isFolding = false;%0A%0A
     
@@ -1197,22 +1197,16 @@
  false;%0A
-    %7D%0A
   %7D%0A%0A  @
@@ -1345,16 +1345,22 @@
 %22);%0A    
+final 
 int inde
@@ -1425,24 +1425,24 @@
  return 
-fullText
+%22 ... %7D%22
 ;%0A    %7D%0A

improve console folding for iOS (#546)