package com.apobates.forum.letterbox.entity;

import java.io.Serializable;
import java.time.LocalDateTime;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * 信件
 *
 * @author xiaofanku
 * @since 20200510
 */
@Entity
@Table(name = "apo_letter")
public class ForumLetter implements Serializable {
    private static final long serialVersionUID = 591037265377031648L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;
    //标题
    private String title;
    //内容
    @Lob
    @Column(columnDefinition = "TEXT")
    private String content;
    //创建日期
    private LocalDateTime entryDateTime;
    //消息类型
    @Basic
    @Enumerated(EnumType.STRING)
    private ForumLetterTypeEnum typed;
    //作者/发件人/会员ID
    private long author;
    //作者名称/会员昵称
    private String nickname;
    //是否需要回应(只有私信需要其它类型都不需要)
    @Column(columnDefinition="tinyint(1) default 0")
    private boolean responsive;
    
    //empty constructor for JPA instantiation
    public ForumLetter() {
    }
    
    public ForumLetter(String title, String content, long author, String authorNickname, ForumLetterTypeEnum type) {
        this.title = title;
        this.content = content;
        this.entryDateTime = LocalDateTime.now();
        this.typed = type;
        this.author = author;
        this.nickname = authorNickname;
        this.responsive = (ForumLetterTypeEnum.LETTER == type);
    }
    
    public long getId() {
        return id;
    }
    
    public String getTitle() {
        return title;
    }
    
    public String getContent() {
        return content;
    }
    
    public LocalDateTime getEntryDateTime() {
        return entryDateTime;
    }
    
    public ForumLetterTypeEnum getTyped() {
        return typed;
    }
    
    public long getAuthor() {
        return author;
    }
    
    public String getNickname() {
        return nickname;
    }
    
    public boolean isResponsive() {
        return responsive;
    }
    
    public void setId(long id) {
        this.id = id;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public void setContent(String content) {
        this.content = content;
    }
    
    public void setEntryDateTime(LocalDateTime entryDateTime) {
        this.entryDateTime = entryDateTime;
    }
    
    public void setTyped(ForumLetterTypeEnum typed) {
        this.typed = typed;
    }
    
    public void setAuthor(long author) {
        this.author = author;
    }
    
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    
    public void setResponsive(boolean responsive) {
        this.responsive = responsive;
    }
    @Transient
    public static ForumLetter noticeLetter(String title, String content){
        return new ForumLetter(title, content, 0L, "机器人", ForumLetterTypeEnum.NOTICE);
    }
}