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

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.pojo.wz.HourRecordData;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.WServiceUtil;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.service.HourRecordService;

@Service("NowWeatherQuery")
@SuppressWarnings({"unchecked", "rawtypes"})
public class NowWeatherQuery extends BaseBusinessService<Map> implements BusinessService {
	private static Set<String> typeSet = new HashSet<>(Arrays.asList("all", "weather", "air"));
	@Resource
	private HourRecordService hourDataService;
	
	@Override
	public void handle(Map dto, List content) throws Exception {

		String cityName = (String) dto.get("cityName");
		String projectId = (String) dto.get("projectId");
		String cityCode = (String) dto.get("cityCode");
		String dataType = (String) dto.get("dataType");

		if (StringUtils.isEmpty(projectId) && StringUtils.isEmpty(cityCode) && StringUtils.isEmpty(cityName)) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		
		if(StringUtils.isEmpty(dataType)) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		
		if(!typeSet.contains(dataType)) {
			throw new Exception(Const.ErrorTag.paramUnrecognized + ":dataType=" + dataType);
		}

		Map<String, Object> resultMap = new HashMap<>();
		String location = ConfigUtil.getHourCity(cityName, cityCode, projectId);
		
		if(null != location) {
			HourRecordData obj = hourDataService.getPresent(location);
			if(null != obj) {
				if("all".equals(dataType) || "air".equals(dataType)) {
					resultMap.put("no2", obj.getNo2());
					resultMap.put("pm25", obj.getPm25());
					resultMap.put("o3", obj.getO3());
					resultMap.put("so2", obj.getSo2());
					resultMap.put("last_update", obj.getLastUpdateAir());
					resultMap.put("aqi", obj.getAqi());
					resultMap.put("pm10", obj.getPm10());
					resultMap.put("primary_pollutant", obj.getPrimary_pollutant());
					resultMap.put("co", obj.getCo());
					resultMap.put("quality", obj.getQuality());
				}
				
				if("all".equals(dataType) || "weather".equals(dataType)) {
					resultMap.put("code", obj.getCode());
					resultMap.put("visibility", obj.getVisibility());
					resultMap.put("wind_direction", obj.getWind_direction());
					resultMap.put("pressure", obj.getPressure());
					resultMap.put("clouds", obj.getClouds());
					resultMap.put("feels_like", obj.getFeels_like());
					resultMap.put("dew_point", obj.getDew_point());
					resultMap.put("wind_scale", obj.getWind_scale());
					resultMap.put("last_update", obj.getLastUpdateNow());
					resultMap.put("temperature", obj.getTemperature());
					resultMap.put("humidity", obj.getHumidity());
					resultMap.put("wind_direction_degree", obj.getWind_direction_degree());
					resultMap.put("wind_speed", obj.getWind_speed());
					resultMap.put("text", obj.getText());
				}
			}
		}
		
		if(resultMap.isEmpty()) {
			location = ConfigUtil.getLocation(cityName, cityCode, projectId);
			if("all".equals(dataType) || "air".equals(dataType)) {
				try {
					Map<String, Object> dataMap = WServiceUtil.getNowAir(location);
					if (!dataMap.isEmpty()) {
						resultMap.putAll(dataMap);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if("all".equals(dataType) || "weather".equals(dataType)) {
				try {
					Map<String, Object> dataMap = WServiceUtil.getNowWeather(location);
					if (!dataMap.isEmpty()) {
						resultMap.putAll(dataMap);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		content.add(resultMap);
	}
}
