package eu.hsrw.ias.ggd;

import java.util.LinkedList;
import java.util.List;

public class Device {

  private String id;
  private String externalID;
  private List<SensorData> data;
  private Severity state = Severity.Unknown;

  public Device(String identifier) {
    this.id = identifier;
    this.data = new LinkedList<SensorData>();
  }

  public void addSensorData(SensorData d) {
    this.data.add(d);
  }

  public List<SensorData> getSensorData() {
    return data;
  }

  public String getId() {
    return id;
  }

  public String setId(String id) {
    this.id = id;
    return id;
  }

  public void setExternalID(String externalID) {
    this.externalID = externalID;
  }

  public String getExternalID() {
    return externalID;
  }

  public String IdMapper(String id) {
    switch (id) {
      case "fd694041-581e-4c2c-9810-505e62b762e6":
        return setId("70B3D570500042D7");
      case "e8c3fca3-c5cd-48e7-9224-0f8c294fa3c0":
        return setId("70B3D57050004EF4");
      case "f9bb4a5c-f783-4bcf-8c34-0a7c3dd5a632":
        return setId("70B3D57050006223");
      case "6c9efd4a-db04-453d-8ab8-d480ec97dd26":
        return setId("70B3D5705000623C");
      default:
        return "NO Working id was found";
    }
  }

  public Boolean hasTag(String tag) {
    for (SensorData sensorData : data) {
      if (sensorData.getTag() == tag) return true;
    }
    return false;
  }

  public SensorData getSensorDataByTag(String tag) {
    for (SensorData sd : data) {
      if (sd.getTag() == tag) return sd;
    }
    return null;
  }

  public SensorData getSensorDataById(String id) {
    for (SensorData sd : data) {
      if (sd.getSensorId() == id) return sd;
    }
    return null;
  }

  public String toString() {
    String str = "Device: '" + id + "', data: [";

    for (SensorData sd : data) {
      str += "SensorData(id: '" + sd.getSensorId() + "',";
      str += " tag: '" + sd.getTag() + "',";
      str += " value: " + sd.getValue() + ")";
    }

    return str + "]";
  }

  public Device(String id, List<SensorData> data) {
    this.id = id;
    this.data = data;
  }

  public void setState(Severity state) {
    if (this.state != Severity.Critical) {
      this.state = state;
    }
  }

  public Severity getState() {
    return state;
  }
}
