/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.ticket;

import java.time.LocalDateTime;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import model.AbstractEntity;
import model.person.Person;

/**
 *
 * @author Patrick
 */
@Entity
public class Comment extends AbstractEntity implements Comparable<Comment> {
    
    @Column(columnDefinition = "longblob")
    private String message;
    
    @ManyToOne
    private Person writer;
    
    private boolean edited;
    
    @ManyToOne
    private Ticket ticket;

    public Comment() {
    }
    
    public Comment(Person writer, String message) {
        this.writer = writer;
        this.edited = false;
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
        edited = true;
    }

    public LocalDateTime getLastEditedDate() {
        return getChangedDate();
    }

    public boolean isEdited() {
        return edited;
    }

    public void setEdited(boolean edited) {
        this.edited = edited;
        setChangedDate(LocalDateTime.now());
    }

    public Ticket getTicket() {
        return ticket;
    }
    
    public void setTicket(Ticket ticket) {
        this.ticket = ticket;
        setChangedDate(LocalDateTime.now());
    }

    public Person getWriter() {
        return writer;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + Objects.hashCode(this.message);
        hash = 79 * hash + Objects.hashCode(this.writer);
        hash = 79 * hash + Objects.hashCode(getCreationDate());
        hash = 79 * hash + Objects.hashCode(getChangedDate());
        hash = 79 * hash + (this.edited ? 1 : 0);
        hash = 79 * hash + Objects.hashCode(this.ticket);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Comment other = (Comment) obj;
        if (this.edited != other.edited) {
            return false;
        }
        if (!Objects.equals(this.message, other.message)) {
            return false;
        }
        if (!Objects.equals(this.writer, other.writer)) {
            return false;
        }
        if (!Objects.equals(getCreationDate(), other.getCreationDate())) {
            return false;
        }
        if (!Objects.equals(this.ticket, other.ticket)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Comment{" + "writer=" + writer.getEmail() + ", creationDate=" + getCreationDate() + ", id=" + getId() + ", message="+ message +  '}';
    }

    @Override
    public int compareTo(Comment c) {
        return c.getCreationDate().compareTo(this.getCreationDate());
    }
    
    
}
