package cn.edu.seu.ise.dca.app.php.model;

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

/**
 * PHPFile represents the PHP file in PHP web application
 * @author Dong Qiu
 *
 */
public class PHPFile {
	private String url;
	private long revision;
	private long lines;
	private long actualLines;
	private List<PHPClass> classes;
	private List<PHPFunction> globalFunctions;
	
	public PHPFile(String url, long revision) {
		super();
		this.url = url;
		this.revision = revision;
		this.classes = new ArrayList<PHPClass>();
		this.globalFunctions = new ArrayList<PHPFunction>();
	}

	public PHPFile(String name, long revision, long lines) {
		this(name, revision);
		this.lines = lines;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public long getRevision() {
		return revision;
	}

	public void setRevision(long revision) {
		this.revision = revision;
	}

	public long getLines() {
		return lines;
	}

	public void setLines(long lines) {
		this.lines = lines;
	}
	
	public long getActualLines() {
		return actualLines;
	}

	public void setActualLines(long actualLines) {
		this.actualLines = actualLines;
	}

	public List<PHPClass> getClasses() {
		return classes;
	}
	
	public List<PHPFunction> getGlobalFunctions() {
		return globalFunctions;
	}

	public void setClasses(List<PHPClass> classes) {
		this.classes = new ArrayList<PHPClass>(classes);
	}
	

	public void setGlobalFunctions(List<PHPFunction> globalFunctions) {
		this.globalFunctions = new ArrayList<PHPFunction>(globalFunctions);
	}


	public void addClass(PHPClass phpclass) {
		this.classes.add(phpclass);
	}
	
	public void addGlobalFunction(PHPFunction globalFunction) {
		this.globalFunctions.add(globalFunction);
	}
	
	/**
	 * Return the list of member functions
	 * @return
	 */
	public List<PHPFunction> getMemberFunctions() {
		List<PHPFunction> memberFunctions = new ArrayList<PHPFunction>();
		for(PHPClass phpClass : this.classes) {
			for(PHPFunction phpFunction : phpClass.getFunctions()) {
				memberFunctions.add(phpFunction);
			}
		}
		return memberFunctions;
	}
	
	/**
	 * Get the number of the classes defined in PHP file
	 * @return
	 */
	public int sizeOfClasses() {
		return this.classes.size();
	}
	
	/**
	 * Get the number of the global functions defined in PHP files
	 * @return
	 */
	public int sizeOfGlobalFunctions() {
		return this.globalFunctions.size();
	}
	
	/**
	 * Get the number of member functions defined in PHP files
	 * @return
	 */
	public int sizeOfMemberFunctions() {
		int size = 0;
		//Get the size of member functions in all classes
		if(this.classes != null) {
			for(PHPClass phpClass : classes)
				size += phpClass.sizeOfFunctions();
		}
		return size;
	}
	
	/**
	 * Get the number of the all functions (including global functions and member functions)
	 * @return
	 */
	public int sizeOfFunctions() {
		return this.sizeOfMemberFunctions() + this.sizeOfGlobalFunctions();
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("[revision:").append(this.getRevision()).append(", url:").append(this.getUrl())
			.append(", lines:").append(this.getLines()).append(", classes:").append(this.sizeOfClasses())
			.append(", global functions:").append(this.sizeOfGlobalFunctions()).append("]");
		return buffer.toString();
	}
}
