package bean;

import beans.ClienteFacadeLocal;
import beans.CompraTarjetaCreditoFacade;
import beans.CompraTarjetaCreditoFacadeLocal;
import beans.PagoTarjetaCreditoFacadeLocal;
import beans.TarjetaCreditoFacadeLocal;
import entities.Cliente;
import entities.CompraTarjetaCredito;
import entities.PagoTarjetaCredito;
import entities.TarjetaCredito;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named(value = "tarjetaCreditoBean")
@SessionScoped
public class TarjetaCreditoBean implements Serializable {

  @EJB
  private TarjetaCreditoFacadeLocal tarjetaCreditoFacade;
  @EJB
  private ClienteFacadeLocal clienteFacade;
  @EJB
  private CompraTarjetaCreditoFacadeLocal compraTarjetaCreditoFacade;
  @EJB
  private PagoTarjetaCreditoFacadeLocal pagoTarjetaCreditoFacade;

  private Cliente cliente;
  private TarjetaCredito tarjetaCredito;

  @PostConstruct
  public void init() {
    this.cliente = new Cliente();
    this.tarjetaCredito = new TarjetaCredito();
  }

  public String contratar() {
    tarjetaCredito.setCliente(cliente);

    tarjetaCreditoFacade.create(tarjetaCredito);
    clienteFacade.edit(cliente);

    this.resetClases();
    return "admin_cliente_view";
  }

  public String gotoContratarLineaSobregiro() {
    FacesContext context = FacesContext.getCurrentInstance();
    LineaSobregiroBean lineaSobregiroBean = context.getApplication().evaluateExpressionGet(context, "#{lineaSobregiroBean}", LineaSobregiroBean.class);
    lineaSobregiroBean.setTarjetaCredito(tarjetaCredito);
    return "admin_linea_sobregiro_credito_contratar";
  }

  public String gotoComprar() {
    FacesContext context = FacesContext.getCurrentInstance();
    CompraBean cb = context.getApplication().evaluateExpressionGet(context, "#{compraBean}", CompraBean.class);
    cb.setTarjetaCredito(tarjetaCredito);
    return "cliente_tarjeta_credito_comprar";
  }

  public String gotoDeleteCompra(CompraTarjetaCredito compra) {
    FacesContext context = FacesContext.getCurrentInstance();
    CompraBean cb = context.getApplication().evaluateExpressionGet(context, "#{compraBean}", CompraBean.class);
    cb.setTarjetaCredito(tarjetaCredito);
    cb.setCompraTarjetaCredito(compra);
    return "admin_tarjeta_credito_delete_compra";
  }

  public String gotoPagar() {
    FacesContext context = FacesContext.getCurrentInstance();
    PagoBean pb = context.getApplication().evaluateExpressionGet(context, "#{pagoBean}", PagoBean.class);
    pb.setTarjetaCredito(tarjetaCredito);
    return "cliente_tarjeta_credito_pagar";
  }

  public String gotoDeletePago(PagoTarjetaCredito pago) {
    FacesContext context = FacesContext.getCurrentInstance();
    PagoBean pb = context.getApplication().evaluateExpressionGet(context, "#{pagoBean}", PagoBean.class);
    pb.setTarjetaCredito(tarjetaCredito);
    pb.setPagoTarjetaCredito(pago);
    return "admin_tarjeta_credito_delete_pago";

  }

  public String gotoPagarSobregiro() {
    FacesContext context = FacesContext.getCurrentInstance();
    LineaSobregiroBean lsb = context.getApplication().evaluateExpressionGet(context, "#{lineaSobregiroBean}", LineaSobregiroBean.class);
    lsb.setLineaSobregiro(tarjetaCredito.getLineaSobregiro());
    return "cliente_tarjeta_credito_sobregiro_pagar";
  }

  public String edit() {
    tarjetaCreditoFacade.edit(tarjetaCredito);

    this.resetClases();
    return "admin_cliente_view";
  }

  private void resetClases() {
    this.cliente = new Cliente();
    this.tarjetaCredito = new TarjetaCredito();
  }

  public Cliente getCliente() {
    return cliente;
  }

  public void setCliente(Cliente cliente) {
    this.cliente = cliente;
  }

  public TarjetaCredito getTarjetaCredito() {
    return tarjetaCredito;
  }

  public void setTarjetaCredito(TarjetaCredito tarjetaCredito) {
    this.tarjetaCredito = tarjetaCredito;
  }

  public List<CompraTarjetaCredito> getCompraList() {
    return this.compraTarjetaCreditoFacade.findAll();
  }
  public List<PagoTarjetaCredito> getPagoList() {
    return this.pagoTarjetaCreditoFacade.findAll();
  }
  
}
