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.PrePersist;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "linea_sobregiro")
@XmlRootElement
public class LineaSobregiro implements Serializable {

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

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

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

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

  @OneToMany(mappedBy = "lineaSobregiro")
  private List<CuentaCorriente> cuentaCorrienteList;

  @OneToMany(mappedBy = "lineaSobregiro")
  private List<TarjetaCredito> tarjetaCreditoList;

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

  public Integer getId() {
    return id;
  }

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

  public int getSobregiro() {
    return sobregiro;
  }

  public void setSobregiro(int sobregiro) {
    this.sobregiro = sobregiro;
  }

  public int getLimite() {
    return limite;
  }

  public void setLimite(int limite) {
    this.limite = limite;
  }

  public List<CuentaCorriente> getCuentaCorrienteList() {
    return cuentaCorrienteList;
  }

  public void setCuentaCorrienteList(List<CuentaCorriente> cuentaCorrienteList) {
    this.cuentaCorrienteList = cuentaCorrienteList;
  }
  
  public void addCuentaCorriente(CuentaCorriente cuentaCorriente){
    this.cuentaCorrienteList.add(cuentaCorriente);
    if(cuentaCorriente.getLineaSobregiro() != this)
      cuentaCorriente.setLineaSobregiro(this);
  }

  public List<TarjetaCredito> getTarjetaCreditoList() {
    return tarjetaCreditoList;
  }

  public void setTarjetaCreditoList(List<TarjetaCredito> tarjetaCreditoList) {
    this.tarjetaCreditoList = tarjetaCreditoList;
  }
  
  public void addTarjetaCredito(TarjetaCredito tarjetaCredito){
    this.tarjetaCreditoList.add(tarjetaCredito);
    if(tarjetaCredito.getLineaSobregiro() != this)
      tarjetaCredito.setLineaSobregiro(this);
  }

  public LocalDateTime getInsertedAt() {
    return insertedAt;
  }

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

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

  @Override
  public boolean equals(Object object) {
    if (!(object instanceof LineaSobregiro)) {
      return false;
    }
    LineaSobregiro other = (LineaSobregiro) object;
    return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
  }

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

}
