package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices;

import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.RoomStatus;

public class SmartTemperatureProducer extends FloatSensorDevice implements SensorLabel {
	
	
	private RoomStatus roomStatus;
	private float tempChangePerHour = 0;
	private float trueTemperature = 10f;
	private SmartTemperatureSensor sensor;
	
	public SmartTemperatureProducer(String name, RoomStatus room, int updateInterval, SmartTemperatureSensor sensor) {
		super(name, updateInterval);
		roomStatus = room;
		this.sensor = sensor;
	}
	
	@Override
	protected void updateData() {
		/**
		 * Slowly move room temperature to producer temperature
		 */
		float cTemp = roomStatus.getTemperature();
		if(tempChangePerHour > 0) {
			roomStatus.setTemperature(Math.min(cTemp, cTemp + tempChangePerHour * (float)super.updateInterval/3600000f));
		} else if(tempChangePerHour < 0) {
			roomStatus.setTemperature(Math.max(cTemp, cTemp + tempChangePerHour * (float)super.updateInterval/3600000f));
		}
		if(roomStatus.getTemperature() == getFSval()) {
			tempChangePerHour = 0;
		}
	}

	public float getTrueTemperature() {
		return trueTemperature;
		
	}

	public void setTrueTemperature(float trueTemperature) {
		this.trueTemperature = trueTemperature;
		/*
		 * Change temperature in 1 hour to target temperature 
		 */
		tempChangePerHour = trueTemperature-roomStatus.getTemperature();
	}

	@Override
	public String getSensorLabel() {
		return "" + sensor.getFSval();
	}
	
}
