/*
 * Copyright 2016 The Winston Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.s13g.winston.lib.nest;

import com.s13g.winston.lib.nest.data.HvacState;
import com.s13g.winston.lib.nest.data.ThermostatData;
import com.s13g.winston.lib.temperature.Temperature;

import java.util.Optional;

/**
 * A nest thermostat that can be queried to the latest data.
 */
public class Thermostat {
  private final String mId;
  private final NestController mNestController;

  private boolean mLastRefreshSuccess;
  private long mLastRefreshTime;
  private String mName;
  private Temperature mAmbientTemperature;
  private Temperature mTargetTemperature;
  private float mHumidty;
  private HvacState mHvacState;

  public Thermostat(String id, NestController nestController) {
    mId = id;
    mNestController = nestController;
  }

  /**
   * Refreshes the thermostat if the last refresh is older than the given age.
   *
   * @param maxAgeMillis if the thermostat data has been refreshed less than this time ago, it will
   * not be refreshed again.
   */
  public Thermostat refresh(long maxAgeMillis) {
    boolean dataExpired = mLastRefreshTime + maxAgeMillis < System.currentTimeMillis();
    if (maxAgeMillis < 0 || !dataExpired) {
      return this;
    }
    mNestController.refresh();
    mLastRefreshSuccess = false;
    for (ThermostatData data : mNestController.getThermostats()) {
      if (data.id.equals(mId)) {
        mName = data.name;
        mAmbientTemperature = data.ambientTemperature;
        mTargetTemperature = data.targetTemperature;
        mHumidty = (float) data.humidity;
        mHvacState = data.hvacState;
        mLastRefreshSuccess = true;
        mLastRefreshTime = System.currentTimeMillis();
        break;
      }
    }
    return this;
  }

  public Optional<String> getName() {
    return returnIfRefreshSuccessful(mName);
  }

  public Optional<Temperature> getAmbientTemperature() {
    return returnIfRefreshSuccessful(mAmbientTemperature);
  }

  public Optional<Temperature> getTargetTemperature() {
    return returnIfRefreshSuccessful(mTargetTemperature);
  }

  public void setTargetTemperature(Temperature temperature) {
    mNestController.setTemperature(mId, temperature);
  }

  public Optional<Float> getHumidity() {
    return returnIfRefreshSuccessful(mHumidty);
  }

  public Optional<HvacState> getHvacState() {
    return returnIfRefreshSuccessful(mHvacState);
  }

  private <T> Optional<T> returnIfRefreshSuccessful(T value) {
    if (!mLastRefreshSuccess) {
      return Optional.empty();
    }
    return Optional.of(value);
  }
}

@@ -1084,163 +1084,34 @@
 ate 
-String mName;%0A  private Temperature mAmbientTemperature;%0A  private Temperature mTargetTemperature;%0A  private float mHumidty;%0A  private HvacState mHvacState
+ThermostatData mLatestData
 ;%0A%0A 
@@ -1838,211 +1838,25 @@
    m
-Name = data.name;%0A        mAmbientTemperature = data.ambientTemperature;%0A        mTargetTemperature = data.targetTemperature;%0A        mHumidty = (float) data.humidity;%0A        mHvacState = data.hvacState
+LatestData = data
 ;%0A  
@@ -2071,17 +2071,28 @@
 essful(m
-N
+LatestData.n
 ame);%0A  
@@ -2189,17 +2189,28 @@
 essful(m
-A
+LatestData.a
 mbientTe
@@ -2320,17 +2320,28 @@
 essful(m
-T
+LatestData.t
 argetTem
@@ -2559,14 +2559,34 @@
 ful(
-mH
+(float) mLatestData.h
 umid
+i
 ty);
@@ -2675,24 +2675,143 @@
 essful(m
-HvacStat
+LatestData.hvacState);%0A  %7D%0A%0A  public Optional%3CBoolean%3E getIsOnline() %7B%0A    return returnIfRefreshSuccessful(mLatestData.isOnlin
 e);%0A  %7D%0A

Refactor of Thermostat class, also added isOnline getter.