package jetbrains.mps.editor.contextActionsTool.pluginSolution.plugin;

/*Generated by MPS */

import java.awt.Color;
import java.util.Set;
import jetbrains.mps.internal.collections.runtime.SetSequence;
import java.util.HashSet;
import javax.swing.JPanel;
import org.jetbrains.annotations.Nullable;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Container;
import javax.swing.SwingUtilities;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.border.Border;

public class Folder {
  private static final Color TITLE_BACKGROUND_COLOR = new Color(200, 200, 200);
  private static final Color TITLE_BORDER_COLOR = new Color(170, 170, 170);

  private static Set<String> ourCollapsedStates = SetSequence.fromSet(new HashSet<String>());

  private String myName;
  private JPanel myComponent;
  private JPanel myItemContainer;
  @Nullable
  private JLabel myTitleComponent;

  public Folder(String name, ViewType viewType) {
    this.myName = name;
    myComponent = new JPanel(new BorderLayout(0, 0));

    if (name != null && name.length() > 0) {
      myTitleComponent = buildTitle(name);
      myTitleComponent.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent event) {
          setCollapsed(!(isCollapsed()));
        }
      });
      myComponent.add(myTitleComponent, BorderLayout.NORTH);
    }
    myItemContainer = new JPanel(new GridLayout(0, 1, 0, 1));
    if (viewType == ViewType.SYMBOLS) {
      myItemContainer.setLayout(new GridLayout(0, 4, 3, 1) {
        @Override
        public Dimension preferredLayoutSize(Container container) {
          updateColumnNumber(container);
          return super.preferredLayoutSize(container);
        }
        @Override
        public Dimension minimumLayoutSize(Container container) {
          updateColumnNumber(container);
          return super.minimumLayoutSize(container);
        }
        @Override
        public void layoutContainer(Container container) {
          updateColumnNumber(container);
          super.layoutContainer(container);
        }
        public void updateColumnNumber(Container container) {
          Container toolComponent = SwingUtilities.getAncestorOfClass(ToolComponent.class, myComponent);
          int maxWidth = toolComponent.getWidth();
          setColumns(1);
          while (super.preferredLayoutSize(container).width <= maxWidth && getColumns() < 100) {
            setColumns(getColumns() + 1);
          }
          setColumns(Math.max(1, getColumns() - 1));
        }
      });
    }

    myItemContainer.setBorder(BorderFactory.createEmptyBorder(6, 3, 12, 3));
    myComponent.add(myItemContainer, BorderLayout.WEST);

    myComponent.setOpaque(false);
    myItemContainer.setOpaque(false);

    setCollapsed(wasCollapsed());
  }

  public void addItem(JComponent item) {
    myItemContainer.add(item);
  }

  public JComponent getComponent() {
    return myComponent;
  }

  public String getName() {
    return myName;
  }

  public boolean isCollapsed() {
    return !(myItemContainer.isVisible());
  }

  public void setCollapsed(boolean collapsed) {
    myItemContainer.setVisible(!(collapsed));
    check_hg2wcn_a1a02(myTitleComponent, collapsed);
    if (collapsed) {
      SetSequence.fromSet(Folder.ourCollapsedStates).addElement(getName());
    } else {
      SetSequence.fromSet(Folder.ourCollapsedStates).removeElement(getName());
    }
  }

  public boolean wasCollapsed() {
    return SetSequence.fromSet(Folder.ourCollapsedStates).contains(getName());
  }

  public static JLabel buildTitle(String text) {
    JLabel label = new JLabel(text);
    Border border = BorderFactory.createMatteBorder(1, 0, 1, 0, TITLE_BORDER_COLOR);
    border = BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(3, 6, 3, 6));
    label.setBorder(border);
    label.setBackground(TITLE_BACKGROUND_COLOR);
    label.setOpaque(true);
    return label;
  }
  private static void check_hg2wcn_a1a02(JLabel checkedDotOperand, Boolean collapsed) {
    if (null != checkedDotOperand) {
      checkedDotOperand.setIcon(new ExpandCollapseIcon(collapsed));
    }

  }
}
