package com.persagy.functions.weather.business.v2021;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.persagy.framework.construct.RequestProcessor;
import com.persagy.framework.dto.DataQueryParam;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.framework.util.WServiceUtil;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.service.HourRecordService;

/**
 * 24小时(含当前小时)预报
 */
@Service("predictHour")
public class QueryPredictHour implements RequestProcessor {
	@Resource
	private HourRecordService hourDataService;

	@Override
	public Object handle(DataQueryParam param) throws Exception {
		List<Map<String, Object>> resultList = new ArrayList<>();
		
		String location = ConfigUtil.getLocation(param.cityName, param.cityCode, param.projectId);
		if(null == location) {
			throw new Exception(Const.ErrorTag.cityNotFound);
		}
		
		try {
			Map<String, Object> dataMap = WServiceUtil.getHour24PredictWithLocation(location);
			if (!dataMap.isEmpty()) {
				List<Map<String, Object>> hourly = (List<Map<String, Object>>) dataMap.get("hourly");
				for(Map<String, Object> item : hourly) {
					Date hourTime = DateUtils.t8DateStr2Date(item.get("time")+"");
					
					Map<String, Object> resultItem = new HashMap<>();
					resultItem.put("temperature", item.get("temperature"));
					resultItem.put("text", item.get("text"));
					resultItem.put("code", item.get("code"));
					resultItem.put("humidity", item.get("humidity"));
					resultItem.put("windDirection", item.get("wind_direction"));
					resultItem.put("windSpeed", item.get("wind_speed"));
					resultItem.put("time", hourTime.getTime());
					
					resultList.add(resultItem);
				}
			}
		} catch (Exception e) {
			throw new Exception(Const.ErrorTag.service3rd);
		}
		
		return resultList;
	}
}