package com.mistraltech.smogen.plugin;

import com.intellij.openapi.project.Project;

import javax.swing.*;

public class SmogenConfiguration {

    private final Project project;
    private JPanel mainPanel;
    private JTextField matcherClassNameSuffixTextField;
    private JTextField matcherClassNamePrefixTextField;
    private JTextField factoryMethodSuffixTextField;
    private JTextField templateFactoryMethodSuffixTextField;
    private JTextField setterPrefixTextField;
    private JTextField setterSuffixTextField;
    private JCheckBox useReflectingPropertyMatcherCheckBox;
    private JCheckBox generateTemplateFactoryMethodCheckBox;
    private JCheckBox makeMethodParametersFinalCheckBox;
    private JTextField baseClassTextField;
    private JCheckBox makeExtensibleCheckBox;

    private String currentMatcherClassNamePrefix;
    private String currentMatcherClassNameSuffix;
    private String currentFactoryMethodSuffix;
    private String currentTemplateFactoryMethodSuffix;
    private String currentSetterPrefix;
    private String currentSetterSuffix;
    private boolean currentUseReflectingPropertyMatcher;
    private boolean currentGenerateTemplateFactoryMethod;
    private boolean currentMakeMethodParametersFinal;
    private String currentBaseClass;
    private boolean currentMakeExtensible;

    public SmogenConfiguration(Project project) {
        this.project = project;
        init();
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public boolean isModified() {
        return !(
                matcherClassNamePrefixTextField.getText().equals(currentMatcherClassNamePrefix) &&
                        matcherClassNameSuffixTextField.getText().equals(currentMatcherClassNameSuffix) &&
                        factoryMethodSuffixTextField.getText().equals(currentFactoryMethodSuffix) &&
                        templateFactoryMethodSuffixTextField.getText().equals(currentTemplateFactoryMethodSuffix) &&
                        setterPrefixTextField.getText().equals(currentSetterPrefix) &&
                        setterSuffixTextField.getText().equals(currentSetterSuffix) &&
                        useReflectingPropertyMatcherCheckBox.isSelected() == currentUseReflectingPropertyMatcher &&
                        generateTemplateFactoryMethodCheckBox.isSelected() == currentGenerateTemplateFactoryMethod &&
                        makeMethodParametersFinalCheckBox.isSelected() == currentMakeMethodParametersFinal &&
                        baseClassTextField.getText().equals(currentBaseClass) &&
                        makeExtensibleCheckBox.isSelected() == currentMakeExtensible
        );
    }

    private void init() {
        ConfigurationProperties properties = new ConfigurationProperties(project);

        currentMatcherClassNamePrefix = properties.getMatcherClassNamePrefix();
        currentMatcherClassNameSuffix = properties.getMatcherClassNameSuffix();
        currentFactoryMethodSuffix = properties.getFactoryMethodSuffix();
        currentTemplateFactoryMethodSuffix = properties.getTemplateFactoryMethodSuffix();
        currentSetterPrefix = properties.getSetterPrefix();
        currentSetterSuffix = properties.getSetterSuffix();
        currentUseReflectingPropertyMatcher = properties.isUseReflectingPropertyMatcher();
        currentGenerateTemplateFactoryMethod = properties.isGenerateTemplateFactoryMethod();
        currentMakeMethodParametersFinal = properties.isMakeMethodParametersFinal();
        currentBaseClass = properties.getBaseClass();
        currentMakeExtensible = properties.isMakeExtensible();

        reset();
    }

    public void save() {
        ConfigurationProperties properties = new ConfigurationProperties(project);

        currentMatcherClassNamePrefix = matcherClassNamePrefixTextField.getText();
        currentMatcherClassNameSuffix = matcherClassNameSuffixTextField.getText();
        currentFactoryMethodSuffix = factoryMethodSuffixTextField.getText();
        currentTemplateFactoryMethodSuffix = templateFactoryMethodSuffixTextField.getText();
        currentSetterPrefix = setterPrefixTextField.getText();
        currentSetterSuffix = setterSuffixTextField.getText();
        currentUseReflectingPropertyMatcher = useReflectingPropertyMatcherCheckBox.isSelected();
        currentGenerateTemplateFactoryMethod = generateTemplateFactoryMethodCheckBox.isSelected();
        currentMakeMethodParametersFinal = makeMethodParametersFinalCheckBox.isSelected();
        currentBaseClass = baseClassTextField.getText();
        currentMakeExtensible = makeExtensibleCheckBox.isSelected();

        properties.setMatcherClassNamePrefix(currentMatcherClassNamePrefix);
        properties.setMatcherClassNameSuffix(currentMatcherClassNameSuffix);
        properties.setFactoryMethodSuffix(currentFactoryMethodSuffix);
        properties.setTemplateFactoryMethodSuffix(currentTemplateFactoryMethodSuffix);
        properties.setSetterPrefix(currentSetterPrefix);
        properties.setSetterSuffix(currentSetterSuffix);
        properties.setUseReflectingPropertyMatcher(currentUseReflectingPropertyMatcher);
        properties.setGenerateTemplateFactoryMethod(currentGenerateTemplateFactoryMethod);
        properties.setMakeMethodParametersFinal(currentMakeMethodParametersFinal);
        properties.setBaseClass(currentBaseClass);
        properties.setMakeExtensible(currentMakeExtensible);
    }

    public void reset() {
        matcherClassNamePrefixTextField.setText(currentMatcherClassNamePrefix);
        matcherClassNameSuffixTextField.setText(currentMatcherClassNameSuffix);
        factoryMethodSuffixTextField.setText(currentFactoryMethodSuffix);
        templateFactoryMethodSuffixTextField.setText(currentTemplateFactoryMethodSuffix);
        setterPrefixTextField.setText(currentSetterPrefix);
        setterSuffixTextField.setText(currentSetterSuffix);
        useReflectingPropertyMatcherCheckBox.setSelected(currentUseReflectingPropertyMatcher);
        generateTemplateFactoryMethodCheckBox.setSelected(currentGenerateTemplateFactoryMethod);
        makeMethodParametersFinalCheckBox.setSelected(currentMakeMethodParametersFinal);
        baseClassTextField.setText(currentBaseClass);
        makeExtensibleCheckBox.setSelected(currentMakeExtensible);
    }
}

@@ -100,16 +100,95 @@
 swing.*;
+%0Aimport javax.swing.event.ChangeEvent;%0Aimport javax.swing.event.ChangeListener;
 %0A%0Apublic
@@ -3700,22 +3700,494 @@
         
-reset(
+generateTemplateFactoryMethodCheckBox.addChangeListener(new ChangeListener() %7B%0A            @Override%0A            public void stateChanged(ChangeEvent e) %7B%0A                onGenerateTemplateFactoryMethodChange();%0A            %7D%0A        %7D);%0A%0A        onGenerateTemplateFactoryMethodChange();%0A%0A        reset();%0A    %7D%0A%0A    private void onGenerateTemplateFactoryMethodChange() %7B%0A        templateFactoryMethodSuffixTextField.setEnabled(generateTemplateFactoryMethodCheckBox.isSelected()
 );%0A    %7D

Disable template factory method suffix field in configuration dialog when template factory method generation checkbox is not selected.