package com.apobates.forum.core.entity;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;
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.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

/**
 * 版块
 *
 * @author xiaofanku
 * @since 20200510
 */
@Entity
@Table(name = "apo_board", uniqueConstraints={@UniqueConstraint(columnNames={"title","volumesId"})})
public class Board implements Serializable, Comparable<Board> {
    private static final long serialVersionUID = 52825921502430557L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;
    //名称
    private String title;
    //描述
    private String description;
    //创建日期
    private LocalDateTime entryDateTime;
    //图标
    private String imageAddr;
    //卷的ID
    private int volumesId = 0;
    //状态
    @Basic
    @Enumerated(EnumType.STRING)
    private ForumEntityStatusEnum status;
    //显示顺序
    private int ranking;
    // 是否原生,多数情况都是站长创建的.默认是false
    // 原生不能在会员可以访问的地方出现.只供系统使用
    @Column(columnDefinition="tinyint(1) default 0")
    private boolean origin;
    // 配合原生一起使用的目录名
    private String directoryNames;

    //empty constructor for JPA instantiation
    public Board() {
        super();
    }
    //从前端来的
    public Board(String title, String description, String imageAddr, int volumesId, ForumEntityStatusEnum status, int ranking) {
        this.id = 0L;
        this.title = title;
        this.description = description;
        this.entryDateTime = LocalDateTime.now();
        this.imageAddr = imageAddr;
        this.volumesId = volumesId;
        this.status = status;
        this.ranking = ranking;
        this.origin = false;
        this.directoryNames = null;
    }
    public long getId() {
        return id;
    }
    
    public String getTitle() {
        return title;
    }
    
    public String getDescription() {
        return description;
    }
    
    public LocalDateTime getEntryDateTime() {
        return entryDateTime;
    }
    
    public String getImageAddr() {
        return imageAddr;
    }
    
    public int getVolumesId() {
        return volumesId;
    }
    
    public ForumEntityStatusEnum getStatus() {
        return status;
    }
    
    public boolean isOrigin() {
        return origin;
    }
    
    public String getDirectoryNames() {
        return directoryNames;
    }
    
    public int getRanking() {
        return ranking;
    }
    
    public void setId(long id) {
        this.id = id;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public void setEntryDateTime(LocalDateTime entryDateTime) {
        this.entryDateTime = entryDateTime;
    }
    
    public void setImageAddr(String imageAddr) {
        this.imageAddr = imageAddr;
    }
    
    public void setVolumesId(int volumesId) {
        this.volumesId = volumesId;
    }
    
    public void setStatus(ForumEntityStatusEnum status) {
        this.status = status;
    }
    
    public void setRanking(int ranking) {
        this.ranking = ranking;
    }
    
    public void setOrigin(boolean origin) {
        this.origin = origin;
    }
    
    public void setDirectoryNames(String directoryNames) {
        this.directoryNames = directoryNames;
    }
    
    @Override
    public int hashCode() {
        int hash = 3;
        hash = 67 * hash + Objects.hashCode(this.title);
        hash = 67 * hash + this.volumesId;
        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 Board other = (Board) obj;
        if (this.volumesId != other.volumesId) {
            return false;
        }
        return Objects.equals(this.title, other.title);
    }
    
    @Override
    public int compareTo(Board o) {
        return ranking - o.getRanking();
    }
    
    @Transient
    public static Board empty(long boardId) {
        Board board = new Board("NUL", "Do Not Allow blank", "image://defat/ico/default_icon.png", 0, ForumEntityStatusEnum.ACTIVE, 1);
        board.setId(boardId);
        return board;
    }
    
    @Transient
    public boolean isNormal() {
        return ForumEntityStatusEnum.ACTIVE == status;
    }
    
    @Transient
    public String getIcoStatus() {
        if (ForumEntityStatusEnum.LOCKED == getStatus()) {
            return "locked"; //锁定
        }
        if (ForumEntityStatusEnum.DELETE == getStatus()) {
            return "read"; //删除
        }
        return "normal"; //正常
    }
}