/*
 * Copyright 2019 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.samples;

import com.intellij.openapi.editor.colors.ColorKey;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.project.Project;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.panels.NonOpaquePanel;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import icons.FlutterIcons;
import io.flutter.FlutterMessages;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class FlutterSampleActionsPanel extends JPanel {

  protected final JLabel myLabel = new JLabel();
  protected final JBLabel goLink;

  @NotNull private final List<FlutterSample> samples;
  @NotNull private final Project project;
  protected Color myBackgroundColor;
  protected ColorKey myBackgroundColorKey;

  final JEditorPane descriptionText;

  // Combo or label.
  JComponent sampleSelector;

  FlutterSampleActionsPanel(@NotNull List<FlutterSample> samples, @NotNull Project project) {
    super(new BorderLayout());
    this.samples = samples;
    this.project = project;

    myBackgroundColorKey = EditorColors.GUTTER_BACKGROUND;

    myLabel.setIcon(FlutterIcons.Flutter);
    myLabel.setText("Open sample project:");
    myLabel.setBorder(JBUI.Borders.emptyRight(5));

    goLink = new JBLabel("<html><a href=\"\">Go...</a></html>");
    goLink.setBorder(JBUI.Borders.emptyLeft(8));
    goLink.setCursor(new Cursor(Cursor.HAND_CURSOR));
    goLink.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        doCreate();
      }
    });


    sampleSelector = setupSelectorComponent();

    descriptionText = new JEditorPane();
    descriptionText.setBackground(getBackground());
    descriptionText.setContentType("text/html");
    descriptionText.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
    descriptionText.setBorder(JBUI.Borders.emptyLeft(3));

    final FlutterSample selectedSample = getSelectedSample();
    descriptionText.setText(selectedSample.getShortHtmlDescription());
    descriptionText.setEditable(false);

    descriptionText.setFont(getFont());
    descriptionText.setForeground(JBColor.gray);

    setupPanel();
  }


  JComponent setupSelectorComponent() {
    if (samples.size() == 1) {
      return new JBLabel(samples.get(0).getDisplayLabel());
    }

    final FlutterSampleComboBox sampleCombo = new FlutterSampleComboBox(samples);
    sampleCombo.addActionListener(e -> updateSelection(sampleCombo.getSelectedItem()));

    return sampleCombo;
  }

  private void updateSelection(@NotNull FlutterSample item) {
    descriptionText.setText(item.getShortHtmlDescription());
  }

  private void setupPanel() {
    // LABEL | SELECTOR | LINK...
    final JPanel subPanel = new NonOpaquePanel(new BorderLayout(0, 10));
    subPanel.add(BorderLayout.WEST, myLabel);
    subPanel.add(BorderLayout.CENTER, sampleSelector);
    subPanel.add(BorderLayout.EAST, goLink);
    final JPanel selectionPanel = new NonOpaquePanel(new BorderLayout(10, 0));
    selectionPanel.add(BorderLayout.NORTH, subPanel);
    add(BorderLayout.WEST, selectionPanel);

    // DESCRIPTION
    final JPanel descriptionPanel = new NonOpaquePanel(new BorderLayout());
    final int descriptionTopNudge = sampleSelector instanceof FlutterSampleComboBox ? -7 : -14;
    descriptionPanel.setBorder(JBUI.Borders.empty(descriptionTopNudge, 12, 5, 5));
    descriptionPanel.add(BorderLayout.NORTH, descriptionText);
    add(BorderLayout.CENTER, descriptionPanel);

    // PLACEHOLDER (to force reflow on resize)
    add(BorderLayout.EAST, new NonOpaquePanel(new BorderLayout()));

    setBorder(JBUI.Borders.empty(10, 10, 5,  10));
  }

  @Override
  public Color getBackground() {
    final EditorColorsScheme globalScheme = EditorColorsManager.getInstance().getGlobalScheme();
    if (myBackgroundColor != null) return myBackgroundColor;
    if (myBackgroundColorKey != null) {
      final Color color = globalScheme.getColor(myBackgroundColorKey);
      if (color != null) return color;
    }
    final Color color = globalScheme.getColor(EditorColors.NOTIFICATION_BACKGROUND);
    return color != null ? color : UIUtil.getToolTipBackground();
  }

  @NotNull
  FlutterSample getSelectedSample() {
    return samples.size() == 1 ? samples.get(0) : ((FlutterSampleComboBox)sampleSelector).getSelectedItem();
  }

  private void doCreate() {
    final FlutterSample sample = getSelectedSample();
    final String status = FlutterSampleManager.createSampleProject(sample, project);
    if (status != null) {
      FlutterMessages.showError("Sample Project Creation", "Error: " + status);
    }
  }
}

@@ -990,16 +990,17 @@
  final J
+B
 Label my
@@ -1012,16 +1012,17 @@
  = new J
+B
 Label();
@@ -1722,311 +1722,103 @@
 k = 
-new JBLabel(%22%3Chtml%3E%3Ca href=%5C%22%5C%22%3EGo...%3C/a%3E%3C/html%3E%22);%0A    goLink.setBorder(JBUI.Borders.emptyLeft(8));%0A    goLink.setCursor(new Cursor(Cursor.HAND_CURSOR));%0A    goLink.addMouseListener(new MouseAdapter() %7B%0A      @Override%0A      public void mouseClicked(MouseEvent e) %7B%0A        doCreate();%0A      %7D%0A    %7D);%0A
+createLinkLabel(%22Go...%22, this::doCreate);%0A    goLink.setBorder(JBUI.Borders.empty(1, 8, 0, 0));
 %0A%0A  
@@ -2434,16 +2434,567 @@
 );%0A  %7D%0A%0A
+  private static JBLabel createLinkLabel(@NotNull String text, @NotNull Runnable onClick) %7B%0A    // Standard hyperlinks were rendering oddly on 2018.3, so we create our own.%0A    // See: https://github.com/flutter/flutter-intellij/issues/3197%0A    final JBLabel label  = new JBLabel(text);%0A    label.setForeground(JBColor.blue);%0A    label.setCursor(new Cursor(Cursor.HAND_CURSOR));%0A    label.addMouseListener(new MouseAdapter() %7B%0A      @Override%0A      public void mouseClicked(MouseEvent e) %7B%0A        onClick.run();%0A      %7D%0A    %7D);%0A    return label;%0A  %7D%0A
 %0A  JComp

work around link rendering issues (#3199)