package org.codiki.core.entities.persistence;

import java.io.Serializable;
import java.util.Date;
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.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.codiki.core.entities.dto.PostDTO;
import org.codiki.core.entities.dto.View;
import org.codiki.core.utils.DateUtils;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;

import com.fasterxml.jackson.annotation.JsonView;

@Entity
@Table(name="post")
public class Post implements Serializable {
	private static final long serialVersionUID = 1L;
	
	/** Number of character that compose the text extract. */
	private static final short EXTRACT_LENGTH = 250;
	
	/* ******************* */
	/* Attributes */
	/* ******************* */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@JsonView(View.PostDTO.class)
	// This annotation serves to fetch the attribute after an insert into db
	@Generated(GenerationTime.ALWAYS)
	private String key;

	@JsonView(View.PostDTO.class)
	private String title;

	@JsonView(View.PostDTO.class)
	private String text;

	@JsonView(View.PostDTO.class)
	@Column(length = 250)
	private String description;

	@JsonView(View.PostDTO.class)
	private String image;

	@JsonView(View.PostDTO.class)
	@Column(name = "creation_date")
	@Temporal(TemporalType.TIMESTAMP)
	private Date creationDate;
	
	/* ******************* */
	/* Relations */
	/* ******************* */
	@JsonView(View.PostDTO.class)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "creator_id")
	private User author;

	@JsonView(View.PostDTO.class)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "category_id")
	private Category category;
	
	@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE)
	private List<PostHistory> history;
	
	/* ******************* */
	/* Constructors */
	/* ******************* */
	public Post() {
		super();
	}
	
	public Post(final PostDTO pPost) {
		title = pPost.getTitle();
		image = pPost.getImage();
		description = pPost.getDescription();
		creationDate = new Date();
		text = pPost.getText();
		category = new Category(pPost.getCategory());
	}

	/* ******************* */
	/* Getters & Setters */
	/* ******************* */
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		if(this.id != null) {
			throw new IllegalAccessError("It's not allowed to rewrite the id entity.");
		}
		this.id = id;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getTitle() {
		return title;
	}

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

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	
	public String getCreationDateStr() {
		if(creationDate == null) {
			return "";
		} else {
			return DateUtils.format(creationDate);
		}
	}

	public String getTextExtract() {
		String extract = text;
		
		if(extract != null && extract.length() > EXTRACT_LENGTH) {
			extract = extract.substring(0, EXTRACT_LENGTH) + "...";
		}
		
		return extract;
	}

	public User getAuthor() {
		return author;
	}

	public void setAuthor(User author) {
		this.author = author;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public List<PostHistory> getHistory() {
		return history;
	}

	public void setHistory(List<PostHistory> history) {
		this.history = history;
	} 
}
