package com.hcks.cmfds.action.health;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.hcks.cmfds.Constant;
import com.hcks.cmfds.commons.util.JsonUtils;
import com.hcks.cmfds.commons.util.StringUtils;
import com.hcks.cmfds.core.web.action.PaginationAction;
import com.hcks.cmfds.model.Curvefittingsub;
import com.hcks.cmfds.model.Windpowerstation;
import com.hcks.cmfds.model.Windturbine;
import com.hcks.cmfds.service.CacheContext;
import com.hcks.cmfds.service.health.CurvefittingsubService;
import com.hcks.cmfds.vo.QxpcVo;

@Namespace("/wtcurve")
@Result(name = "json", type = "json", params = { "textName", "json" })
public class WindturbineCurveAction extends PaginationAction  {
	private static final long serialVersionUID = 1L;

	private List<Windpowerstation> wps;
	private Date recodedate;
	private String json;
	private String wpId;
	private String wtId;
	
	private List<QxpcVo> vos;
	
	private CurvefittingsubService curvefittingsubService;
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	private void inital() {
		wps = new ArrayList<Windpowerstation>();

		for (Windpowerstation station : CacheContext.wpls) {
			if (station.getId().endsWith("FDC")) {
				wps.add(station);
			}
		}

		if (StringUtils.empty(wpId)) {
			wpId = ((Windpowerstation) wps.get(0)).getId();
		}
		if (StringUtils.empty(recodedate)) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DAY_OF_MONTH, -1);
			recodedate = cal.getTime();
		}

		vos = new ArrayList<QxpcVo>();
	}
	@Action(value = "wtcurveindex", results = { @Result(name = "success", location = "/pages/health/wtcurve.jsp") })
	public String wtcurveindex() {
		inital();
		return SUCCESS;
	}
	@Action(value = "wtcurvelist")
	public String wtcurvelist() throws Exception {

		inital();
		Calendar c = Calendar.getInstance();
		c.setTime(recodedate);
	
		List<Windturbine> wtls = null;
		if (CacheContext.wtsmap.containsKey(wpId)) {
			wtls = CacheContext.wtsmap.get(wpId);
		}

		if (!wtls.isEmpty()) {
			Map<String, List<Curvefittingsub>> cfmap1 = curvefittingsubService.findCurvefittingmapByDate(c.getTime(), 3.0, 5.0);
			Map<String, List<Curvefittingsub>> cfmap2 = curvefittingsubService.findCurvefittingmapByDate(c.getTime(), 5.0, 11.0);
			Map<String, List<Curvefittingsub>> cfmap3 = curvefittingsubService.findCurvefittingmapByDate(c.getTime(), 11.0, 25.0);
			for (Windturbine wt : wtls) {
				QxpcVo vo = new QxpcVo();

				vo.setWtId(wt.getId());
				vo.setRd(df.format(recodedate));
				if (cfmap1.containsKey(wt.getId())) {
					double qxpc1 = curvefittingsubService.getNuberValue(cfmap1, wt);
					vo.setQxpc1(qxpc1);
				} else {
					vo.setQxpc1(0.0);
				}

				if (cfmap2.containsKey(wt.getId())) {
					double qxpc2 = curvefittingsubService.getNuberValue(cfmap2, wt);
					vo.setQxpc2(qxpc2);
				} else {
					vo.setQxpc2(0.0);
				}

				if (cfmap3.containsKey(wt.getId())) {
					double qxpc3 = curvefittingsubService.getNuberValue(cfmap3, wt);
					vo.setQxpc3(qxpc3);
				} else {
					vo.setQxpc3(0.0);
				}
				vos.add(vo);
			}
		}

		if (null != vos && !vos.isEmpty()) {

			json = JsonUtils.wrapListForGrid(vos, getPage());
		} else {
			json = JsonUtils.wrapListForGrid(new ArrayList<QxpcVo>(), getPage());
		}

		return JSON;
	}

	

	public List<Windpowerstation> getWps() {
		return wps;
	}

	public void setWps(List<Windpowerstation> wps) {
		this.wps = wps;
	}

	public Date getRecodedate() {
		return recodedate;
	}

	public void setRecodedate(Date recodedate) {
		this.recodedate = recodedate;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getWpId() {
		return wpId;
	}

	public void setWpId(String wpId) {
		this.wpId = wpId;
	}

	public String getWtId() {
		return wtId;
	}

	public void setWtId(String wtId) {
		this.wtId = wtId;
	}

	public List<QxpcVo> getVos() {
		return vos;
	}

	public void setVos(List<QxpcVo> vos) {
		this.vos = vos;
	}
	public CurvefittingsubService getCurvefittingsubService() {
		return curvefittingsubService;
	}
	public void setCurvefittingsubService(
			CurvefittingsubService curvefittingsubService) {
		this.curvefittingsubService = curvefittingsubService;
	}


}