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_debito")
@XmlRootElement
public class TarjetaDebito implements Serializable {

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

  @Column(name = "saldo")
  private int saldo;

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

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

  @OneToMany(mappedBy = "tarjetaDebitoFrom")
  private List<TransferenciaTarjetaDebito> transferenciaFromList;

  @OneToMany(mappedBy = "tarjetaDebitoTo")
  private List<TransferenciaTarjetaDebito> transferenciaToList;

  @OneToMany(mappedBy = "tarjetaDebito")
  private List<DepositoTarjetaDebito> depositoList;

  @OneToMany(mappedBy = "tarjetaDebito")
  private List<GiroTarjetaDebito> giroList;

  @OneToMany(mappedBy = "tarjetaDebito")
  private List<CompraTarjetaDebito> compraList;

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

  public Integer getId() {
    return id;
  }

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

  public int getSaldo() {
    return saldo;
  }

  public void setSaldo(int saldo) {
    this.saldo = saldo;
  }

  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.getTarjetaDebito() != this)
      this.cliente.setTarjetaDebito(this);
  }

  @XmlTransient
  public List<DepositoTarjetaDebito> getDepositoList() {
    return depositoList;
  }

  public void setDepositoList(List<DepositoTarjetaDebito> depositoList) {
    this.depositoList = depositoList;
  }

  public void addDeposito(DepositoTarjetaDebito deposito) {
    this.depositoList.add(deposito);
    if (deposito.getTarjetaDebito()!= this)
      deposito.setTarjetaDebito(this);
  }

  @XmlTransient
  public List<TransferenciaTarjetaDebito> getTransferenciaFromList() {
    return transferenciaFromList;
  }

  public void setTransferenciaFromList(List<TransferenciaTarjetaDebito> transferenciaFromList) {
    this.transferenciaFromList = transferenciaFromList;
  }

  public void addTransferenciaFrom(TransferenciaTarjetaDebito transferenciaFrom) {
    this.transferenciaFromList.add(transferenciaFrom);
    if (transferenciaFrom.getTarjetaDebitoFrom()!= this)
      transferenciaFrom.setTarjetaDebitoFrom(this);
  }

  @XmlTransient
  public List<TransferenciaTarjetaDebito> getTransferenciaToList() {
    return transferenciaToList;
  }

  public void setTransferenciaToList(List<TransferenciaTarjetaDebito> transferenciaToList) {
    this.transferenciaToList = transferenciaToList;
  }

  public void addTransferenciaTo(TransferenciaTarjetaDebito transferenciaTo) {
    this.transferenciaFromList.add(transferenciaTo);
    if (transferenciaTo.getTarjetaDebitoTo() != this)
      transferenciaTo.setTarjetaDebitoTo(this);
  }

  @XmlTransient
  public List<GiroTarjetaDebito> getGiroList() {
    return giroList;
  }

  public void setGiroList(List<GiroTarjetaDebito> giroList) {
    this.giroList = giroList;
  }

  public void addGiro(GiroTarjetaDebito giro) {
    this.giroList.add(giro);
    if (giro.getTarjetaDebito()!= this)
      giro.setTarjetaDebito(this);
  }

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

  public void setCompraList(List<CompraTarjetaDebito> compraList) {
    this.compraList = compraList;
  }

  public void addCompra(CompraTarjetaDebito compra) {
    this.compraList.add(compra);
    if(compra.getTarjetaDebito() != this)
      compra.setTarjetaDebito(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 TarjetaDebito)) {
      return false;
    }
    TarjetaDebito other = (TarjetaDebito) object;
    return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
  }

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