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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

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

/**
 *  on 18.06.2015.
 */
public class PraesentationStatistik implements Serializable {

  private static final Logger LOG = LoggerFactory.getLogger(PraesentationStatistik.class);

  private static final int MAX_ENTRIES = 50;

  private static final long serialVersionUID = 1L;

  private final List<PraesentationStatistikEntry> entries = new CopyOnWriteArrayList<>();

  public List<PraesentationStatistikEntry> getEntries() {
    return new ArrayList<>(entries);
  }

  private volatile PraesentationStatistikTimer praesentationStatistikTimer = new PraesentationStatistikTimer();

  private void add(PraesentationStatistikEntry statistikEntry) {
    if (statistikEntry!=null) {
      entries.add(statistikEntry);
      if (entries.size() > MAX_ENTRIES) {
        entries.remove(0);
      }

      LOG.info("PraesentationStatistik: " + statistikEntry + ", Historie: " + entries);
    }
  }

  public void startGesamt() {
    praesentationStatistikTimer = new PraesentationStatistikTimer();
  }

  public void endeGesamt() {
    add(praesentationStatistikTimer.finish());
  }

  public void startDatenLaden(){
    praesentationStatistikTimer.setDatenLadenStart(System.currentTimeMillis());
  }

  public void endeDatenLaden(){
    praesentationStatistikTimer.setDatenLadenEnde(System.currentTimeMillis());
  }

  public void startKontextBauen(){
    praesentationStatistikTimer.setKontextBauenStart(System.currentTimeMillis());
  }

  public void endeKontextBauen(){
    praesentationStatistikTimer.setKontextBauenEnde(System.currentTimeMillis());
  }

  public void startRendern(){
    praesentationStatistikTimer.setRendernStart(System.currentTimeMillis());
  }

  public void endeRendern(){
    praesentationStatistikTimer.setRendernEnde(System.currentTimeMillis());
  }

  public void startUpload() {
    praesentationStatistikTimer.setUploadStart(System.currentTimeMillis());
  }

  public void endeUpload() {
    praesentationStatistikTimer.setUploadEnde(System.currentTimeMillis());
  }

}
