package org.bench4q.agent.scenario.engine;

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

public class PageResult {
	private int pageId;
	private long pageStartTime;
	private long pageEndTime;
	private long executeRange;

	public int getPageId() {
		return pageId;
	}

	private void setPageId(int pageId) {
		this.pageId = pageId;
	}

	private long getPageStartTime() {
		return pageStartTime;
	}

	private void setPageStartTime(long pageStartTime) {
		this.pageStartTime = pageStartTime;
	}

	public long getPageEndTime() {
		return pageEndTime;
	}

	private void setPageEndTime(long pageEndTime) {
		this.pageEndTime = pageEndTime;
	}

	public long getExecuteRange() {
		return executeRange;
	}

	private void setExecuteRange(long executeRange) {
		this.executeRange = executeRange;
	}

	private PageResult() {
		init();
	}

	private void init() {
		this.setPageStartTime(Long.MAX_VALUE);
		this.setPageEndTime(Long.MIN_VALUE);
		this.setExecuteRange(0);
	}

	public static PageResult buildPageResult(int pageId,
			List<BehaviorResult> behaviorResults) {
		PageResult result = new PageResult();
		result.setPageId(pageId);
		if (behaviorResults == null) {
			behaviorResults = new ArrayList<BehaviorResult>();
		}
		for (BehaviorResult behaviorResult : behaviorResults) {
			if (behaviorResult.getStartDate().getTime() < result
					.getPageStartTime()) {
				result.setPageStartTime(behaviorResult.getStartDate().getTime());
			}
			if (behaviorResult.getEndDate().getTime() > result.getPageEndTime()) {
				result.setPageEndTime(behaviorResult.getEndDate().getTime());
			}
			// Page excuteRange rely on the behaviors' execute way, if it's
			// executed in batch, i should take the longest behavior in batch
			// to calculate this One.
		}
		result.setExecuteRange(result.getPageEndTime()
				- result.getPageStartTime());
		return result;
	}

	public static PageResult buildPageResult(int pageId, long startTime,
			long endTime) {
		PageResult result = new PageResult();
		result.setPageId(pageId);
		result.setPageStartTime(startTime);
		result.setPageEndTime(endTime);
		result.setExecuteRange(endTime - startTime);
		return result;
	}
}
