package org.antlr.intellij.plugin.preview;

import com.intellij.ui.components.JBPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.atn.AmbiguityInfo;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.gui.TreeViewer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class ShowAmbigTreesDialog extends JDialog {
	public static final int MAX_PHRASE_WIDTH = 25;
	private JPanel contentPane;
	private JButton buttonOK;
	protected JScrollPane treeScrollPane;
	public List<ParserRuleContext> ambiguousParseTrees;
	public TreeViewer[] treeViewers;
	public PreviewState previewState;

	public ShowAmbigTreesDialog() {
		$$$setupUI$$$();
		setContentPane(contentPane);
		setModal(true);
		getRootPane().setDefaultButton(buttonOK);

		buttonOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onOK();
			}
		});
	}

	public void setTrees(PreviewState previewState, AmbiguityInfo ambigEvent) {
		Parser parser = previewState.parsingResult.parser;
		int startRuleIndex = parser.getRuleIndex(previewState.startRuleName);
		List<ParserRuleContext> ambiguousParseTrees =
			Parser.getAmbiguousParseTrees(parser, ambigEvent, startRuleIndex);
		this.previewState = previewState;
		this.ambiguousParseTrees = ambiguousParseTrees;
		if (ambiguousParseTrees != null) {
			int numTrees = ambiguousParseTrees.size();
			String phrase = ambigEvent.input.getText(Interval.of(ambigEvent.startIndex, ambigEvent.stopIndex));
			if (phrase.length() > MAX_PHRASE_WIDTH) {
				phrase = phrase.substring(0, MAX_PHRASE_WIDTH) + "...";
			}
			setTitle(numTrees + " Interpretations of Ambiguous Input Phrase: " + phrase);
			treeViewers = new TreeViewer[ambiguousParseTrees.size()];
			int numComponentsIncludingSeparators = 2 * numTrees - 1;
			JBPanel panelOfTrees = new JBPanel();
			panelOfTrees.setLayout(new BoxLayout(panelOfTrees, BoxLayout.X_AXIS));
			for (int i = 0; i < numTrees; i++) {
				if (i > 0) {
					panelOfTrees.add(new JSeparator(JSeparator.VERTICAL));
				}
				ParserRuleContext ctx = ambiguousParseTrees.get(i);
				String[] ruleNames = previewState.parsingResult.parser.getRuleNames();
				treeViewers[i] = new TrackpadZoomingTreeView(null, null);
//				treeViewers[i] = new TreeViewer(Arrays.asList(ruleNames), ctx);
				treeViewers[i].setTreeTextProvider(new AltLabelTextProvider(previewState.parsingResult.parser, previewState.g));
				treeViewers[i].setTree(ctx);
				panelOfTrees.add(treeViewers[i]);
			}

			// Wrap tree viewer components in scroll pane
			treeScrollPane.setViewportView(panelOfTrees);
		}
	}

	private void onOK() {
// add your code here
		dispose();
	}

	/**
	 * Method generated by IntelliJ IDEA GUI Designer
	 * >>> IMPORTANT!! <<<
	 * DO NOT edit this method OR call it in your code!
	 *
	 * @noinspection ALL
	 */
	private void $$$setupUI$$$() {
		contentPane = new JPanel();
		contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
		final JPanel panel1 = new JPanel();
		panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
		contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
		final Spacer spacer1 = new Spacer();
		panel1.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
		final JPanel panel2 = new JPanel();
		panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
		panel1.add(panel2, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
		buttonOK = new JButton();
		buttonOK.setText("OK");
		panel2.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
		treeScrollPane = new JScrollPane();
		contentPane.add(treeScrollPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
	}

	/**
	 * @noinspection ALL
	 */
	public JComponent $$$getRootComponent$$$() {
		return contentPane;
	}
}

@@ -1009,11 +1009,12 @@
 dal(
-tru
+fals
 e);%0A

make ambig trees nonmodal