package com.persagy.framework.dto;

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

import com.persagy.core.enumeration.SpecialOperator;
import com.persagy.framework.util.DateUtils;
import com.persagy.framework.util.DateUtils.TimeNum;
import com.persagy.functions.weather.constant.Const.ErrorTag;

public class DataQueryParam {
	public String cityName;
	public String projectId;
	public String cityCode;
	public Double longitude;
	public Double latitude;
	public Date time;
	public TimeSpanCondition timeSpan;
	public Map<String, String> fieldEqualMap = new HashMap<>();

	public boolean timeRight(Date timeParam) {
		if(time != null) {
			return time.equals(timeParam);
		}
		
		if(timeSpan != null) {
			boolean leftbol = timeSpan.optLeft == SpecialOperator.$gt ? timeParam.after(timeSpan.timeLeft) : !timeParam.before(timeSpan.timeLeft);
			if(leftbol) {
				return timeSpan.optRight == SpecialOperator.$lt ? timeParam.before(timeSpan.timeRight) : !timeParam.after(timeSpan.timeRight);
			}
		}
		return false;
	}
	
	public boolean timeSpanCheck(int maxDays) throws Exception {
		try {
			long timeSpanNum = timeSpan.timeRight.getTime() - timeSpan.timeLeft.getTime();
			if(timeSpanNum > 0 && timeSpanNum >= (maxDays * TimeNum.day)) {
				return true;
			}
		}catch (Exception e) {
			throw new Exception(ErrorTag.timeSpanLack);
		}
		return false;
	}
	
	public DataQueryParam(Map<String, Object> paramMap) throws Exception {
		cityName = (String) paramMap.get("cityName");
		projectId = (String) paramMap.get("projectId");
		cityCode = (String) paramMap.get("cityCode");
		longitude = (Double) paramMap.get("longitude");
		latitude = (Double) paramMap.get("latitude");
		
		if(cityCheck()) {
			throw new Exception(ErrorTag.paramMis);
		}
		
		Object timeObj = paramMap.get("time");
		if(timeObj != null) {
			try {
				if(timeObj instanceof Map) {
					Map<String, Object> timeSpanMap = (Map<String, Object>) timeObj;
					
					timeSpan = new TimeSpanCondition();
					if(timeSpanMap.containsKey("$gte")) {
						timeSpan.optLeft = SpecialOperator.$gte;
						timeSpan.timeLeft = DateUtils.long2Date((long) timeSpanMap.get("$gte"));
					}else if(timeSpanMap.containsKey("$gt")) {
						timeSpan.optLeft = SpecialOperator.$gt;
						timeSpan.timeLeft = DateUtils.long2Date((long) timeSpanMap.get("$gt"));
					}else {
						throw new Exception(ErrorTag.timeSpanLack);
					}

					if(timeSpanMap.containsKey("$lt")) {
						timeSpan.optRight = SpecialOperator.$lt;
						timeSpan.timeRight = DateUtils.long2Date((long) timeSpanMap.get("$lt"));
					}else if(timeSpanMap.containsKey("$lte")) {
						timeSpan.optRight = SpecialOperator.$lte;
						timeSpan.timeRight = DateUtils.long2Date((long) timeSpanMap.get("$lte"));
					}else {
						throw new Exception(ErrorTag.timeSpanLack);
					}
					
					if(!timeSpan.timeLeft.before(timeSpan.timeRight)) {
						throw new Exception(ErrorTag.timeEndBeforeStart);
					}
				}else if(timeObj instanceof Long) {
					time = DateUtils.long2Date((long) timeObj);
				}else {
					throw new Exception(ErrorTag.paramFormat);
				}
			}catch (Exception e) {
				throw new Exception("time param error: " + e.getMessage());
			}
		}
	}
	
	/**
	 * 判断城市信息参数是否全为null
	 * @return
	 */
	public boolean cityCheck() {
		return cityName==null && projectId==null && cityCode==null && (longitude==null || latitude==null);
	}
	
}