package kiyut.ekspos.pycasa;

import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.photos.AlbumEntry;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 * PycasaAddPhotoPane
 * 
 */
public class PycasaAddPhotoPane extends javax.swing.JPanel {
    
    private ResourceBundle bundle = ResourceBundle.getBundle("kiyut.ekspos.pycasa.PycasaAddPhotoPane");
    
    private List<AlbumEntry> albums;
    private boolean updateAlbumList = false;
    
    /** Creates new form PycasaAddPhotoPane */
    public PycasaAddPhotoPane() {
        initComponents();
        initCustomComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        webAlbumButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        newAlbumRadio = new javax.swing.JRadioButton();
        existingAlbumRadio = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        albumTitlePane = new javax.swing.JPanel();
        albumTitleField = new javax.swing.JTextField();
        albumTitleCombo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        albumDescArea = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        albumPlaceField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();

        setLayout(new java.awt.GridBagLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("CTL_Album.Text"))); // NOI18N
        jPanel1.setLayout(new java.awt.GridLayout(2, 0));

        webAlbumButtonGroup.add(newAlbumRadio);
        newAlbumRadio.setSelected(true);
        newAlbumRadio.setText(bundle.getString("CTL_AddToNewAlbum.Text")); // NOI18N
        jPanel1.add(newAlbumRadio);

        webAlbumButtonGroup.add(existingAlbumRadio);
        existingAlbumRadio.setText(bundle.getString("CTL_AddToExistingAlbum.Text")); // NOI18N
        jPanel1.add(existingAlbumRadio);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel1, gridBagConstraints);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText(bundle.getString("CTL_AlbumTitle.Text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(jLabel1, gridBagConstraints);

        albumTitlePane.setLayout(new java.awt.CardLayout());

        albumTitleField.setColumns(30);
        albumTitlePane.add(albumTitleField, "newAlbum");

        albumTitlePane.add(albumTitleCombo, "existingAlbum");

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(albumTitlePane, gridBagConstraints);

        jLabel2.setText(bundle.getString("CTL_AlbumDescription.Text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
        jPanel2.add(jLabel2, gridBagConstraints);

        albumDescArea.setRows(5);
        jScrollPane1.setViewportView(albumDescArea);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(jScrollPane1, gridBagConstraints);

        jLabel3.setText(bundle.getString("CTL_AlbumPlaceTaken.Text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
        jPanel2.add(jLabel3, gridBagConstraints);

        albumPlaceField.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(albumPlaceField, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
        add(jPanel2, gridBagConstraints);

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 0, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 0, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jPanel3, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea albumDescArea;
    private javax.swing.JTextField albumPlaceField;
    private javax.swing.JComboBox albumTitleCombo;
    private javax.swing.JTextField albumTitleField;
    private javax.swing.JPanel albumTitlePane;
    private javax.swing.JRadioButton existingAlbumRadio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton newAlbumRadio;
    private javax.swing.ButtonGroup webAlbumButtonGroup;
    // End of variables declaration//GEN-END:variables
    
    private void initCustomComponents() {
        albumTitleCombo.setModel(new DefaultComboBoxModel());
        albumTitleCombo.setRenderer(createAlbumTitleRenderer());
        
        ItemListener albumItemListener = new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                if (evt.getStateChange() == ItemEvent.DESELECTED) {
                    return;
                }
                CardLayout cl = (CardLayout)(albumTitlePane.getLayout());
                if (newAlbumRadio.isSelected()) {
                    cl.show(albumTitlePane, "newAlbum");
                    albumDescArea.setText("");
                    albumPlaceField.setText("");
                    albumDescArea.setEditable(true);
                    albumPlaceField.setEditable(true);
                } else {
                    cl.show(albumTitlePane,"existingAlbum");
                    albumDescArea.setEditable(false);
                    albumPlaceField.setEditable(false);
                    
                    // forcing to update
                    int index = albumTitleCombo.getSelectedIndex();
                    albumTitleCombo.setSelectedIndex(-1);
                    albumTitleCombo.setSelectedIndex(index);
                }
            }
        };
        
        newAlbumRadio.addItemListener(albumItemListener);
        existingAlbumRadio.addItemListener(albumItemListener);

        
        ItemListener albumTitleItemListener = new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                if (updateAlbumList) {
                    return;
                }
                if (evt.getStateChange() == ItemEvent.DESELECTED) {
                    return;
                }
                
                int index = albumTitleCombo.getSelectedIndex();
                if (index < 0)  {
                    return;
                }
                
                AlbumEntry album = (AlbumEntry)albumTitleCombo.getSelectedItem();
                albumDescArea.setText(album.getDescription().getPlainText());
                albumPlaceField.setText(album.getLocation());
            }
        };
        
        albumTitleCombo.addItemListener(albumTitleItemListener);
        
        //albumTitleCombo.setSelectedIndex(-1);
        //newAlbumRadio.setSelected(true);
    }
    
    private ListCellRenderer createAlbumTitleRenderer() {
        return new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                if (value != null) {
                    if (value instanceof AlbumEntry ) {
                        String title = ((AlbumEntry)value).getTitle().getPlainText();
                        return super.getListCellRendererComponent( list, title, index, isSelected, cellHasFocus );
                    }
                }
                return super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
            }
        };
    }
    
    
    public String getTitle() {
        return bundle.getString("CTL_Title.Text");
    }
    
    public void setAlbums(List<AlbumEntry> albums) {
        updateAlbumList = true;
        
        this.albums = albums;
        DefaultComboBoxModel comboModel = (DefaultComboBoxModel)albumTitleCombo.getModel();
        comboModel.removeAllElements();
        
        if (albums == null) {
            updateAlbumList = false;
            return;
        }
        
        for (int i=0; i<albums.size(); i++) {
            comboModel.addElement(albums.get(i));
        }
        
        updateAlbumList = false;
        
    }
    
    public AlbumEntry getSelectedAlbum() {
        if (isNewAlbum()) {
            AlbumEntry album = new AlbumEntry();
            album.setTitle(new PlainTextConstruct(albumTitleField.getText()));
            album.setDescription(new PlainTextConstruct(albumDescArea.getText()));
            album.setLocation(albumPlaceField.getText());
            return album;
        }
        return (AlbumEntry)albumTitleCombo.getSelectedItem();
    }
    
    public boolean isNewAlbum() {
        if (newAlbumRadio.isSelected()) {
            return true;
        }
        return false;
    }
}
