package cz.hsrs.db.model.composite;

import java.sql.ResultSet;
import java.sql.SQLException;

import cz.hsrs.db.DBObject;
import cz.hsrs.db.model.Phenomenon;
import cz.hsrs.db.pool.SQLExecutor;
import cz.hsrs.db.util.SensorUtil;

/**
 * Sensor generated by hbm2java
 */
public class UnitSensor implements DBObject{

	private long sensorId;
	private String sensorName;
	private String sensorType;
	private Phenomenon phenomenon;
	
	private String firstObservationTime;
	private String lastObservationTime;
	
	
	public UnitSensor(long sensorId, String sensorName, String sensorType,
			Phenomenon phenomenon, long unitId) throws SQLException {
		super();
		this.sensorId = sensorId;
		this.sensorName = sensorName;
		this.sensorType = sensorType;
		this.phenomenon = phenomenon;
		this.firstObservationTime = findFirstObservationTimeDB(unitId, sensorId);
		this.lastObservationTime = findLastObservationTimeDB(unitId, sensorId);
	}
	
	public UnitSensor(ResultSet set) throws SQLException{
		
		this.sensorId = set.getLong("sensor_id");
		this.sensorName = set.getString("sensor_name");
		this.sensorType = set.getString("sensor_type");	
		this.firstObservationTime = findFirstObservationTimeDB(set.getLong("unit_id"),sensorId);
		this.lastObservationTime = findLastObservationTimeDB(set.getLong("unit_id"),sensorId);
		SensorUtil sUtil = new SensorUtil();
		this.phenomenon = sUtil.getPhenomenonById(set.getString("phenomenon_id"));
	}
	
	public UnitSensor() throws SQLException{
		
	}	
	
	@Override
	public DBObject getDBObject(ResultSet set) throws SQLException {
		return new UnitSensor(set);
	}

	public long getSensorId() {
		return sensorId;
	}
	public void setSensorId(long sensorId) {
		this.sensorId = sensorId;
	}
	public String getSensorName() {
		return sensorName;
	}
	public void setSensorName(String sensorName) {
		this.sensorName = sensorName;
	}
	public String getSensorType() {
		return sensorType;
	}
	public void setSensorType(String sensorType) {
		this.sensorType = sensorType;
	}
	public Phenomenon getPhenomenon() {
		return phenomenon;
	}
	public void setPhenomenon(Phenomenon phenomenon) {
		this.phenomenon = phenomenon;
	}
	public String getFirstObservationTime() {
		return firstObservationTime;
	}
	public String getLastObservationTime() {
		return lastObservationTime;
	}

	private String findFirstObservationTimeDB(long unitId, long sensorId) throws SQLException{
		String query = "SELECT first_obs FROM units_to_sensors WHERE unit_id = "+unitId+" AND sensor_id = "+sensorId+";";
		ResultSet res = SQLExecutor.getInstance().executeQuery(query);
		if(res.next() == true){
			String time = res.getString(1);
			boolean wasNull = res.wasNull();
			if (wasNull == false){
				return time;
			}
			else{
				return "unknown";
			}
		}
		else{
			return "unknown";
		}		
	}
	
	private String findLastObservationTimeDB(long unitId, long sensorId) throws SQLException{
		String query = "SELECT last_obs FROM units_to_sensors WHERE unit_id = "+unitId+" AND sensor_id = "+sensorId+";";
		ResultSet res = SQLExecutor.getInstance().executeQuery(query);
		if(res.next() == true){
			String time = res.getString(1);
			boolean wasNull = res.wasNull();
			if (wasNull == false){
				return time;
			}
			else{
				return "unknown";
			}
		}
		else{
			return "unknown";
		}			
	}	
}
