package de.ivu.elect.business.system.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.ivu.elect.jee.performance.ViewAccessStatistics;
import de.ivu.elect.jee.performance.ViewAccessTargetStatistics;


import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;

public enum PerformanceStatistikViewZeitraum {

  SYSTEM_START {
    @Override
    public List<ViewAccessTargetStatistics> passeAnZeitraumAn(List<ViewAccessTargetStatistics> statistics, ViewAccessStatistics snaphost) {
      return statistics;
    }
  },
  SNAPSHOT {
    @Override
    public List<ViewAccessTargetStatistics> passeAnZeitraumAn(List<ViewAccessTargetStatistics> statistics, ViewAccessStatistics snaphost) {
      if (snaphost == null) {
        return SYSTEM_START.passeAnZeitraumAn(statistics, null);
      }

      Map<ViewAccessTargetStatistics, ViewAccessTargetStatistics> snapshotMapped = snaphost.getStatistics().stream().collect(toMap(identity(), identity()));

      List<ViewAccessTargetStatistics> result = new ArrayList<>(statistics.size() + 1);
      for (ViewAccessTargetStatistics statistic : statistics) {
        ViewAccessTargetStatistics snapshotStatistic = snapshotMapped.get(statistic);
        if (snapshotStatistic == null) {
          result.add(statistic);
        } else {
          if (statistic.getCount() - snapshotStatistic.getCount() > 0) {
            ViewAccessTargetStatistics diffStatistic = new ViewAccessTargetStatistics(statistic, statistic.getCount() - snapshotStatistic.getCount(),
                statistic.getTimeTotal() - snapshotStatistic.getTimeTotal());
            result.add(diffStatistic);
          }
        }
      }

      return result;
    }
  };

  public abstract List<ViewAccessTargetStatistics> passeAnZeitraumAn(List<ViewAccessTargetStatistics> statistics, ViewAccessStatistics snaphost);
}
