package bean;

import beans.ClienteFacadeLocal;
import beans.TarjetaCreditoFacadeLocal;
import entities.Cliente;
import entities.TarjetaCredito;
import java.io.Serializable;
import java.util.Date;
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;

  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 gotoComprar() {
    FacesContext context = FacesContext.getCurrentInstance();
    CompraBean cb = context.getApplication().evaluateExpressionGet(context, "#{compraBean}", CompraBean.class);
    cb.setTarjetaCredito(tarjetaCredito);
    return "cliente_tarjeta_credito_comprar";
  }
  
  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 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;
  }

}
