package jrex.ui;


import jrex.ui.model.SimpleListModel;
import jrex.ui.model.PivotTableModel;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import java.awt.Window;
import java.util.Collection;

/**
 *
 * @author Mark George <mark.george@otago.ac.nz>
 */
public final class LongToWidePivotDialog extends javax.swing.JDialog {

	private final SimpleListModel repeatingModel = new SimpleListModel();
	private ListMultimap<String, String> columns;

	/**
	 * Creates new form PivotDialog
	 * @param parent The parent window.
	 */
	@SuppressWarnings("unchecked")
	public LongToWidePivotDialog(Window parent) {
		super(parent);
		setModal(true);
		setLocationRelativeTo(parent);
		initComponents();
		lstRepeating.setModel(repeatingModel);
	}

	/**
	 * 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.
	 */
	@SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jLabel1 = new javax.swing.JLabel();
      btnOk = new javax.swing.JButton();
      btnCancel = new javax.swing.JButton();
      scrollPane = new javax.swing.JScrollPane();
      lstRepeating = new javax.swing.JList<>();
      btnHeaders = new javax.swing.JButton();
      btnValues = new javax.swing.JButton();
      txtHeaders = new javax.swing.JTextField();
      txtValues = new javax.swing.JTextField();

      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

      jLabel1.setText("Columns");
      jLabel1.setName("jLabel1"); // NOI18N

      btnOk.setText("OK");
      btnOk.setName("btnOk"); // NOI18N
      btnOk.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnOkActionPerformed(evt);
         }
      });

      btnCancel.setText("Cancel");
      btnCancel.setName("btnCancel"); // NOI18N
      btnCancel.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCancelActionPerformed(evt);
         }
      });

      scrollPane.setName("scrollPane"); // NOI18N

      lstRepeating.setName("lstRepeating"); // NOI18N
      scrollPane.setViewportView(lstRepeating);

      btnHeaders.setText("Header Column");
      btnHeaders.setName("btnHeaders"); // NOI18N
      btnHeaders.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnHeadersActionPerformed(evt);
         }
      });

      btnValues.setText("Value Column");
      btnValues.setName("btnValues"); // NOI18N
      btnValues.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnValuesActionPerformed(evt);
         }
      });

      txtHeaders.setName("txtHeaders"); // NOI18N

      txtValues.setName("txtValues"); // NOI18N

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                     .addComponent(btnHeaders, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(btnValues, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(txtHeaders)
                     .addComponent(txtValues, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)))
               .addGroup(layout.createSequentialGroup()
                  .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(0, 0, Short.MAX_VALUE))
               .addGroup(layout.createSequentialGroup()
                  .addComponent(btnCancel)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addComponent(btnOk)))
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                     .addComponent(btnOk)
                     .addComponent(btnCancel)))
               .addGroup(layout.createSequentialGroup()
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                     .addComponent(btnHeaders)
                     .addComponent(txtHeaders, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                     .addComponent(btnValues)
                     .addComponent(txtValues, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(0, 0, Short.MAX_VALUE)))
            .addContainerGap())
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

   private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
      dispose();
   }//GEN-LAST:event_btnCancelActionPerformed

	@SuppressWarnings("unchecked")
   private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
      columns = ArrayListMultimap.create();
		columns.putAll("repeating", repeatingModel.getAll());
		columns.put("header", txtHeaders.getText());
		columns.put("value", txtValues.getText());
		dispose();
   }//GEN-LAST:event_btnOkActionPerformed

   private void btnHeadersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHeadersActionPerformed
      String headers = lstRepeating.getSelectedValue();
		repeatingModel.removeElement(headers);
		txtHeaders.setText(headers);
   }//GEN-LAST:event_btnHeadersActionPerformed

   private void btnValuesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValuesActionPerformed
      String values = lstRepeating.getSelectedValue();
		repeatingModel.removeElement(values);
		txtValues.setText(values);
   }//GEN-LAST:event_btnValuesActionPerformed


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnCancel;
   private javax.swing.JButton btnHeaders;
   private javax.swing.JButton btnOk;
   private javax.swing.JButton btnValues;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JList<String> lstRepeating;
   private javax.swing.JScrollPane scrollPane;
   private javax.swing.JTextField txtHeaders;
   private javax.swing.JTextField txtValues;
   // End of variables declaration//GEN-END:variables

	@SuppressWarnings("unchecked")
	public ListMultimap<String, String> mapColumns(PivotTableModel model) {
		Collection<String> headers = model.getHeaders();

		repeatingModel.updateItems(headers);

		setVisible(true);
		return columns;
	}
}
