package org.bench4q.agent.datacollector.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.bench4q.agent.scenario.engine.BehaviorResult;
import org.bench4q.agent.scenario.engine.PageResult;
import org.bench4q.share.models.agent.statistics.AgentPageBriefModel;

public class PageResultCollector extends AbstractDataCollector {
	Map<Integer, PageBrief> pageBriefMap;

	private Map<Integer, PageBrief> getPageBriefMap() {
		return pageBriefMap;
	}

	private void setPageBriefMap(Map<Integer, PageBrief> pageBriefMap) {
		this.pageBriefMap = pageBriefMap;
	}

	public PageResultCollector() {
		this.setPageBriefMap(new HashMap<Integer, PageBrief>());
	}

	public void add(PageResult pageResult) {
		if (pageResult == null || pageResult.getPageId() < 0) {
			return;
		}
		PageBrief pageBrief = guardTheValueOfThePageIdExists(pageResult
				.getPageId());
		pageBrief.countThisTime++;
		pageBrief.countFromBegin++;
		pageBrief.totalResponseTimeThisTime += pageResult.getExecuteRange();
		pageBrief.latesTimeResponseTime = pageResult.getExecuteRange();
		if (pageResult.getExecuteRange() > pageBrief.maxResponseTimeFromBegin) {
			pageBrief.maxResponseTimeFromBegin = pageResult.getExecuteRange();
		}
		if (pageResult.getExecuteRange() < pageBrief.minResponseTimeFromBegin) {
			pageBrief.minResponseTimeFromBegin = pageResult.getExecuteRange();
		}
	}

	private synchronized PageBrief guardTheValueOfThePageIdExists(int pageId) {
		if (!this.getPageBriefMap().containsKey(pageId)) {
			this.getPageBriefMap().put(pageId, new PageBrief());
		}
		return this.getPageBriefMap().get(pageId);
	}

	public Object getPageBriefStatistics(int pageId) {
		PageBrief pageBrief = guardTheValueOfThePageIdExists(pageId);
		AgentPageBriefModel result = new AgentPageBriefModel();
		result.setCountFromBegin(pageBrief.countFromBegin);
		result.setCountThisTime(pageBrief.countThisTime);
		result.setMaxResponseTimeFromBegin(pageBrief.maxResponseTimeFromBegin);
		result.setMinResponseTimeFromBegin(pageBrief.minResponseTimeFromBegin);
		result.setTotalResponseTimeThisTime(pageBrief.totalResponseTimeThisTime);
		result.setPageId(pageId);
		long nowTime = new Date().getTime();
		result.setTimeFrame(nowTime - pageBrief.lastSampleTime);
		result.setLatestResponseTime(pageBrief.latesTimeResponseTime);
		pageBrief.resetTemperatyField();
		return result;
	}

	@Override
	public void add(BehaviorResult behaviorResult) {
	}

	@Override
	protected String calculateSavePath(BehaviorResult behaviorResult) {
		return null;
	}

	@Override
	public Object getScenarioBriefStatistics() {
		return null;
	}

	@Override
	public Map<Integer, BehaviorStatusCodeResult> getBehaviorBriefStatistics(
			int id) {
		return null;
	}

	public class PageBrief {
		public long lastSampleTime;
		public long countThisTime;
		public long totalResponseTimeThisTime;
		public long maxResponseTimeFromBegin;
		public long minResponseTimeFromBegin;
		public long countFromBegin;
		public long latesTimeResponseTime;

		public PageBrief() {
			resetTemperatyField();
			this.maxResponseTimeFromBegin = Long.MIN_VALUE;
			this.minResponseTimeFromBegin = Long.MAX_VALUE;
			this.countFromBegin = 0;
			this.latesTimeResponseTime = 0;
		}

		public void resetTemperatyField() {
			this.lastSampleTime = new Date().getTime();
			this.countThisTime = 0;
			this.totalResponseTimeThisTime = 0;
		}
	}
}
