package com.dianping.cat.report.page.metric;

import java.io.IOException;

import javax.servlet.ServletException;

import org.unidal.lookup.annotation.Inject;
import org.unidal.web.mvc.PageHandler;
import org.unidal.web.mvc.annotation.InboundActionMeta;
import org.unidal.web.mvc.annotation.OutboundActionMeta;
import org.unidal.web.mvc.annotation.PayloadMeta;

import com.dianping.cat.consumer.metric.model.entity.MetricReport;
import com.dianping.cat.report.ReportPage;
import com.dianping.cat.report.page.metric.MetricConfig.MetricFlag;
import com.dianping.cat.report.page.model.spi.ModelRequest;
import com.dianping.cat.report.page.model.spi.ModelResponse;
import com.dianping.cat.report.page.model.spi.ModelService;

public class Handler implements PageHandler<Context> {
	@Inject
	private JspViewer m_jspViewer;

	@Inject(type = ModelService.class, value = "metric")
	private ModelService<MetricReport> m_service;

	private static final String TUAN = "TuanGou";

	private MetricReport getReport(Payload payload) {
		String group = payload.getGroup();
		String channel = payload.getChannel();
		String date = String.valueOf(payload.getDate());
		ModelRequest request = new ModelRequest(group, payload.getPeriod()) //
		      .setProperty("date", date);

		if (channel != null) {
			request.setProperty("channel", channel);
		}
		if (m_service.isEligable(request)) {
			ModelResponse<MetricReport> response = m_service.invoke(request);
			MetricReport report = response.getModel();

			return report;
		} else {
			throw new RuntimeException("Internal error: no eligable metric service registered for " + request + "!");
		}
	}

	@Override
	@PayloadMeta(Payload.class)
	@InboundActionMeta(name = "metric")
	public void handleInbound(Context ctx) throws ServletException, IOException {
	}

	@Override
	@OutboundActionMeta(name = "metric")
	public void handleOutbound(Context ctx) throws ServletException, IOException {
		Model model = new Model(ctx);
		Payload payload = ctx.getPayload();
		normalize(model, payload);

		MetricReport report = getReport(payload);
		String channel = payload.getChannel();

		if (report != null) {
			MetricDisplay display = new MetricDisplay(buildTuanGouMetricConfig(channel),channel, report.getStartTime());

			display.visitMetricReport(report);
			model.setDisplay(display);
			model.setReport(report);
		}
		m_jspViewer.view(ctx, model);
	}

	private MetricConfig buildTuanGouMetricConfig(String channel) {
		MetricConfig config = new MetricConfig();

		MetricFlag indexUrl = new MetricFlag("/index", channel, 1, true, false, false, MetricTitle.INDEX);
		MetricFlag detailUrl = new MetricFlag("/detail", channel, 2, true, false, false, MetricTitle.DETAIL);
		MetricFlag payUrl = new MetricFlag("/order/submitOrder", channel, 3, true, false, false, MetricTitle.PAY);
		MetricFlag orderKey = new MetricFlag("order", channel, 4, false, true, false, MetricTitle.ORDER);
		MetricFlag totalKey = new MetricFlag("payment.success", channel, 5, false, true, false, MetricTitle.SUCCESS);
		// MetricFlag sumKey = new MetricFlag("payment.pending", 5, false, true, false);

		config.put(indexUrl);
		config.put(detailUrl);
		config.put(payUrl);
		config.put(orderKey);
		config.put(totalKey);
		return config;
	}

	private void normalize(Model model, Payload payload) {
		payload.setGroup(TUAN);
		model.setIpAddress(payload.getIpAddress());
		model.setAction(Action.VIEW);
		model.setPage(ReportPage.METRIC);
		model.setLongDate(payload.getDate());
		model.setDisplayDomain(payload.getDomain());
		model.setDomain(payload.getDomain());
		model.setGroup(payload.getGroup());
		model.setChannel(payload.getChannel());
	}

	public class MetricTitle {

		public static final String INDEX = "团购首页";

		public static final String DETAIL = "团购详情";

		public static final String PAY = "支付页面";

		public static final String ORDER = "订单创建数量";

		public static final String SUCCESS = "支付金额";

	}

}

@@ -3712,16 +3712,19 @@
  = %22%E5%9B%A2%E8%B4%AD%E9%A6%96%E9%A1%B5
+(%E6%AC%A1)
 %22;%0A%0A%09%09pu
@@ -3762,16 +3762,19 @@
  = %22%E5%9B%A2%E8%B4%AD%E8%AF%A6%E6%83%85
+(%E6%AC%A1)
 %22;%0A%0A%09%09pu
@@ -3809,16 +3809,19 @@
  = %22%E6%94%AF%E4%BB%98%E9%A1%B5%E9%9D%A2
+(%E6%AC%A1)
 %22;%0A%0A%09%09pu
@@ -3860,16 +3860,19 @@
  %22%E8%AE%A2%E5%8D%95%E5%88%9B%E5%BB%BA%E6%95%B0%E9%87%8F
+(%E4%B8%AA)
 %22;%0A%0A%09%09pu
@@ -3911,16 +3911,19 @@
  = %22%E6%94%AF%E4%BB%98%E9%87%91%E9%A2%9D
+(%E5%85%83)
 %22;%0A%0A%09%7D%0A%0A

modify the metric title