/*
 * Created on Feb 7, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.nasa.jsc.plum;

import gov.nasa.jsc.plum.PlumUtil.DatabaseWindow;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.TreeMap;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

/**
 * @author randrews
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SelectionWindow extends JFrame implements ActionListener{

    private DatabaseWindow parent;
    private Connection conn;
    private int idx;
    private TreeMap selection;
    
    /**
     * @param parent
     * @param conn
     * @param idx
     * @throws java.awt.HeadlessException
     */
    public SelectionWindow(DatabaseWindow parent, Connection conn, int idx)
            throws HeadlessException {
        super("Rack Selection Status");
        this.parent = parent;
        this.conn = conn;
        this.idx = idx;
        
        JPanel panel=(JPanel) getContentPane();
        JPanel names=new JPanel();
        JPanel radio=new JPanel();
        selection=new TreeMap();
        
        try{
            ResultSet racknum=conn.createStatement().executeQuery("select count(idx) from racks");
            racknum.next();int numracks=racknum.getInt(1);
            
            names.setLayout(new GridLayout(numracks+1,1));
            radio.setLayout(new GridLayout(numracks+1,5));
                        
            String[] strs=new String[5];
            strs[0]="R";
            strs[1]="S";
            strs[2]="A";
            strs[3]="O";
            strs[4]="K";
            
            for(int n=0;n!=5;n++)
                radio.add(new JLabel(strs[n]));
            names.add(new JLabel("Rack name"));
            
            ResultSet res=conn.createStatement().executeQuery("select racks.acronym,racks.idx,selection.status from racks,selection where racks.idx=selection.rack and selection.run="+idx+" group by racks.idx order by racks.idx");
            while(res.next())
            {
                String rack=res.getString("acronym");
                Integer value=new Integer(res.getInt("status"));
                Integer rackidx=new Integer(res.getInt("idx"));
                names.add(new JLabel(rack));
                selection.put(rackidx,value);
                
                JRadioButton[] btns=new JRadioButton[5];
                ButtonGroup g=new ButtonGroup();
                RadioListener r=new RadioListener(rackidx);
                for(int n=0;n!=5;n++)
                {
                    btns[n]=new JRadioButton();
                    g.add(btns[n]);
                    btns[n].setActionCommand(strs[n]);
                    btns[n].addActionListener(r);
                    if(value.intValue()==n)
                        btns[n].setSelected(true);
                    radio.add(btns[n]);
                }
            }
            
        } catch(Exception e){
            e.printStackTrace();
        }
        
        JPanel controls=new JPanel();
        controls.setLayout(new BorderLayout());
        controls.add(names,BorderLayout.WEST);
        controls.add(radio,BorderLayout.EAST);
        
        panel.setLayout(new BorderLayout());
        panel.add(new JScrollPane(controls),BorderLayout.CENTER);
        JButton save=new JButton("Save Changes");
        save.addActionListener(this);
        panel.add(save,BorderLayout.SOUTH);
        pack();
    }
    
    private class RadioListener implements ActionListener{
        private Integer rack;
        
        public RadioListener(Integer rack){
            this.rack=rack;
        }
        
        public void actionPerformed(ActionEvent e) {
            String newvalue=e.getActionCommand();
            Integer i=null;
            if(newvalue.equals("R"))
                i=new Integer(0);
            else if(newvalue.equals("S"))
                i=new Integer(1);
            else if(newvalue.equals("A"))
                i=new Integer(2);
            else if(newvalue.equals("O"))
                i=new Integer(3);
            else if(newvalue.equals("K"))
                i=new Integer(4);
            
            if(i!=null)
                selection.put(rack,i);
        }
    }
    
    public void actionPerformed(ActionEvent arg0) {
        try{
            conn.createStatement().execute("delete from selection where run="+idx);
            Iterator i=selection.keySet().iterator();
            while(i.hasNext())
            {
                Integer key=(Integer) i.next();
                Integer value=(Integer) selection.get(key);
                conn.createStatement().execute("insert into selection (run,rack,status) values ("+idx+","+key.intValue()+","+value.intValue()+")");
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }

}
