package gitflow.ui;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.CollectionComboBoxModel;
import gitflow.GitflowInitOptions;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

/**
 * @author Andreas Vogler (Andreas.Vogler@geneon.de)
 */

public class GitflowInitOptionsDialog extends DialogWrapper {
    private JPanel contentPane;
    private JCheckBox useNonDefaultConfigurationCheckBox;

    private JComboBox productionBranchComboBox;
    private JComboBox developmentBranchComboBox;
    private JTextField featurePrefixTextField;
    private JTextField releasePrefixTextField;
    private JTextField hotfixPrefixTextField;
    private JTextField supportPrefixTextField;
    private JTextField versionPrefixTextField;
    private JTextField bugfixPrefixTextField;

    public GitflowInitOptionsDialog(Project project, List<String> localBranches) {
        super(project);

        setTitle("Options for gitflow init");

        productionBranchComboBox.setModel(new CollectionComboBoxModel(localBranches));
        developmentBranchComboBox.setModel(new CollectionComboBoxModel(localBranches));

        init();
        useNonDefaultConfigurationCheckBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                enableFields(e.getStateChange()==ItemEvent.SELECTED);
            }
        });
    }

    private void enableFields(boolean enable) {
        productionBranchComboBox.setEnabled(enable);
        developmentBranchComboBox.setEnabled(enable);
        featurePrefixTextField.setEnabled(enable);
        releasePrefixTextField.setEnabled(enable);
        hotfixPrefixTextField.setEnabled(enable);
        supportPrefixTextField.setEnabled(enable);
        bugfixPrefixTextField.setEnabled(enable);
        versionPrefixTextField.setEnabled(enable);
    }

    public boolean useNonDefaultConfiguration()
    {
        return useNonDefaultConfigurationCheckBox.isSelected();
    }

    public GitflowInitOptions getOptions()
    {
        GitflowInitOptions options = new GitflowInitOptions();

        options.setUseDefaults(!useNonDefaultConfigurationCheckBox.isSelected());
        options.setProductionBranch((String) productionBranchComboBox.getSelectedItem());
        options.setDevelopmentBranch((String) developmentBranchComboBox.getSelectedItem());
        options.setFeaturePrefix(featurePrefixTextField.getText());
        options.setReleasePrefix(releasePrefixTextField.getText());
        options.setHotfixPrefix(hotfixPrefixTextField.getText());
        options.setBugfixPrefix(bugfixPrefixTextField.getText());
        options.setSupportPrefix(supportPrefixTextField.getText());
        options.setVersionPrefix(versionPrefixTextField.getText());

        return options;
    }

    @Nullable
    @Override
    protected ValidationInfo doValidate() {
        String message = "Please fill all branch names and prefixes";

        if(useNonDefaultConfiguration()) {
            if(productionBranchComboBox.getSelectedItem().equals(developmentBranchComboBox.getSelectedItem())) {
                return new ValidationInfo("Production and development branch must be distinct branches", developmentBranchComboBox);
            }
            if (StringUtil.isEmptyOrSpaces(featurePrefixTextField.getText())) {
                return new ValidationInfo(message, featurePrefixTextField);
            }
            if (StringUtil.isEmptyOrSpaces(releasePrefixTextField.getText())) {
                return new ValidationInfo(message, releasePrefixTextField);
            }
            if (StringUtil.isEmptyOrSpaces(hotfixPrefixTextField.getText())) {
                return new ValidationInfo(message, hotfixPrefixTextField);
            }
            if (StringUtil.isEmptyOrSpaces(supportPrefixTextField.getText())) {
                return new ValidationInfo(message, supportPrefixTextField);
            }
            if (StringUtil.isEmptyOrSpaces(bugfixPrefixTextField.getText())) {
                return new ValidationInfo(message, bugfixPrefixTextField);
            }
        }

        return null;
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        return contentPane;
    }
}

@@ -416,24 +416,49 @@
 emListener;%0A
+import java.util.Arrays;%0A
 import java.
@@ -1062,16 +1062,57 @@
 Field;%0A%0A
+    private List%3CString%3E localBranches;%0A%0A
     publ
@@ -1161,32 +1161,33 @@
 t, List%3CString%3E 
+_
 localBranches) %7B
@@ -1210,16 +1210,56 @@
 roject);
+%0A        localBranches = _localBranches;
 %0A%0A      
@@ -1298,25 +1298,24 @@
 init%22);%0A
-%0A
         
 producti
@@ -1310,172 +1310,38 @@
     
-productionBranchComboBox.setModel(new CollectionComboBoxModel(localBranches));%0A        developmentBranchComboBox.setModel(new CollectionComboBoxModel(localBranches)
+setLocalBranchesComboBox(false
 );%0A%0A
@@ -1638,38 +1638,594 @@
 oid 
-enableFields(boolean enable) %7B
+setLocalBranchesComboBox(boolean isNonDefault)%7B%0A        if (isNonDefault)%7B%0A            developmentBranchComboBox.setModel(new CollectionComboBoxModel(localBranches));%0A            productionBranchComboBox.setModel(new CollectionComboBoxModel(localBranches));%0A        %7D else %7B%0A            developmentBranchComboBox.setModel(new CollectionComboBoxModel(Arrays.asList(%22develop%22)));%0A            productionBranchComboBox.setModel(new CollectionComboBoxModel(Arrays.asList(%22master%22)));%0A        %7D%0A    %7D%0A%0A    private void enableFields(boolean enable) %7B%0A        setLocalBranchesComboBox(enable);%0A
 %0A   

Fix init settings shown in UI are misleading (do not match default) #283