package org.openhab.binding.maxcul.internal.messages;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThermostatStateMsg extends BaseMsg {

	final static private int THERMOSTAT_STATE_TIME_PAYLOAD_LEN = 6; /* in bytes */
	final static private int THERMOSTAT_STATE_MEAS_PAYLOAD_LEN = 5; /* in bytes */
	final static private int THERMOSTAT_STATE_SHORT_PAYLOAD_LEN = 3; /* in bytes */

	private static final Logger logger =
			LoggerFactory.getLogger(ThermostatStateMsg.class);

	private double desiredTemperature;
	private int valvePos;
	private boolean dstActive;
	private boolean lanGateway; // unknown what this is for
	private boolean lockedForManualSetPoint;
	private boolean rfError;
	private boolean batteryLow;
	private Double measuredTemperature = null;
	private GregorianCalendar untilDateTime = null;
	private ThermostatControlMode ctrlMode;

	public ThermostatStateMsg(String rawMsg) {
		super(rawMsg);
		logger.debug(this.msgType+" Payload Len -> "+this.payload.length);

		if (this.payload.length == THERMOSTAT_STATE_SHORT_PAYLOAD_LEN ||
				this.payload.length == THERMOSTAT_STATE_MEAS_PAYLOAD_LEN ||
				this.payload.length == THERMOSTAT_STATE_TIME_PAYLOAD_LEN )
		{
			/* extract control mode */
			ctrlMode = ThermostatControlMode.values()[(this.payload[0]&0x3)];
			/* extract DST status */
			dstActive = extractBitFromByte(this.payload[0], 3);;
			/* extract lanGateway */
			lanGateway = extractBitFromByte(this.payload[0], 4);
			/* extract locked status */
			lockedForManualSetPoint = extractBitFromByte(this.payload[0], 5);
			/* extract rferror status */
			rfError = extractBitFromByte(this.payload[0], 6);
			/* extract battery status */
			batteryLow = extractBitFromByte(this.payload[0], 7);
			/* extract valve position */
			valvePos = this.payload[1];
			/* extract desired temperature information */
			desiredTemperature = (this.payload[2] & 0x7f) / 2.0;

			/* handle longer packet */
			if (this.payload.length == THERMOSTAT_STATE_MEAS_PAYLOAD_LEN)
			{
				/* extract measured temperature */
				if (ctrlMode != ThermostatControlMode.TEMPORARY)
				{
					int mTemp = (this.payload[3]&0x80);
					mTemp <<= 1;
					mTemp |= (((int)this.payload[4])&0xff);
					logger.debug("mTemp => "+mTemp);
					measuredTemperature = mTemp/10.0; // temperature over 25.5 uses extra bit in desiredTemperature byte
				}
			} else if (this.payload.length == THERMOSTAT_STATE_TIME_PAYLOAD_LEN)
			{
				// TODO extract Date/Time if payload is THERMOSTAT_STATE_TIME_PAYLOAD_LEN
			}
		}
		else logger.error("Got "+this.msgType+" message with incorrect length!");
	}

	/**
	 * Print output as decoded fields
	 */
	@Override
	protected void printFormattedPayload()
	{
		logger.debug("\tDesired Temperature => "+desiredTemperature);
		logger.debug("\tMeasured Temperature=> "+measuredTemperature);
		logger.debug("\tValve Position      => "+valvePos);
		logger.debug("\tControl Mode        => "+ctrlMode);
		logger.debug("\tDST Active          => "+dstActive);
		logger.debug("\tLAN Gateway         => "+lanGateway);
		logger.debug("\tPanel locked        => "+lockedForManualSetPoint);
		logger.debug("\tRF Error            => "+rfError);
		logger.debug("\tBattery Low         => "+batteryLow);
		if (untilDateTime != null)
			logger.debug("\tUntil DateTime      => "+untilDateTime.get(Calendar.YEAR)+"-"+(untilDateTime.get(Calendar.MONTH)+1)+"-"+untilDateTime.get(Calendar.DAY_OF_MONTH)+" "+untilDateTime.get(Calendar.HOUR_OF_DAY)+":"+untilDateTime.get(Calendar.MINUTE)+":"+untilDateTime.get(Calendar.SECOND));
	}

	public double getDesiredTemperature()
	{
		return desiredTemperature;
	}

	public double getMeasuredTemperature()
	{
		return measuredTemperature;
	}

	public boolean getBatteryLow()
	{
		return batteryLow;
	}

	public ThermostatControlMode getControlMode()
	{
		return ctrlMode;
	}

}

@@ -3612,33 +3612,33 @@
 ));%0A%09%7D%0A%0A%09public 
-d
+D
 ouble getDesired
@@ -3695,17 +3695,17 @@
 %09public 
-d
+D
 ouble ge

Return correct types for last commit to work.