/*
 * Copyright (c) 2013 David Boissier
 *
 * 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 org.codinjutsu.tools.mongo.view;

import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.components.JBCheckBox;
import org.codinjutsu.tools.mongo.view.model.JsonDataType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public abstract class AbstractAddDialog extends DialogWrapper {
    protected static final Map<JsonDataType, TextFieldWrapper> UI_COMPONENT_BY_JSON_DATATYPE = new HashMap<JsonDataType, TextFieldWrapper>();


    static {

        UI_COMPONENT_BY_JSON_DATATYPE.put(JsonDataType.STRING, new StringFieldWrapper());
        UI_COMPONENT_BY_JSON_DATATYPE.put(JsonDataType.BOOLEAN, new BooleanFieldWrapper());
        UI_COMPONENT_BY_JSON_DATATYPE.put(JsonDataType.NUMBER, new StringFieldWrapper());
        UI_COMPONENT_BY_JSON_DATATYPE.put(JsonDataType.NULL, new NullFieldWrapper());
        UI_COMPONENT_BY_JSON_DATATYPE.put(JsonDataType.OBJECT, new StringFieldWrapper());
        UI_COMPONENT_BY_JSON_DATATYPE.put(JsonDataType.ARRAY, new StringFieldWrapper());
    }

    protected final MongoEditionPanel mongoEditionPanel;
    protected JsonDataType currentDataType = null;
    protected TextFieldWrapper currentEditor = null;


    public AbstractAddDialog(MongoEditionPanel mongoEditionPanel) {
        super(mongoEditionPanel, true);
        this.mongoEditionPanel = mongoEditionPanel;
    }

    protected void initCombo(final ComboBox combobox, final JPanel parentPanel) {
        combobox.setModel(new DefaultComboBoxModel(JsonDataType.values()));
        combobox.setRenderer(new ColoredListCellRenderer() {

            @Override
            protected void customizeCellRenderer(JList jList, Object o, int i, boolean b, boolean b2) {
                append(((JsonDataType) o).type);
            }
        });

        combobox.setSelectedItem(null);
        combobox.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent itemEvent) {
                JsonDataType selectedType = (JsonDataType) combobox.getSelectedItem();
                currentDataType = selectedType;
                currentEditor = UI_COMPONENT_BY_JSON_DATATYPE.get(selectedType);

                parentPanel.invalidate();
                parentPanel.removeAll();
                parentPanel.add(currentEditor.getComponent(), BorderLayout.CENTER);
                parentPanel.validate();
            }
        });

        combobox.setSelectedItem(JsonDataType.STRING);
    }

    public JsonDataType getJsonDataType() {
        return currentDataType;
    }

    public abstract String getValue();

    public interface TextFieldWrapper<T extends JComponent> {

        String getValue();

        T getComponent();
    }

    protected static class StringFieldWrapper implements TextFieldWrapper {

        private final JTextField component;

        protected StringFieldWrapper() {
            component = new JTextField();
        }

        @Override
        public String getValue() {
            return component.getText();
        }

        public JTextField getComponent() {
            return component;
        }
    }

    protected static class BooleanFieldWrapper implements TextFieldWrapper {

        private final JBCheckBox component;

        protected BooleanFieldWrapper() {
            component = new JBCheckBox();
        }

        @Override
        public String getValue() {
            return String.valueOf(component.isSelected());
        }

        public JBCheckBox getComponent() {
            return component;
        }
    }

    protected static class NullFieldWrapper implements TextFieldWrapper {

        private final JLabel component;

        protected NullFieldWrapper() {
            component = new JLabel("null");
        }

        @Override
        public String getValue() {
            return component.getText();
        }


        public JLabel getComponent() {
            return component;
        }
    }
}

@@ -3040,16 +3040,55 @@
 edType);
+%0A                currentEditor.reset();
 %0A%0A      
@@ -3612,24 +3612,47 @@
 Component();
+%0A%0A        void reset();
 %0A    %7D%0A%0A    
@@ -4041,32 +4041,126 @@
 onent;%0A        %7D
+%0A%0A        @Override%0A        public void reset() %7B%0A            component.setText(%22%22);%0A        %7D
 %0A    %7D%0A%0A    prot
@@ -4566,32 +4566,133 @@
 onent;%0A        %7D
+%0A%0A        @Override%0A        public void reset() %7B%0A            component.setSelected(false);%0A        %7D
 %0A    %7D%0A%0A    prot
@@ -4997,16 +4997,75 @@
      %7D%0A%0A
+        @Override%0A        public void reset() %7B%0A%0A        %7D%0A
 %0A       

clear text field when changing datatype in combobox