package de.ivu.elect.jee.database;

import java.io.Serializable;
import java.util.Map;

public class DatabaseStatistikData implements Serializable, Comparable<DatabaseStatistikData> {

  private static final long serialVersionUID = 1L;

  private final long zeitpunkt;

  private final DatabaseCountData countData;

  private final DatabaseTimeData timeData;

  private final boolean statisticsEnabled;

  public DatabaseStatistikData(Map<String, Object> attributeMap) {
    statisticsEnabled = getBooleanValue(attributeMap, "statisticsEnabled");
    countData = new DatabaseCountData(attributeMap);
    timeData = new DatabaseTimeData(attributeMap);
    zeitpunkt = System.currentTimeMillis();
  }

  public DatabaseCountData getCountData() {
    return countData;
  }

  public DatabaseTimeData getTimeData() {
    return timeData;
  }

  public boolean isStatisticsEnabled() {
    return statisticsEnabled;
  }

  public long getZeitpunkt() {
    return zeitpunkt;
  }

  @Override
  public int compareTo(DatabaseStatistikData o) {
    return Long.compare(zeitpunkt, o.zeitpunkt);
  }

  public static boolean getBooleanValue(Map<String, Object> attributeMap, String name) {
    try {
      return Boolean.parseBoolean(attributeMap.getOrDefault(name, "0").toString());
    } catch (Exception e) {
      return false;
    }
  }

  public static long getLongValue(Map<String, Object> attributeMap, String name) {
    try {
      return Long.parseLong(attributeMap.getOrDefault(name, "0").toString());
    } catch (Exception e) {
      return 0L;
    }
  }

  @Override
  public String toString() {
    return "DatabaseStatistikData{" +
        "zeitpunkt=" + zeitpunkt +
        ", countData=" + countData +
        ", timeData=" + timeData +
        ", statisticsEnabled=" + statisticsEnabled +
        '}';
  }

/*
ActiveCount; value: 10
AvailableCount; value: 10
AverageBlockingTime; value: 0
AverageCreationTime; value: 9
AverageGetTime; value: 3
AveragePoolTime; value: 7094
AverageUsageTime; value: 21
BlockingFailureCount; value: 0
CreatedCount; value: 10
DestroyedCount; value: 0
IdleCount; value: 10
InUseCount; value: 0
MaxCreationTime; value: 77
MaxGetTime; value: 7
MaxPoolTime; value: 14209
MaxUsageTime; value: 24
MaxUsedCount; value: 2
MaxWaitCount; value: 0
MaxWaitTime; value: 0
TimedOut; value: 0
TotalBlockingTime; value: 0
TotalCreationTime; value: 95
TotalGetTime; value: 12
TotalPoolTime; value: 28376
TotalUsageTime; value: 86
WaitCount; value: 0
XACommitAverageTime; value: 0
XACommitCount; value: 0
XACommitMaxTime; value: 0
XACommitTotalTime; value: 0
XAEndAverageTime; value: 0
XAEndCount; value: 0
XAEndMaxTime; value: 0
XAEndTotalTime; value: 0
XAForgetAverageTime; value: 0
XAForgetCount; value: 0
XAForgetMaxTime; value: 0
XAForgetTotalTime; value: 0
XAPrepareAverageTime; value: 0
XAPrepareCount; value: 0
XAPrepareMaxTime; value: 0
XAPrepareTotalTime; value: 0
XARecoverAverageTime; value: 0
XARecoverCount; value: 0
XARecoverMaxTime; value: 0
XARecoverTotalTime; value: 0
XARollbackAverageTime; value: 0
XARollbackCount; value: 0
XARollbackMaxTime; value: 0
XARollbackTotalTime; value: 0
XAStartAverageTime; value: 0
XAStartCount; value: 0
XAStartMaxTime; value: 0
XAStartTotalTime; value: 0
statisticsEnabled; value: true
 */

}
