/*
 * Copyright (C) 2016 Reece H. Dunn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.xquery.settings;

import uk.co.reecedunn.intellij.plugin.core.ui.SettingsUI;
import uk.co.reecedunn.intellij.plugin.xquery.lang.*;

import javax.swing.*;

@SuppressWarnings({"RedundantIfStatement", "SameParameterValue"})
public class XQueryPropertiesUI implements SettingsUI<XQueryProjectSettings> {
    private JComboBox<XQueryVersion> mVersion;
    private JComboBox<ImplementationItem> mImplementations;
    private JComboBox<ImplementationItem> mImplementationVersions;
    private JComboBox<ImplementationItem> mDialectForXQuery1_0;
    private JComboBox<ImplementationItem> mDialectForXQuery3_0;
    private JComboBox<ImplementationItem> mDialectForXQuery3_1;

    private JPanel mPanel;

    public XQueryPropertiesUI() {
    }

    @Override
    public JPanel getPanel() {
        return mPanel;
    }

    private void populateVersionComboBox(JComboBox<XQueryVersion> control, ImplementationItem source, Versioned filter) {
        if (source == null) {
            return;
        }

        final XQueryVersion selected = (XQueryVersion) control.getSelectedItem();
        boolean found = false;

        control.removeAllItems();
        for (XQueryVersion version : source.getVersions(ImplementationItem.IMPLEMENTATION_DIALECT, filter)) {
            control.addItem(version);
            if (version == selected) {
                control.setSelectedItem(version);
                found = true;
            }
        }
        if (!found) {
            control.setSelectedItem(source.getDefaultVersion(ImplementationItem.IMPLEMENTATION_DIALECT, filter));
        }
    }

    private void populateComboBox(JComboBox<ImplementationItem> control, ImplementationItem source, String filter) {
        control.removeAllItems();
        for (ImplementationItem item : source.getItems(filter)) {
            control.addItem(item);
        }
        control.setSelectedItem(source.getDefaultItem(filter));
    }

    private void populateComboBox(JComboBox<ImplementationItem> control, ImplementationItem source, String filter, XQueryVersion version) {
        if (source == null) {
            return;
        }

        final ImplementationItem selected = (ImplementationItem)control.getSelectedItem();
        boolean found = false;

        control.removeAllItems();
        for (ImplementationItem item : source.getItemsByVersion(filter, XQuery.INSTANCE, version)) {
            control.addItem(item);
            if (selected != null && item.toString().equals(selected.toString())) {
                control.setSelectedItem(item);
                found = true;
            }
        }
        if (!found) {
            control.setSelectedItem(source.getDefaultItemByVersion(filter, XQuery.INSTANCE, version));
        }
    }

    private void createUIComponents() {
        mImplementations = new JComboBox<>();
        mImplementationVersions = new JComboBox<>();
        mVersion = new JComboBox<>();
        mDialectForXQuery1_0 = new JComboBox<>();
        mDialectForXQuery3_0 = new JComboBox<>();
        mDialectForXQuery3_1 = new JComboBox<>();

        mImplementations.setName("Implementation");
        mImplementationVersions.setName("ImplementationVersion");
        mVersion.setName("XQueryVersion");
        mDialectForXQuery1_0.setName("DialectForXQuery1.0");
        mDialectForXQuery3_0.setName("DialectForXQuery3.0");
        mDialectForXQuery3_1.setName("DialectForXQuery3.1");

        mImplementationVersions.addActionListener(e -> {
            final ImplementationItem version = (ImplementationItem)mImplementationVersions.getSelectedItem();
            populateVersionComboBox(mVersion, version, XQuery.INSTANCE);
            populateComboBox(mDialectForXQuery1_0, version, ImplementationItem.IMPLEMENTATION_DIALECT, XQueryVersion.VERSION_1_0);
            populateComboBox(mDialectForXQuery3_0, version, ImplementationItem.IMPLEMENTATION_DIALECT, XQueryVersion.VERSION_3_0);
            populateComboBox(mDialectForXQuery3_1, version, ImplementationItem.IMPLEMENTATION_DIALECT, XQueryVersion.VERSION_3_1);
        });

        mImplementations.addActionListener(e -> {
            final ImplementationItem implementation = (ImplementationItem)mImplementations.getSelectedItem();
            populateComboBox(mImplementationVersions, implementation, ImplementationItem.IMPLEMENTATION_VERSION);
        });

        for (ImplementationItem implementation : Implementations.getImplementations()) {
            mImplementations.addItem(implementation);
        }
    }

    @Override
    public boolean isModified(XQueryProjectSettings settings) {
        if (!mImplementations.getSelectedItem().equals(settings.getImplementationItem())) return true;
        if (!mImplementationVersions.getSelectedItem().equals(settings.getImplementationVersionItem())) return true;
        if (!mVersion.getSelectedItem().equals(settings.getXQueryVersion())) return true;
        if (!mDialectForXQuery1_0.getSelectedItem().equals(settings.getDialectForXQueryVersion(XQuery.INSTANCE.getREC_1_0_20070123()))) return true;
        if (!mDialectForXQuery3_0.getSelectedItem().equals(settings.getDialectForXQueryVersion(XQuery.INSTANCE.getREC_3_0_20140408()))) return true;
        if (!mDialectForXQuery3_1.getSelectedItem().equals(settings.getDialectForXQueryVersion(XQuery.INSTANCE.getREC_3_1_20170321()))) return true;
        return false;
    }

    @Override
    public void apply(XQueryProjectSettings settings) {
        settings.setImplementationItem((ImplementationItem)mImplementations.getSelectedItem());
        settings.setImplementationVersionItem((ImplementationItem)mImplementationVersions.getSelectedItem());
        settings.setXQueryVersion((XQueryVersion)mVersion.getSelectedItem());
        settings.setDialectForXQueryVersion(XQuery.INSTANCE.getREC_1_0_20070123(), (ImplementationItem)mDialectForXQuery1_0.getSelectedItem());
        settings.setDialectForXQueryVersion(XQuery.INSTANCE.getREC_3_0_20140408(), (ImplementationItem)mDialectForXQuery3_0.getSelectedItem());
        settings.setDialectForXQueryVersion(XQuery.INSTANCE.getREC_3_1_20170321(), (ImplementationItem)mDialectForXQuery3_1.getSelectedItem());
    }

    @Override
    public void reset(XQueryProjectSettings settings) {
        mImplementations.setSelectedItem(settings.getImplementationItem());
        mImplementationVersions.setSelectedItem(settings.getImplementationVersionItem());
        mVersion.setSelectedItem(settings.getXQueryVersion());
        mDialectForXQuery1_0.setSelectedItem(settings.getDialectForXQueryVersion(XQuery.INSTANCE.getREC_1_0_20070123()));
        mDialectForXQuery3_0.setSelectedItem(settings.getDialectForXQueryVersion(XQuery.INSTANCE.getREC_3_0_20140408()));
        mDialectForXQuery3_1.setSelectedItem(settings.getDialectForXQueryVersion(XQuery.INSTANCE.getREC_3_1_20170321()));
    }
}

@@ -648,16 +648,57 @@
 tings;%0A%0A
+import com.intellij.openapi.ui.ComboBox;%0A
 import u
@@ -3473,33 +3473,32 @@
 entations = new 
-J
 ComboBox%3C%3E();%0A  
@@ -3529,25 +3529,24 @@
 sions = new 
-J
 ComboBox%3C%3E()
@@ -3566,25 +3566,24 @@
 rsion = new 
-J
 ComboBox%3C%3E()
@@ -3611,33 +3611,32 @@
 XQuery1_0 = new 
-J
 ComboBox%3C%3E();%0A  
@@ -3664,25 +3664,24 @@
 ry3_0 = new 
-J
 ComboBox%3C%3E()
@@ -3717,17 +3717,16 @@
 1 = new 
-J
 ComboBox

Use the IntelliJ ComboBox instead of JComboBox.