import java.sql.*;
import java.util.Properties;
import java.lang.Exception;

public class dataBase
{
    /** Rut del paciente sin dígito verificativo */
    protected String rut1;
    /** Dígito verificativo del rut del paciente */
    protected String rut2;
    /** Nombre del paciente */
    protected String name;
    /** Fecha de nacimiento del paciente*/
    protected int nacimientoD;
    /** Fecha de nacimiento del paciente*/
    protected int nacimientoM;
    /** Fecha de nacimiento del paciente*/
    protected int nacimientoA;
    /**Sexo del paciente */
    protected String sex;
    /** Dirección del paciente */
    protected String address;
    /** Ciudad donde vive el paciente */
    protected String city;
    /** Pimer Teléfono del paciente */
    protected int fono1;
    /** Segundo Teléfono del paciente */
    protected int fono2;
    /** Previsión del paciente */
    protected String prevision;
    /** Nombre del archivo donde se encuentran los diagnósticos */
    protected String diagnosticFile;
    /** Nombre del archivo donde se encuentran los tratamientos */
    protected String treatmentFile;
    /** Nombre del archivo donde se encuentran los comentarios */
    protected String commentaryFile;
    /** Nombre del archivo donde se encuentran los signos vitales */
    protected String vitalSignsFile;

    /** Contiene la conexión a la Base de Datos */
    Connection conn;

    /**
        Constructor del objeto dataBase sobre el cual se aplican todos
        los metodos que manejan la base de datos. Los atributos de este
        objeto son los distintos datos que se manejan.
    */
    public dataBase()
    {
        try
        {
            // Inicialización de atributos del objeto
            fono1 = fono2 = 0;
            //sex = null;
            //rut2 = name = address = city = prevision = null;
            //diagnosticFile = treatmentFile = commentaryFile = vitalSignsFile = null;

            Class.forName("org.postgresql.Driver");

            String url = "jdbc:postgresql://localhost:5432/Clinic";
            Properties props = new Properties();
            props.setProperty("user","postgres");
            props.setProperty("password","3.141592653");
            conn = DriverManager.getConnection(url, props);
        }
        catch(Exception e)
        {
            System.out.println("ERROR DE APERTURA O CONEXION A BASE DE DATOS");
            System.out.println(e.toString());
        }
}

    /**
        Este método obtiene los datos personales del paciente y setea los atributos
        correspondientes del objeto con estos.
        @param rut El rut del paciente
        @return true - Si existe el paciente. false - Si no existe el paciente o si hay
        error en la obtención de los datos.
    */
    public boolean getPersonalData(String rut) {
        try
        {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(
            "SELECT d.rut1, d.rut2, d.nombre, d.sexo, d.nacimientoD, d.nacimientoM, d.nacimientoA, d.fono1, d.fono2, "
            + "d.calle, d.ciudad, d.prevision "
            + "FROM datos d WHERE rut1 = " + rut );

            if(rs.next()) {
                rut1 = rs.getString(1);
                rut2 = rs.getString(2);
                name = rs.getString(3);
                sex = rs.getString(4);
                nacimientoD = rs.getInt(5);
                nacimientoM = rs.getInt(6);
                nacimientoA = rs.getInt(7);
                fono1 = rs.getInt(8);
                fono2 = rs.getInt(9);
                address = rs.getString(10);
                city = rs.getString(11);
                prevision = rs.getString(12);

                rs.close();
                st.close();
                return true;
            }
            else {
                rs.close();
                st.close();
                System.out.println("NO EXISTE EL PACIENTE");
                return false;
            }
        }
        catch(Exception e)
        {
            System.out.println("ERROR EN OBTENCIÓN DE DATOS PERSONALES DEL PACIENTE");
            System.out.println(e.toString());
            return false;
        }
    }

    /**
        Este método obtiene los nombres de los archivos donde se encuentran los
        datos objetivos del paciente y setea los atributos correspondientes del objeto
        con estos.
        @param rut El rut del paciente
        @return true - Si existe el paciente. false - Si no existe el paciente o si hay
        error en la obtención de los datos.
    */
    public boolean getFilesData(String rut)
    {
        try
        {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(
            "SELECT d.diagnostico, d.tratamiento, d.comentario, d.tabla "
            + "FROM datos d WHERE rut1 = " + rut );

            if(rs.next()) {
                diagnosticFile = rs.getString(1);
                treatmentFile = rs.getString(2);
                commentaryFile = rs.getString(3);
                vitalSignsFile = rs.getString(4);

                rs.close();
                st.close();
                return true;
            }
            else {
                rs.close();
                st.close();
                System.out.println("NO EXISTE EL PACIENTE");
                return false;
            }
        }
        catch(Exception e)
        {
            System.out.println("ERROR EN OBTENCIÓN DE DATOS DE ARCHIVOS DEL PACIENTE");
            System.out.println(e.toString());
            return false;
        }
    }

    public String getRut1() {return rut1;}
    public String getRut2() {return rut2;}
    public String getName() {return name;}
    public String getSex() {return sex;}
    public int getNacimientoD() {return nacimientoD;}
    public int getNacimientoM() {return nacimientoM;}
    public int getNacimientoA() {return nacimientoA;}
    public String getAddress() {return address;}
    public String getCity() {return city;}
    public int getFono1() {return fono1;}
    public int getFono2() {return fono2;}
    public String getPrevision() {return prevision;}
    public String getDiagnosticFile() {return diagnosticFile;}
    public String getTreatmentFile() {return treatmentFile;}
    public String getCommentaryFile() {return commentaryFile;}
    public String getVitalSignsFile() {return vitalSignsFile;}

    /********************************************************************************/
/*
    public int setRut1(int rut1) {return rut1;}
    public String getRut2() {return rut2;}
    public String getName() {return name;}
    public int getAge() {return age;}
    public String getSex() {return sex;}
    public String getAddress() {return address;}
    public String getCity() {return city;}
    public int getFono1() {return fono1;}
    public int getFono2() {return fono2;}
    public String getPrevision() {return prevision;}
    public String getDiagnosticFile() {return diagnosticFile;}
    public String getTreatmentFile() {return treatmentFile;}
    public String getCommentaryFile() {return commentaryFile;}
    public String getVitalSignsFile() {return vitalSignsFile;}
*/
}