package ch.ds1.challengeTask.TodoList.todolist;

import ch.ds1.challengeTask.TodoList.entry.Entry;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity(name = "TodoList")
@Table(name = "todolist")
public class TodoList {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "listid", updatable = false)
  private Long listId;
  @Column(name = "name")
  private String name;
  @Column(name = "creation_date")
  private LocalDate creationDate;

  @OneToMany(mappedBy = "list", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
  private List<Entry> entries = new ArrayList<>();

  public TodoList() {
  }

  public TodoList(Long listId, String name, LocalDate creationDate) {
    this.listId = listId;
    this.name = name;
    this.creationDate = creationDate;
  }

  public TodoList(String name, LocalDate creationDate) {
    this.name = name;
    this.creationDate = creationDate;
  }

  public TodoList(String name) {
    this.name = name;
    this.creationDate = LocalDate.now();
  }

  public Long getListId() {
    return listId;
  }

  public void setListId(Long listId) {
    this.listId = listId;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public LocalDate getCreationDate() {
    return creationDate;
  }

  public void setCreationDate(LocalDate creationDate) {
    this.creationDate = creationDate;
  }

  public List<Entry> getEntries() {
    return entries;
  }

  public void setEntries(List<Entry> entries) {
    this.entries = entries;
  }

  public void addEntry(Entry entry) {
    if (!this.containsEntry(entry)) {
      this.entries.add(entry);
      if (entry.getList() != this) {
        entry.setList(this);
      }
    }
  }

  public boolean containsEntry(Entry entry) {
    return entries.contains(entry);
  }

  public void removeEntry(Entry entry) {
    entries.remove(entry);
  }

  @Override
  public String toString() {
    return "TodoList{" +
        "listId=" + listId +
        ", name='" + name + '\'' +
        ", creationDate=" + creationDate +
        '}';
  }
}
