package versao3.persistencia.impl.jds;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import versao3.dominio.Cliente;
import versao3.persistencia.ClienteDAO;
import versao3.persistencia.DAO;
import versao3.persistencia.DAOFactory;
import versao3.persistencia.PedidoDAO;
import versao3.persistencia.PersistanceException;

/**
 * Implementaçao do DAO de Cliente para JDataStore.
 *
 * @see versao3.persistencia.ClienteDAO
 */
public final class JDSClienteDAOImpl
    implements ClienteDAO {

  private JDSClienteDAOImpl(){}

  private static JDSClienteDAOImpl instance;

  static {
    instance = new JDSClienteDAOImpl();
  }

  protected static ClienteDAO getInstance() {
    return instance;
  }

  private void update(Cliente cliente) throws PersistanceException {
    try {
      Connection conn = JDSDBUtil.getConnection();
      PreparedStatement pstmt = conn.
          prepareStatement("UPDATE CLIENTE SET NOME = ? WHERE CLIENTEID = ?");
      pstmt.setString(1, cliente.getNome());
      pstmt.setLong(2, cliente.getClienteId());
      pstmt.executeUpdate();
      conn.close();
    }
    catch (SQLException ex) {
      throw new PersistanceException(ex);
    }
  }

  public void save(Cliente cliente) throws PersistanceException {
    if (cliente.getClienteId() == UNSAVED) {
      create(cliente);
    }
    else {
      update(cliente);
    }
  }

  public void remove(Cliente cliente) throws PersistanceException {
    try {
      PedidoDAO pedidoDAO = DAOFactory.getDAOFactory().createPedidoDAO();
      pedidoDAO.removerPorCliente(cliente);
      Connection conn = JDSDBUtil.getConnection();
      Statement stmt = conn.createStatement();
      stmt.executeUpdate("DELETE FROM CLIENTE WHERE CLIENTEID=" +
                         cliente.getClienteId());
      cliente.setClienteId(UNSAVED);
      conn.close();
    }
    catch (SQLException ex) {
      throw new PersistanceException(ex);
    }
  }

  public void removerTodos() throws PersistanceException {
    try {
      Connection conn = JDSDBUtil.getConnection();
      Statement stmt = conn.createStatement();
      stmt.executeUpdate("DELETE FROM ITEM");
      stmt.executeUpdate("DELETE FROM PEDIDO");
      stmt.executeUpdate("DELETE FROM CLIENTE");
      conn.close();
    }
    catch (SQLException ex) {
      throw new PersistanceException(ex);
    }
  }

  public List buscarTodos() throws PersistanceException {
    try {
      List result = new ArrayList();
      Cliente cliente = null;

      Connection conn = JDSDBUtil.getConnection();
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery("SELECT * FROM CLIENTE");

      while (rs.next()) {
        cliente = new Cliente();
        cliente.setClienteId(rs.getLong("CLIENTEID"));
        cliente.setNome(rs.getString("NOME"));
        result.add(cliente);
      }
      conn.close();
      return result;
    }
    catch (SQLException ex) {
      throw new PersistanceException(ex);
    }

  }



  private void create(Cliente cliente) throws PersistanceException {
    try {
      Connection conn = JDSDBUtil.getConnection();
      PreparedStatement pstmt = conn.
          prepareStatement("INSERT INTO CLIENTE(NOME) VALUES (?)");
      pstmt.setString(1, cliente.getNome());
      pstmt.executeUpdate();
      ResultSet rs = pstmt.getGeneratedKeys();
      if (rs.next()) {
        cliente.setClienteId(rs.getLong(1));
      }
      conn.close();
    }
    catch (SQLException ex) {
      throw new PersistanceException(ex);
    }

  }

}
