/*
 * Created on Feb 6, 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.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
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 ConstraintWindow extends JFrame implements ActionListener{

    private DatabaseWindow parent;
    private Connection conn;
    private String name;
    private TreeMap racks,triggers;
    
    private JButton commentbtn,savebtn;
    
    /**
     * @param parent
     * @param conn
     * @param name
     * @throws java.awt.HeadlessException
     */
    public ConstraintWindow(DatabaseWindow parent, Connection conn, String name)
            throws HeadlessException {
        super(name);
        this.parent = parent;
        this.conn = conn;
        this.name = name;
        
        savebtn=new JButton("Save");
        commentbtn=new JButton("Comments");
        savebtn.addActionListener(this);
        commentbtn.addActionListener(this);
        
        JPanel checkpanel=new JPanel();
        JPanel rackpanel=new JPanel();
        JPanel namepanel=new JPanel();
        
        rackpanel.setLayout(new BorderLayout());
        
        rackpanel.add(checkpanel,BorderLayout.CENTER);
        rackpanel.add(namepanel,BorderLayout.WEST);
        
        racks=new TreeMap();
        triggers=new TreeMap();
        try{
            TreeMap consracks=new TreeMap();
            ResultSet cons=conn.createStatement().executeQuery("select * from constraints where name=\""+name+"\"");
            while(cons.next()){
                consracks.put(cons.getObject("rack"),cons.getObject("trigger"));
            }
            
            ResultSet racknum=conn.createStatement().executeQuery("select count(idx) from racks");
            racknum.next();int num=racknum.getInt(1);
            checkpanel.setLayout(new GridLayout(num+1,2));
            namepanel.setLayout(new GridLayout(num+1,1));
            
            checkpanel.add(new JLabel("Required?"));
            checkpanel.add(new JLabel("Trigger?"));
            namepanel.add(new JLabel("Rack Name"));
            
            ResultSet res=conn.createStatement().executeQuery("select acronym,idx from racks");
            while(res.next()){
                Integer idx=new Integer(res.getInt("idx"));
                namepanel.add(new JLabel(res.getString("acronym")));
                JCheckBox rack=new JCheckBox();
                JCheckBox trigger=new JCheckBox();
                if(consracks.containsKey(idx))
                {
                    rack.setSelected(true);
                    Boolean b=(Boolean) consracks.get(idx);
                    if(b.booleanValue())
                        trigger.setSelected(true);
                }
                checkpanel.add(rack);
                checkpanel.add(trigger);
                
                racks.put(idx,rack);
                triggers.put(idx,trigger);
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        
        JPanel btnpanel=new JPanel();
        btnpanel.setLayout(new GridLayout(2,1));
        btnpanel.add(savebtn);
        btnpanel.add(commentbtn);
        
        JPanel panel=(JPanel) getContentPane();
        panel.setLayout(new BorderLayout());
        
        panel.add(btnpanel,BorderLayout.SOUTH);
        panel.add(new JScrollPane(rackpanel),BorderLayout.CENTER);
        
        pack();
    }

    public void actionPerformed(ActionEvent arg0) {
        try{
            conn.createStatement().execute("delete from constraints where name=\""+name+"\"");
            Iterator i=racks.keySet().iterator();
            while(i.hasNext())
            {
                Integer rack=(Integer) i.next();
                JCheckBox reqd=(JCheckBox) racks.get(rack);
                if(reqd.isSelected()==false)continue;
                JCheckBox trig=(JCheckBox) triggers.get(rack);
                int t=(trig.isSelected()?1:0);
//                System.out.println(rack+" "+trig.isSelected());
                conn.createStatement().execute("insert into constraints (name,rack,trigger) values (\""+name+"\","+rack.intValue()+","+t+")");
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}
