package org.jetbrains.plugins.scala.codeInsight.hints;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Method;
import java.util.ResourceBundle;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static org.jetbrains.plugins.scala.codeInsight.ScalaCodeInsightSettings.MAX_PRESENTATION_LENGTH;
import static org.jetbrains.plugins.scala.codeInsight.ScalaCodeInsightSettings.MIN_PRESENTATION_LENGTH;

final class GeneralSettingsPanel {

    private JCheckBox obviousTypesCheckbox;
    private JCheckBox preserveIndentsCheckBox;
    private JPanel panel;
    private SpinnerNumberModel presentationLengthModel;
    private JSpinner presentationLengthSpinner;

    private final Supplier<Boolean> obviousTypesGetter;
    private final Supplier<Boolean> preserveIndentsGetter;
    private final Supplier<Integer> presentationLengthGetter;

    // TODO Just use public fields?
    public GeneralSettingsPanel(Supplier<Boolean> obviousTypesGetter, Consumer<Boolean> obviousTypesSetter,
                                Supplier<Boolean> preserveIndentsGetter, Consumer<Boolean> preserveIndentsSetter,
                                Supplier<Integer> presentationLengthGetter, Consumer<Integer> presentationLengthSetter) {
        this.obviousTypesGetter = obviousTypesGetter;
        this.preserveIndentsGetter = preserveIndentsGetter;
        this.presentationLengthGetter = presentationLengthGetter;
        presentationLengthModel = new SpinnerNumberModel();
        $$$setupUI$$$();
        presentationLengthModel.setMinimum(MIN_PRESENTATION_LENGTH);
        presentationLengthModel.setMaximum(MAX_PRESENTATION_LENGTH);
        presentationLengthModel.setStepSize(1);
        reset();
        obviousTypesCheckbox.addActionListener((e) -> obviousTypesSetter.accept(obviousTypesCheckbox.isSelected()));
        preserveIndentsCheckBox.addActionListener((e) -> preserveIndentsSetter.accept(preserveIndentsCheckBox.isSelected()));
        presentationLengthModel.addChangeListener((e) -> presentationLengthSetter.accept(presentationLengthModel.getNumber().intValue()));
    }

    public void reset() {
        obviousTypesCheckbox.setSelected(obviousTypesGetter.get());
        preserveIndentsCheckBox.setSelected(preserveIndentsGetter.get());
        presentationLengthModel.setValue(presentationLengthGetter.get());
    }

    private void createUIComponents() {
        presentationLengthSpinner = new JSpinner(presentationLengthModel);
        ((JSpinner.DefaultEditor) presentationLengthSpinner.getEditor()).getTextField().setColumns(3);
    }

    public JPanel getPanel() {
        return panel;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        panel = new JPanel();
        panel.setLayout(new GridLayoutManager(4, 4, new Insets(0, 0, 0, 0), -1, -1));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, this.$$$getMessageFromBundle$$$("messages/ScalaCodeInsightBundle", "inlay.hints.limit.hint.length.to"));
        panel.add(label1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        panel.add(presentationLengthSpinner, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label2 = new JLabel();
        this.$$$loadLabelText$$$(label2, this.$$$getMessageFromBundle$$$("messages/ScalaCodeInsightBundle", "inlay.hints.characters"));
        panel.add(label2, new GridConstraints(2, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel.add(spacer1, new GridConstraints(2, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final Spacer spacer2 = new Spacer();
        panel.add(spacer2, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        preserveIndentsCheckBox = new JCheckBox();
        this.$$$loadButtonText$$$(preserveIndentsCheckBox, this.$$$getMessageFromBundle$$$("messages/ScalaCodeInsightBundle", "inlay.hints.respect.vertical.alignment"));
        panel.add(preserveIndentsCheckBox, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        obviousTypesCheckbox = new JCheckBox();
        this.$$$loadButtonText$$$(obviousTypesCheckbox, this.$$$getMessageFromBundle$$$("messages/ScalaCodeInsightBundle", "show.types.even.if.they.are.obvious"));
        panel.add(obviousTypesCheckbox, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        label1.setLabelFor(presentationLengthSpinner);
    }

    private static Method $$$cachedGetBundleMethod$$$ = null;

    private String $$$getMessageFromBundle$$$(String path, String key) {
        ResourceBundle bundle;
        try {
            Class<?> thisClass = this.getClass();
            if ($$$cachedGetBundleMethod$$$ == null) {
                Class<?> dynamicBundleClass = thisClass.getClassLoader().loadClass("com.intellij.DynamicBundle");
                $$$cachedGetBundleMethod$$$ = dynamicBundleClass.getMethod("getBundle", String.class, Class.class);
            }
            bundle = (ResourceBundle) $$$cachedGetBundleMethod$$$.invoke(null, path, thisClass);
        } catch (Exception e) {
            bundle = ResourceBundle.getBundle(path);
        }
        return bundle.getString(key);
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

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

}
