package ub.scp2.entities;

import infoObjects.InfoTarea;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;


/**
 * Task.java -this class is an entity binded to the task table from the database
 */
public class Task implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3922667470146248437L;
	
	private Long taskId;
	private User user;
	private Languaje languaje;
	private String title;
	private Date startDate;
	private Date endDate;
	private String description;
	private String compilerArguments;
	private Set<Submission> submissions;
        private Set<FileTask> files;
	private Set<Test> tests;
	private Subject subject;
	private boolean hasCodeTeacher = false;
        private boolean hasMakefile = false;
	private String nameFile;
        private int validFiles;

	public Task() {
	}

	public Task(User user, String title, Date startDate, Date endDate,
			String description, Languaje languaje,
			String compilerArguments, String nameFile) {
		this.user = user;
		this.title = title;
		this.startDate = startDate;
		this.endDate = endDate;
		this.description = description;
		this.languaje = languaje;
		this.compilerArguments = compilerArguments;
		this.nameFile = nameFile;
	}

	public Task(User user, String title, Date startDate, Date endDate,
			String description, Languaje languaje,
			String compilerArguments, Set<Submission> submissions, Set<Test> tests, String nameFile) {
		this.user = user;
		this.title = title;
		this.startDate = startDate;
		this.endDate = endDate;
		this.description = description;
		this.languaje = languaje;
		this.compilerArguments = compilerArguments;
		this.submissions = submissions;
		this.tests = tests;
		this.nameFile = nameFile;
	}
	
        
	public java.util.ArrayList<String> getFilenameArray(){
                java.util.ArrayList<String> filenames = new java.util.ArrayList();
                for (FileTask ft:this.files)
                    filenames.add(ft.getFilename());
		return filenames;
	}

	public Long getTaskId() {
		return this.taskId;
	}

	public void setTaskId(Long taskId) {
		this.taskId = taskId;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getTitle() {
		return this.title;
	}

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

	public Date getStartDate() {
		return this.startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return this.endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getDescription() {
		return this.description;
	}

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

	public String getCompilerArguments() {
		return this.compilerArguments;
	}

	public void setCompilerArguments(String compilerArguments) {
		this.compilerArguments = compilerArguments;
	}

	public Set<Submission> getSubmissions() {
		return this.submissions;
	}

	public void setSubmissions(Set<Submission> submissions) {
		this.submissions = submissions;
	}
        
	public void addSubmission(Submission submission) {
                java.util.Iterator<Submission> sIte = submissions.iterator();
                while (sIte.hasNext()){
                    Submission s = sIte.next();
                    if (s.getId() == submission.getId()) {
                        sIte.remove();
                        break;
                    }
                }
                submissions.add(submission);
	}

	public Set<Test> getTests() {
		return tests;
	}

	public void setTests(Set<Test> tests) {
		this.tests = tests;
	}

	public Subject getSubject() {
		return subject;
	}

	public void setSubject(Subject subject) {
		this.subject = subject;
	}

	public boolean isHasCodeTeacher() {
		return hasCodeTeacher;
	}

	public void setHasCodeTeacher(boolean hasCodeTeacher) {
		this.hasCodeTeacher = hasCodeTeacher;
	}

	public String getNameFile() {
		return nameFile;
	}

	public void setNameFile(String nameFile) {
		this.nameFile = nameFile;
	}

	public Languaje getLanguaje() {
		return languaje;
	}

	public void setLanguaje(Languaje languaje) {
		this.languaje = languaje;
	}

        public boolean isHasMakefile() {
                return hasMakefile;
        }

        public void setHasMakefile(boolean hasMakefile) {
                this.hasMakefile = hasMakefile;
        }  

        public Set<FileTask> getFiles() {
                return files;
        }

        public void setFiles(Set<FileTask> files) {
                this.files = files;
        }
        
        
        
        public void addFile(FileTask filetask){
            for (FileTask f:files){
                if (f.getFilename().equals(filetask.getFilename())){
                    files.remove(f);
                    break;
                }
            }
            files.add(filetask);
        }
        
        public void removeFile(FileTask filetask){
            files.remove(filetask);
        }
        
        public void removeAllFiles(){
            files.removeAll(files);
        }
        

        public int getValidFiles() {
                return validFiles;
        }

        public void setValidFiles(int validFiles) {
                this.validFiles = validFiles;
        }     
        
        public boolean fileIsValid(String[] mimetype) {
                if (mimetype[0].equals("text")) return true;
                
                boolean pdf = ((this.validFiles & 1) > 0);
                if (pdf && ("application/pdf").equals(mimetype[0]+"/"+mimetype[1])) return true;
                
                boolean image = ((this.validFiles & 2) > 0);
                if (image && mimetype[0].equals("image")) return true;
                
                boolean sound = ((this.validFiles & 4) > 0);
                if (sound && mimetype[0].equals("audio")) return true;
                
                boolean video = ((this.validFiles & 8) > 0);
                if (video && mimetype[0].equals("video")) return true;
                
                return false;
        }

}
