package net.bytebutcher.burpsendtoextension.gui;

import burp.BurpExtender;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import net.bytebutcher.burpsendtoextension.models.CommandObject;
import net.bytebutcher.burpsendtoextension.models.placeholder.behaviour.CommandSeparatedPlaceholderBehaviour;
import net.bytebutcher.burpsendtoextension.models.placeholder.behaviour.FileSeparatedPlaceholderBehaviour;
import net.bytebutcher.burpsendtoextension.models.placeholder.behaviour.IPlaceholderBehaviour;
import net.bytebutcher.burpsendtoextension.models.placeholder.behaviour.StringSeparatedPlaceholderBehaviour;

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

public class SendToAddAdvancedPlaceholderBehaviourPanel extends JPanel {

    private final CommandObject.Placeholder placeholder;

    private JTextField txtSeparator;
    private JPanel pnlMain;
    private JComboBox cmbSeperator;
    private JLabel lblPlaceholder;

    public SendToAddAdvancedPlaceholderBehaviourPanel(CommandObject.Placeholder placeholder) {
        this.placeholder = placeholder;
        this.add(pnlMain);
        this.lblPlaceholder.setText(placeholder.getName());
        initFields();
        initEventListener();
    }

    private void initFields() {
        IPlaceholderBehaviour placeholderBehaviour = placeholder.getBehaviour();
        if (placeholderBehaviour instanceof StringSeparatedPlaceholderBehaviour) {
            this.txtSeparator.setText(((StringSeparatedPlaceholderBehaviour) placeholderBehaviour).getSeparator());
            this.txtSeparator.setEnabled(true);
            this.cmbSeperator.setSelectedIndex(1);
        } else if (placeholderBehaviour instanceof FileSeparatedPlaceholderBehaviour) {
            this.txtSeparator.setText("");
            this.txtSeparator.setEnabled(false);
            this.cmbSeperator.setSelectedIndex(2);
        } else {
            this.txtSeparator.setText("");
            this.txtSeparator.setEnabled(false);
            this.cmbSeperator.setSelectedIndex(0);
        }
    }

    private void initEventListener() {
        this.cmbSeperator.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (cmbSeperator.getSelectedIndex() == 1) {
                    txtSeparator.setEnabled(true);
                    txtSeparator.setText("");
                } else {
                    txtSeparator.setEnabled(false);
                    txtSeparator.setText("");
                }
            }
        });
    }

    public CommandObject.Placeholder getPlaceholder() {
        switch (cmbSeperator.getSelectedIndex()) {
            case 1:
                placeholder.setBehaviour(new StringSeparatedPlaceholderBehaviour(txtSeparator.getText()));
                break;
            case 2:
                placeholder.setBehaviour(new FileSeparatedPlaceholderBehaviour());
                break;
            default:
                placeholder.setBehaviour(new CommandSeparatedPlaceholderBehaviour());
                break;
        }
        return placeholder;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        pnlMain = new JPanel();
        pnlMain.setLayout(new GridLayoutManager(1, 3, new Insets(0, 0, 0, 0), -1, -1));
        txtSeparator = new JTextField();
        pnlMain.add(txtSeparator, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        cmbSeperator = new JComboBox();
        final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel();
        defaultComboBoxModel1.addElement("split into separate commands");
        defaultComboBoxModel1.addElement("separate by string");
        defaultComboBoxModel1.addElement("merge into file");
        cmbSeperator.setModel(defaultComboBoxModel1);
        pnlMain.add(cmbSeperator, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        lblPlaceholder = new JLabel();
        lblPlaceholder.setText("...");
        pnlMain.add(lblPlaceholder, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

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

}
