/*
 * Copyright 2017 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.view;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.openapi.wm.ToolWindowManager;
import org.jetbrains.annotations.NotNull;

public class FlutterViewFactory implements ToolWindowFactory {
  public static void init(@NotNull Project project) {
    //TODO: re-enable after UI review (#691)
    //noinspection CodeBlock2Expr
    //project.getMessageBus().connect().subscribe(
    //  FlutterViewMessages.FLUTTER_DEBUG_TOPIC, (event) -> {
    //    openFlutterView(project, event);
    //  });
  }

  private static void openFlutterView(@NotNull Project project, FlutterViewMessages.FlutterDebugEvent event) {
    ApplicationManager.getApplication().invokeLater(() -> {
      final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("Flutter");
      toolWindow.show(() -> {
        final FlutterView flutterView = ServiceManager.getService(project, FlutterView.class);
        flutterView.debugActive(event);
      });
    });
  }

  @Override
  public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
    //noinspection CodeBlock2Expr
    DumbService.getInstance(project).runWhenSmart(() -> {
      (ServiceManager.getService(project, FlutterView.class)).initToolWindow(toolWindow);
    });
  }
}

@@ -497,58 +497,8 @@
 ry;%0A
-import com.intellij.openapi.wm.ToolWindowManager;%0A
 impo
@@ -657,53 +657,8 @@
 ) %7B%0A
-    //TODO: re-enable after UI review (#691)%0A
     
@@ -691,18 +691,16 @@
 xpr%0A    
-//
 project.
@@ -740,18 +740,16 @@
 be(%0A    
-//
   Flutte
@@ -804,18 +804,16 @@
     
-//
     
-open
+init
 Flut
@@ -841,18 +841,16 @@
 t);%0A    
-//
   %7D);%0A  
@@ -874,20 +874,20 @@
 ic void 
-open
+init
 FlutterV
@@ -1033,135 +1033,59 @@
     
-final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(%22Flutter%22);%0A      toolWindow.show(() -%3E %7B%0A  
+//TODO: re-enable auto-show after UI review (#691)%0A
     
@@ -1179,18 +1179,16 @@
 ;%0A      
-  
 flutterV
@@ -1211,26 +1211,16 @@
 event);%0A
-      %7D);%0A
     %7D);%0A

Fix Flutter View initialization. (#703)