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

import java.io.FileWriter;
import java.sql.Connection;
import java.sql.ResultSet;

/**
 * @author randrews
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class InterfaceReport {
    private Connection conn;
    private String filename;
    private String[] iflabels,ifrack,ifloc;
    /**
     * @param conn
     * @param filename
     */
    public InterfaceReport(Connection conn, String filename) {
        super();
        this.conn = conn;
        this.filename = filename;
        initArrays();
        try {
            toFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     */
    private void initArrays() {
        iflabels=new String[9];
        ifrack=new String[9];
        ifloc=new String[9];
        
        int n=0;
        iflabels[n]="Low Temp Loop";
        ifrack[n]="interface_LTL";
        ifloc[n++]="LTL";
        
        iflabels[n]="Moderate Temp Loop";
        ifrack[n]="interface_MTL";
        ifloc[n++]="MTL";
        
        iflabels[n]="ARIS";
        ifrack[n]="interface_ARIS";
        ifloc[n++]="ARIS";
        
        iflabels[n]="Vacuum-R";
        ifrack[n]="interface_VacuumR";
        ifloc[n++]="VacuumR";
        
        iflabels[n]="Vacuum-E";
        ifrack[n]="interface_VacuumE";
        ifloc[n++]="VacuumE";
        
        iflabels[n]="Gaseous Helium (He)";
        ifrack[n]="interface_He";
        ifloc[n++]="He";
        
        iflabels[n]="Gaseous Nitrogen (GN2)";
        ifrack[n]="interface_N2";
        ifloc[n++]="N2";
        
        iflabels[n]="Gaseous Carbon Dioxide (CO2)";
        ifrack[n]="interface_CO2";
        ifloc[n++]="CO2";
        
        iflabels[n]="Gaseous Argon (Ar)";
        ifrack[n]="interface_Ar";
        ifloc[n++]="Ar";        
    }

    /**
     * @throws Exception
     * 
     */
    private void toFile() throws Exception {
        FileWriter out=new FileWriter(filename);
        out.write("<html><head><title>Interface Report</title></head>");
        out.write("<body>\n");
        out.write("<center><h1>Interface Report</h2></center>\n");
        ResultSet res=conn.createStatement().executeQuery("select * from racks");
        while(res.next())
        {
            ResultSet loc=conn.createStatement().executeQuery("select * from locations where idx="+res.getInt("location"));
            loc.next();
            out.write("<hr>\n");
            out.write("Payload: "+res.getString("acronym")+"<br>\n");
            out.write("Location: "+loc.getString("name")+"<br>\n");
            out.write("<table>\n");
            out.write("<tr><td></td><td>Required by payload:</td><td>Available at location:</td><td></td></tr>\n");
            for(int n=0;n!=iflabels.length;n++)
            {
                boolean r=res.getBoolean(ifrack[n]);
                boolean l=loc.getBoolean(ifloc[n]);
                out.write("<tr><td>"+iflabels[n]+"</td><td>");
                if(r)out.write("X");
                out.write("</td><td>");
                if(l)out.write("X");
                out.write("</td><td>");
                if(r && !l)
                    out.write("<b>Warning! Required interface is not available.</b>");
                out.write("</td></tr>\n");
            }
            out.write("</table>\n");
        }
//        out.write("<hr>\n");
        out.write("</body></html>");
        out.close();
    }
}
