package models;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import models.Test.TestType;

@Entity
public class Report extends ModelUuid {

	@Basic
	// the date of Test submission
	private Date reportDate;

	// the path to download csv report
	@Basic
	private String reportCsvPath;

	@Basic
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
	private Test reportTest;

	@OneToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
	private List<Metadata> metadata = new LinkedList<Metadata>();

	// array of all question report for the test
	@OneToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
	private List<ReportQuestion> questions = new LinkedList<ReportQuestion>();

	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
	private User author;

	@Basic
	private Date startedAt;

	@Basic
	private Date endedAt;

	@Basic
	// endedAt - startedAt
	private Date effectiveElapsedDate;

	public Date getReportDate() {
		return reportDate;
	}

	public void setReportDate(Date reportDate) {
		this.reportDate = reportDate;
	}

	public String getReportCsvPath() {
		return reportCsvPath;
	}

	public void setReportCsvPath(String reportCsvPath) {
		this.reportCsvPath = reportCsvPath;
	}

	public Test getReportTest() {
		return reportTest;
	}

	public void setReportTest(Test reportTest) {
		this.reportTest = reportTest;
	}

	public List<Metadata> getMetadata() {
		return metadata;
	}

	public void setMetadata(List<Metadata> metadata) {
		this.metadata = metadata;
	}

	public List<ReportQuestion> getQuestions() {
		return questions;
	}

	public void setQuestions(List<ReportQuestion> questions) {
		this.questions = questions;
	}

	public User getAuthor() {
		return author;
	}

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

	public Date getStartedAt() {
		return startedAt;
	}

	public void setStartedAt(Date startedAt) {
		this.startedAt = startedAt;
	}

	public Date getEndedAt() {
		return endedAt;
	}

	public void setEndedAt(Date endedAt) {
		this.endedAt = endedAt;
	}

	public Date getEffectiveElapsedDate() {
		return effectiveElapsedDate;
	}

	public void setEffectiveElapsedDate(Date effectiveElapsedDate) {
		this.effectiveElapsedDate = effectiveElapsedDate;
	}

	public boolean addMetadata(Metadata metadata) {
		if (metadata == null)
			return false;
		if (this.metadata.contains(metadata)) {
			return false;
		}
		return this.metadata.add(metadata);
	}

	public boolean addSlide(ReportQuestion reportQuestion) {
		if (reportQuestion == null)
			return false;
		if (this.questions.contains(reportQuestion)) {
			return false;
		}
		return this.questions.add(reportQuestion);
	}

	public static List<Report> findAllByType(String type) {
		if(type==null)
			return findAll();
		TestType tt = Test.TestType.tryBuildTestTypeFromName(type);
		return find("reportTest.testType=? and reportCsvPath is not null and reportCsvPath!=''", tt).fetch();
		//return null;
	}
}