package entities;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "tarjeta_credito")
@XmlRootElement
public class TarjetaCredito implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  private Integer id;

  @Column(name = "limite_nacional", nullable = false)
  private int limiteNacional;

  @Column(name = "limite_internacional", nullable = false)
  private int limiteInternacional;

  @Column(name = "deuda_nacional")
  private int deudaNacional;

  @Column(name = "deuda_internacional")
  private int deudaInternacional;

  @Column(name = "inserted_at")
  private LocalDateTime insertedAt;

  @OneToOne
  @JoinColumn(name = "linea_sobregiro")
  private LineaSobregiro lineaSobregiro;

  @OneToOne(mappedBy = "tarjetaCredito")
  private Cliente cliente;

  @OneToMany(mappedBy = "tarjetaCredito")
  private List<PagoTarjetaCredito> pagoList;

  @OneToMany(mappedBy = "tarjetaCredito")
  private List<CompraTarjetaCredito> compraList;

  @PrePersist
  public void onPersist() {
    this.insertedAt = LocalDateTime.now();
    this.deudaNacional = 0;
    this.deudaInternacional = 0;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public int getLimiteNacional() {
    return limiteNacional;
  }

  public void setLimiteNacional(int limiteNacional) {
    this.limiteNacional = limiteNacional;
  }

  public int getLimiteInternacional() {
    return limiteInternacional;
  }

  public void setLimiteInternacional(int limiteInternacional) {
    this.limiteInternacional = limiteInternacional;
  }

  public int getDeudaNacional() {
    return deudaNacional;
  }

  public int getDeudaInternacional() {
    return deudaInternacional;
  }

  public LocalDateTime getInsertedAt() {
    return insertedAt;
  }

  public void setInsertedAt(LocalDateTime insertedAt) {
    this.insertedAt = insertedAt;
  }

  public Cliente getCliente() {
    return cliente;
  }

  public void setCliente(Cliente cliente) {
    this.cliente = cliente;
    if(this.cliente.getTarjetaCredito() != this)
      this.cliente.setTarjetaCredito(this);
  }

  @XmlTransient
  public List<PagoTarjetaCredito> getPagoList() {
    return pagoList;
  }

  public void setPagoList(List<PagoTarjetaCredito> pagoList) {
    this.pagoList = pagoList;
  }
  
  public void addPago(PagoTarjetaCredito pago) {
    this.pagoList.add(pago);
    if(pago.getTarjetaCredito() != this)
      pago.setTarjetaCredito(this);
  }

  @XmlTransient
  public List<CompraTarjetaCredito> getCompraList() {
    return compraList;
  }

  public void setCompraList(List<CompraTarjetaCredito> compraList) {
    this.compraList = compraList;
  }
  
  public void addCompra(CompraTarjetaCredito compra) {
    this.compraList.add(compra);
    if(compra.getTarjetaCredito() != this) 
      compra.setTarjetaCredito(this);
  }

  public void setDeudaNacional(int deudaNacional) {
    this.deudaNacional = deudaNacional;
  }

  public void setDeudaInternacional(int deudaInternacional) {
    this.deudaInternacional = deudaInternacional;
  }

  public LineaSobregiro getLineaSobregiro() {
    return lineaSobregiro;
  }

  public void setLineaSobregiro(LineaSobregiro lineaSobregiro) {
    this.lineaSobregiro = lineaSobregiro;
    if(!lineaSobregiro.getTarjetaCreditoList().contains(this))
      lineaSobregiro.getTarjetaCreditoList().add(this);
  }

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof TarjetaCredito)) {
      return false;
    }
    TarjetaCredito other = (TarjetaCredito) object;
    return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
  }

  @Override
  public String toString() {
    return "entities.TarjetaCredito[ id=" + id + " ]";
  }

}
