package com.zhiwei.manage.servlet;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.support.RequestPartServletServerHttpRequest;

import com.alibaba.fastjson.JSON;
import com.zhiwei.manage.bean.Channel;
import com.zhiwei.manage.bean.Data;
import com.zhiwei.manage.bean.EasyNews;
import com.zhiwei.manage.bean.JsonResult;
import com.zhiwei.manage.service.DataService;
import com.zhiwei.manage.service.ServerService;
import com.zhiwei.manage.util.Change;
import com.zhiwei.manage.util.TimeUtil;
import com.zhiwei.manage.util.Tools;

@Controller
public class DataController {

	private static final Log log = LogFactory.getLog(DataController.class);

	@Autowired
	private DataService dataService;
	@Autowired
	private ServerService serverService;

	@RequestMapping(value = "/getData", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getData(HttpServletRequest request, HttpServletResponse response) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -75);
		List<Data> list = dataService.findBytempName(c.getTime(), new Date(), "uc");
		return JSON.toJSONString(list);
	}

	@RequestMapping(value = "/getDataBySource", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getDataBySource(HttpServletRequest request, HttpServletResponse response,@RequestBody Map<String, Object> map) {
		long start=(long) map.get("start");
		long end=(long) map.get("end");
		String source=(String) map.get("source");
		Calendar startd = Calendar.getInstance();
		startd.setTimeInMillis(start);
		Calendar endd = Calendar.getInstance();
		endd.setTimeInMillis(end);
		List<Data> list = dataService.findBySource(startd.getTime(), endd.getTime(), source);
		return JSON.toJSONString(list);
	}

	@RequestMapping(value = "/getDataByTempName", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getDataByTempName(HttpServletRequest request, HttpServletResponse response,@RequestBody Map<String, Object> map)
			throws UnsupportedEncodingException {
		long start=(long) map.get("start");
		long end=(long) map.get("end");
		String timeType=(String) map.get("timeType");
		String method=(String) map.get("method");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar startd = Calendar.getInstance();
		Calendar endTime = Calendar.getInstance();
		if (method == null) {
			JsonResult result = new JsonResult(false, 200, "参数不全", "");
			return JSON.toJSONString(result);
		} else if (timeType.equals("hour")) {
			// 小时数据
			if (start == 0) {
				startd.set(Calendar.HOUR, -10);
			} else {
				startd.setTimeInMillis(start);
				endTime.setTimeInMillis(start);
			}
			String source = "";
			List<Data> list = new ArrayList<Data>();
			startd.add(Calendar.HOUR, -8);
			while (endTime.getTimeInMillis() < end) {
				endTime.add(Calendar.HOUR, 1);
				List<EasyNews> newsList = new ArrayList<>();
				if(method.equals("type")){
					source=(String) map.get("type");
					newsList=dataService.findByAccurateType(startd.getTime(), endTime.getTime(), source);
				}else{
					if (method.equals("source")) {
						source = (String) map.get("source");
						newsList = dataService.findBySourceFromLibrary(startd.getTime(), endTime.getTime(), source);
					} else {
						source = (String) map.get("tempName");
						newsList = dataService.findByTempName(startd.getTime(), endTime.getTime(), source);
					}
				}
				Data data = Change.changeNewsToDate(newsList, startd.getTime());
				list.add(data);
				startd.add(Calendar.HOUR, 1);
			}
			return JSON.toJSONString(Tools.beResult(list, source));
		} else {
			if (start == 0) {
				start = startd.getTimeInMillis() - 1000 * 3600 * 24 * 7;
				startd.setTimeInMillis(start);
			} else {
				startd.setTimeInMillis(start);
				endTime.setTimeInMillis(end);
			}
			System.out.println(sdf.format(startd.getTime()));
			System.out.println(sdf.format(endTime.getTime()));
			Channel channel = new Channel();
			List<Data> list = new ArrayList<Data>();
			if (method.equals("source")) {
				String source = (String) map.get("source");
				list = dataService.findBySource(startd.getTime(), endTime.getTime(), source);
				channel.setValue(source);
			} else {
				String tempName = (String) map.get("tempName");
				list = dataService.findBytempName(startd.getTime(), endTime.getTime(), tempName);
				channel.setValue(tempName);
			}
			channel.setList(list);
			JsonResult result = new JsonResult(true, 200, "返回成功", channel);
			return JSON.toJSONString(result);
		}
	}

	@RequestMapping(value = "/getDataAll", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAllData(@RequestBody Map<String, Object> map) {
		long start=(long) map.get("start");
		long end=(long) map.get("end");
		try {
			if (start == 0) {
				List<Channel> channel = serverService.findDisChannel();
				List<Channel> list = new ArrayList<>();
				Calendar startTime = Calendar.getInstance();
				start = startTime.getTimeInMillis() - 1000 * 3600 * 24 * 7;
				startTime.setTimeInMillis(start);
				Calendar endTime = Calendar.getInstance();
				for(Channel chan:channel){
					List<Data> weixin = dataService.findByPt(startTime.getTime(), endTime.getTime(), chan.getPt());
					chan.setList(weixin);
					chan.setPt(chan.getPt());
					chan.setValue(chan.getValue());
					list.add(chan);
				}
				JsonResult result = new JsonResult(true, 200, "请求成功", list);
				return JSON.toJSONString(result);
			} else {
				Calendar startTime = Calendar.getInstance();
				startTime.setTimeInMillis(start);
				Calendar endTime = Calendar.getInstance();
				endTime.setTimeInMillis(end);
				List<Channel> channel = serverService.findDisChannel();
				List<Channel> list = new ArrayList<>();
				for(Channel chan:channel){
					List<Data> weixin = dataService.findByPt(startTime.getTime(), endTime.getTime(), chan.getPt());
					chan.setList(weixin);
					chan.setPt(chan.getPt());
					chan.setValue(chan.getValue());
					list.add(chan);
				}
				JsonResult result = new JsonResult(true, 200, "请求成功", list);
				return JSON.toJSONString(result);
			}
		} catch (Exception e) {
			JsonResult result = new JsonResult(false, 500, e.getMessage(), "");
			return JSON.toJSONString(result);
		}
	}

	@RequestMapping(value = "/getSource", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAllDataCountByTime(@RequestBody Map<String, Object> map) {
		String value=(String) map.get("value");
		String method=(String) map.get("method");
		if (method != null) {
			if (method.equals("tempName")) {
				List<String> list = dataService.findTmpNameByLikeParam(value);
				JsonResult result = new JsonResult(true, 200, "请求成功", list);
				return JSON.toJSONString(result);
			} else if(method.equals("type")){
				List<String> list = dataService.findTypeByLikeParam(value);
				JsonResult result = new JsonResult(true, 200, "请求成功", list);
				return JSON.toJSONString(result);
			}else{
				JsonResult result = new JsonResult(true, 200, "请求成功", new ArrayList<String>());
				return JSON.toJSONString(result);
			}
		}
		JsonResult result = new JsonResult(true, 200, "参数错误");
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/getTest", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getTestData(@RequestBody Map<String, Object> map){
		String value=(String) map.get("value");
		String method=(String) map.get("method");
		Calendar startTime=Calendar.getInstance();
		startTime.add(Calendar.DATE, -6);
		Calendar endTime=Calendar.getInstance();
		List<Data> list=dataService.findByMethod(startTime.getTime(), endTime.getTime(), method, value);
		JsonResult result = new JsonResult(true, 200, "获取成功",list);
		return JSON.toJSONString(result);
	}
}
