package Datos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import Modelo.CodConcepto;
import java.util.ArrayList;

public class CodConceptoDB {

    public static CodConcepto getCodConcepto(String codConcepto) {
        ConnectionPool cp = ConnectionPool.getInstance();
        Connection c = cp.getConnection();
        PreparedStatement ps;
        ResultSet rs;
        CodConcepto cod_concepto = null;
        String query = "SELECT * FROM COD_CONCEPTO cc "
                + "WHERE cc.cod_concept = ?";
        try {
            ps = c.prepareStatement(query);
            ps.setString(1, codConcepto);
            rs = ps.executeQuery();
            if(rs.next()){
                String codC = rs.getString("cod_concept");
                String descripcion_concepto = rs.getString("descripcion_concept");
                cod_concepto = new CodConcepto(codC, descripcion_concepto);
            }else {
                cod_concepto = new CodConcepto(codConcepto, "");
            }    
        } catch (SQLException e) {
            e.printStackTrace();
        }
        cp.freeConnection(c);
        return cod_concepto;
    }
    
    public static ArrayList <CodConcepto> getConceptos(){
        ConnectionPool cp = ConnectionPool.getInstance();
        Connection c = cp.getConnection();
        PreparedStatement ps;
        ResultSet rs;
        ArrayList <CodConcepto> conceptos = new ArrayList<>();
        CodConcepto codConcepto = null;
        String query = "SELECT * FROM COD_CONCEPTO cc ORDER BY DESCRIPCION_CONCEPT ASC";
        try {
            ps = c.prepareStatement(query);
            rs = ps.executeQuery();
            while(rs.next()){
                String cod_concepto = rs.getString("cod_concept");
                String descripcion_concepto = rs.getString("descripcion_concept");
                codConcepto = new CodConcepto(cod_concepto, descripcion_concepto);
                conceptos.add(codConcepto);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        cp.freeConnection(c);
        return conceptos;
    }

    public static boolean addConcepto(CodConcepto cod_concepto) {
        int resultado = 0;
        ConnectionPool cp = ConnectionPool.getInstance();
        Connection c = cp.getConnection();
        PreparedStatement ps;
        String query = "INSERT INTO COD_CONCEPTO VALUES(?,?)";
        try {
            ps = c.prepareStatement(query);
            ps.setString(1, cod_concepto.getCod_concepto());
            ps.setString(2, cod_concepto.getDescripcion_concepto());
            resultado = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        cp.freeConnection(c);
        return resultado == 1;
    }
    
    public static boolean updateConcepto(CodConcepto cod_concepto) {
        int resultado = 0;
        ConnectionPool cp = ConnectionPool.getInstance();
        Connection c = cp.getConnection();
        PreparedStatement ps;
        String query = "UPDATE COD_CONCEPTO "+   
                       "SET cod_concept = ? ,"+
                             " descripcion_concept = ? "+
                       "WHERE cod_concept = ?";
        try {
            ps = c.prepareStatement(query);
            ps.setString(1, cod_concepto.getCod_concepto());
            ps.setString(2, cod_concepto.getDescripcion_concepto());
            ps.setString(1, cod_concepto.getCod_concepto());
            resultado = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        cp.freeConnection(c);
        return resultado == 1;
    }
}
