/**
 * 
 */
package cz.hsrs.rest.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import cz.hsrs.db.pool.SQLExecutor;
import cz.hsrs.rest.beans.LastObsTimestampBean;

/**
 * @author mkepka
 *
 */
public class ObservationRestUtil {

	/**
	 * 
	 * @param groupName
	 * @param groupId
	 * @return
	 * @throws SQLException
	 */
	public static List<LastObsTimestampBean> getLastTimestampPerGroup(String groupName, Integer groupId) throws SQLException {
		String query;
		if(groupName == null && groupId != null) {
			query = "SELECT * FROM public.units_sensors_lastobs WHERE id = "+groupId+";";
		}
		else if(!groupName.isEmpty() && groupId == null) {
			query = "SELECT * FROM public.units_sensors_lastobs WHERE group_name = '"+groupName+"';";
		}
		else {
			throw new SQLException("Group identification must be provided!");
		}
		List<LastObsTimestampBean> obsList = new LinkedList<LastObsTimestampBean>();
		
		ResultSet res = SQLExecutor.getInstance().executeQuery(query);
		if(res != null) {
			while (res.next()) {
				LastObsTimestampBean obs = new LastObsTimestampBean(
						res.getInt("id"),
						res.getString("group_name"),
						res.getLong("unit_id"),
						res.getString("description"),
						res.getLong("sensor_id"),
						res.getString("sensor_name"),
						res.getString("last_timestamp")
						);
				obsList.add(obs);
			}
		}
		return obsList;
	}
	
	/**
	 * 
	 * @param unitIds
	 * @return
	 * @throws SQLException
	 */
	public static List<LastObsTimestampBean> getLastTimestampPerUnit(String unitIds) throws SQLException {
		String query;
		if(unitIds != null) {
			query = "SELECT DISTINCT ON (unit_id, sensor_id) unit_id, description, sensor_id, sensor_name, last_timestamp"
					+ " FROM public.units_sensors_lastobs"
					+ " WHERE unit_id IN ("+unitIds+")"
					+ " ORDER BY unit_id, sensor_id;";
		}
		else {
			throw new SQLException("Unit identification must be provided!");
		}
		List<LastObsTimestampBean> obsList = new LinkedList<LastObsTimestampBean>();
		
		ResultSet res = SQLExecutor.getInstance().executeQuery(query);
		if(res != null) {
			while (res.next()) {
				LastObsTimestampBean obs = new LastObsTimestampBean(
						res.getLong("unit_id"),
						res.getString("description"),
						res.getLong("sensor_id"),
						res.getString("sensor_name"),
						res.getString("last_timestamp")
						);
				obsList.add(obs);
			}
		}
		return obsList;
	}
	
	/**
	 * 
	 * @param sensorIds
	 * @return
	 * @throws SQLException
	 */
	public static List<LastObsTimestampBean> getLastTimestampPerSensor(String sensorIds) throws SQLException {
		String query;
		if(sensorIds != null) {
			query = "SELECT DISTINCT ON (unit_id, sensor_id) unit_id, description, sensor_id, sensor_name, last_timestamp"
					+ " FROM public.units_sensors_lastobs"
					+ " WHERE sensor_id IN ("+sensorIds+")"
					+ " ORDER BY unit_id, sensor_id;";
		}
		else {
			throw new SQLException("Sensor identification must be provided!");
		}
		List<LastObsTimestampBean> obsList = new LinkedList<LastObsTimestampBean>();
		
		ResultSet res = SQLExecutor.getInstance().executeQuery(query);
		if(res != null) {
			while (res.next()) {
				LastObsTimestampBean obs = new LastObsTimestampBean(
						res.getLong("unit_id"),
						res.getString("description"),
						res.getLong("sensor_id"),
						res.getString("sensor_name"),
						res.getString("last_timestamp")
						);
				obsList.add(obs);
			}
		}
		return obsList;
	}
}