package uk.ac.reload.editor.prefs;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import uk.ac.reload.diva.prefs.UserPrefs;
import uk.ac.reload.dweezil.gui.layout.XYConstraints;
import uk.ac.reload.dweezil.gui.layout.XYLayout;
import uk.ac.reload.dweezil.prefs.PrefsPanel;
import uk.ac.reload.dweezil.util.DweezilUIManager;
import uk.ac.reload.editor.upload.repository.cesga.RepositoryConstantsCesga;
import uk.ac.reload.editor.upload.repository.osami.RepositoryConstantsOsami;
import uk.ac.reload.editor.upload.repository.suma.RepositoryConstantsSuma;

/**
 *
 * @author Gonzalo
 */
public class Upload_PrefsPanel
extends PrefsPanel implements ItemListener
{
    /**
     * A combo box for selecting the repository to be used
     */
    private JComboBox jComboBoxCurrentRepository;
    /**
     * Text field used to set the repository address
     */
    private JTextField jTextFieldAddress;
    /**
     * Text field used to set the repository service name
     */
    private JTextField jTextFieldName;
    /**
     * Text field used to set the repository namespace
     */
    private JTextField jTextFieldNamespace;
    /**
     * The user preferences
     */
    private UserPrefs prefs;
    /**
     * Constructor
     */
    public Upload_PrefsPanel() {
        setLayout(new XYLayout());
        constructFields();
    }

    /**
     * Set up the fields
     */
    private void constructFields() {
        int x = 10;
        int y = 10;


        //The combo box
        JLabel jLabelCurrentRepository = new JLabel("Select repository:");
        add(jLabelCurrentRepository, new XYConstraints(x, y, 0, TEXTBOX_HEIGHT));
        jComboBoxCurrentRepository = new JComboBox();
        jComboBoxCurrentRepository.addItem("Osami");
        jComboBoxCurrentRepository.addItem("Suma");
        jComboBoxCurrentRepository.addItem("Cesga");
        jComboBoxCurrentRepository.addItem("null");
        jComboBoxCurrentRepository.setSelectedItem("null");
        jComboBoxCurrentRepository.addItemListener(this);
        add(jComboBoxCurrentRepository, new XYConstraints(x+160, y, 0, TEXTBOX_HEIGHT));
        

        //Repository Service Address
        JLabel jLabelAddress = new JLabel("RepositoryServices Address:");
        add(jLabelAddress, new XYConstraints(x,y+50,0,TEXTBOX_HEIGHT));
        jTextFieldAddress = new JTextField();
        jTextFieldAddress.setFont(DweezilUIManager.plainFont11);
        add(jTextFieldAddress, new XYConstraints(x+160,y+50,380,TEXTBOX_HEIGHT));

        //Repository Service Name
        JLabel jLabelName = new JLabel("Service Name:");
        add(jLabelName, new XYConstraints(x,y+75,0,TEXTBOX_HEIGHT));
        jTextFieldName = new JTextField();
        jTextFieldName.setFont(DweezilUIManager.plainFont11);
        add(jTextFieldName, new XYConstraints(x+160, y+75, 380, TEXTBOX_HEIGHT));

        //Repository Service Namespace
        JLabel jLabelNamespace = new JLabel("Namespace:");
        add(jLabelNamespace, new XYConstraints(x, y+100, 0, TEXTBOX_HEIGHT));
        jTextFieldNamespace = new JTextField();
        jTextFieldNamespace.setFont(DweezilUIManager.plainFont11);
        add(jTextFieldNamespace, new XYConstraints(x+160, y+100, 380, TEXTBOX_HEIGHT));
    }

    /**
     * Update UserPrefs according to the controls' settings
     */
    @Override
    public void saveToUserPrefs(UserPrefs prefs) {
        System.out.println("saveToUserPrefs "+jTextFieldAddress.getText());
        prefs.putValue(EditorPrefs.DEFAULT_REPOSITORY,(String) jComboBoxCurrentRepository.getSelectedItem());
        if (jComboBoxCurrentRepository.getSelectedItem().equals("Osami")) {
            prefs.putValue(EditorPrefs.REPOSITORY_SERVICE_ADDRESS_OSAMI, jTextFieldAddress.getText());
            prefs.putValue(EditorPrefs.SERVICE_NAME_OSAMI, jTextFieldName.getText());
            prefs.putValue(EditorPrefs.NAMESPACE_OSAMI, jTextFieldNamespace.getText());
            RepositoryConstantsOsami.setValues(jTextFieldAddress.getText(), jTextFieldName.getText(), jTextFieldNamespace.getText());
        } else if (jComboBoxCurrentRepository.getSelectedItem().equals("Suma")) {
            prefs.putValue(EditorPrefs.REPOSITORY_SERVICE_ADDRESS_SUMA, jTextFieldAddress.getText());
            prefs.putValue(EditorPrefs.SERVICE_NAME_SUMA, jTextFieldName.getText());
            prefs.putValue(EditorPrefs.NAMESPACE_SUMA, jTextFieldNamespace.getText());
            RepositoryConstantsSuma.setValues(jTextFieldAddress.getText(), jTextFieldName.getText(), jTextFieldNamespace.getText());
        } else if (jComboBoxCurrentRepository.getSelectedItem().equals("Cesga")) {
            prefs.putValue(EditorPrefs.REPOSITORY_SERVICE_ADDRESS_CESGA, jTextFieldAddress.getText());
            prefs.putValue(EditorPrefs.SERVICE_NAME_CESGA, jTextFieldName.getText());
            prefs.putValue(EditorPrefs.NAMESPACE_CESGA, jTextFieldNamespace.getText());
            RepositoryConstantsCesga.setValues(jTextFieldAddress.getText(), jTextFieldName.getText(), jTextFieldNamespace.getText());
        }
    }

    /**
     * Set the fields' settings from those found in UserPrefs
     */
    @Override
    public void setFields(UserPrefs prefs) {
        // Set value in field
        System.out.println(prefs.getValue(EditorPrefs.REPOSITORY_SERVICE_ADDRESS_OSAMI));
        this.prefs = prefs;
        String defaultRepository = prefs.getValue(EditorPrefs.DEFAULT_REPOSITORY);
        jComboBoxCurrentRepository.setSelectedItem(defaultRepository);
        if (jComboBoxCurrentRepository.getItemCount() == 4) {
            jComboBoxCurrentRepository.removeItem("null");
        }
        System.out.println("Default repository: "+defaultRepository);

        
    }

    @Override
    public void cancel() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            System.out.println(e.getItem() + " seleccionado");
            if (e.getItem().equals("Osami")) {
                jTextFieldAddress.setText(prefs.getValue(EditorPrefs.REPOSITORY_SERVICE_ADDRESS_OSAMI));
                jTextFieldName.setText(prefs.getValue(EditorPrefs.SERVICE_NAME_OSAMI));
                jTextFieldNamespace.setText(prefs.getValue(EditorPrefs.NAMESPACE_OSAMI));
            } else if (e.getItem().equals("Suma")) {
                jTextFieldAddress.setText(prefs.getValue(EditorPrefs.REPOSITORY_SERVICE_ADDRESS_SUMA));
                jTextFieldName.setText(prefs.getValue(EditorPrefs.SERVICE_NAME_SUMA));
                jTextFieldNamespace.setText(prefs.getValue(EditorPrefs.NAMESPACE_SUMA));
            } else if (e.getItem().equals("Cesga")) {
                jTextFieldAddress.setText(prefs.getValue(EditorPrefs.REPOSITORY_SERVICE_ADDRESS_CESGA));
                jTextFieldName.setText(prefs.getValue(EditorPrefs.SERVICE_NAME_CESGA));
                jTextFieldNamespace.setText(prefs.getValue(EditorPrefs.NAMESPACE_CESGA));
            }

        } else if (e.getStateChange() == ItemEvent.DESELECTED) {
            System.out.println(e.getItem() + " deseleccionado");
        }
    }



}
