package ch.ds1.challengeTask.TodoList.entry;


import ch.ds1.challengeTask.TodoList.todolist.TodoList;
import com.fasterxml.jackson.annotation.JsonIgnore;
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.Table;

@Entity(name = "Entry")
@Table(name = "entry")
public class Entry {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "entryid")
  private Long entryId;
  @Column(name = "title")
  private String title;
  @Column(name = "done")
  private boolean done;

  @ManyToOne(optional = false)
  @JoinColumn(name = "entry_listid")
  @JsonIgnore
  private TodoList list;

  public Entry() {
  }

  public Entry(Long entryId, String title, boolean done) {
    this.entryId = entryId;
    this.title = title;
    this.done = done;
  }

  public Entry(String title, boolean done) {
    this.title = title;
    this.done = done;
  }

  public Entry(String title) {
    this.title = title;
    this.done = false;
  }

  public Long getEntryId() {
    return entryId;
  }

  public void setEntryId(Long entryId) {
    this.entryId = entryId;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public boolean isDone() {
    return done;
  }

  public void setDone(boolean done) {
    this.done = done;
  }

  public TodoList getList() {
    return list;
  }

  public void setList(TodoList list) {
    TodoList oldList = this.list;
    this.list = list;

    if (list != null && !list.containsEntry(this)) {
      list.addEntry(this);
    }
    if (oldList != null && oldList.containsEntry(this)) {
      oldList.removeEntry(this);
    }
  }

  @Override
  public String toString() {
    return "Entry{" +
        "entryId=" + entryId +
        ", title='" + title + '\'' +
        ", done=" + done +
        '}';
  }
}
