package cn.edu.seu.ise.dca.filter;

import java.util.ArrayList;
import java.util.List;

import cn.edu.seu.ise.dca.model.ChangedFile;
import cn.edu.seu.ise.dca.model.Revision;

public class RevisionFilter {
	private List<String> includes;
	private List<String> excludes;
	
	public RevisionFilter() {
		includes = new ArrayList<String>();
		excludes = new ArrayList<String>();
	}

	public RevisionFilter(List<String> includes, List<String> excludes) {
		this();
		if(includes != null) {
			this.includes = includes;
		}
		if(excludes != null) {
			this.excludes = excludes;
		}
	}
	
	public void setIncludes(List<String> includes) {
		this.includes = includes;
	}

	public void setExcludes(List<String> excludes) {
		this.excludes = excludes;
	}

	public Revision filter(Revision revision) {
		Revision updatedRevision = new Revision(revision.getId(), revision.getAuthor(), revision.getCommitDate());
		if(includes.isEmpty() && excludes.isEmpty()) {
			for(ChangedFile file : revision.getChangedFiles()) 
				updatedRevision.addChangedFile(file);
		} else if(includes.isEmpty() && !excludes.isEmpty()) {
			for(ChangedFile file : revision.getChangedFiles()) {
				boolean flag = true;
				for(String exclude : excludes) {
					if(file.getUrl().startsWith(exclude)) {
						flag = false;
					}
				}
				if(flag)
					updatedRevision.addChangedFile(file);
			}
		} else if(!includes.isEmpty() && excludes.isEmpty()) {
			for(ChangedFile file : revision.getChangedFiles()) {
				boolean flag = false;
				for(String include : includes) {
					if(file.getUrl().startsWith(include)) {
						flag = true;
					}
				}
				if(flag)
					updatedRevision.addChangedFile(file);
			}
		} else {
			for(ChangedFile file : revision.getChangedFiles()) {
				boolean flag = false;
				for(String include : includes) {
					if(file.getUrl().startsWith(include)) {
						flag = true;
					}
				}
				for(String exclude : excludes) {
					if(file.getUrl().startsWith(exclude)) {
						flag = false;
					}
				}
				if(flag)
					updatedRevision.addChangedFile(file);
			}
		}
		
		return updatedRevision;
	}
	
	
}
