package eu.gity.jmeter.ddos.stairsddosthreadgrp.charting.rows;

import eu.gity.jmeter.ddos.stairsddosthreadgrp.charting.AbstractGraphPanelChartElement;
import eu.gity.jmeter.ddos.stairsddosthreadgrp.charting.AbstractGraphRow;
import eu.gity.jmeter.ddos.stairsddosthreadgrp.charting.elements.GraphPanelChartAverageElement;

import java.util.Iterator;
import java.util.Map.Entry;

public class GraphRowOverallAverages extends AbstractGraphRow
        implements Iterator<Entry<Long, AbstractGraphPanelChartElement>>, Entry<Long, AbstractGraphPanelChartElement> {

    private boolean hasNext;
    private final GraphPanelChartAverageElement element;
    private double avgX = 0;

    /**
     *
     */
    public GraphRowOverallAverages() {
        super();
        element = new GraphPanelChartAverageElement();
        hasNext = true;
    }

    /**
     * @param xVal
     * @param yVal
     */
    @Override
    public void add(long xVal, double yVal) {
        avgX = (avgX * element.getCount() + xVal) / (element.getCount() + 1);
        element.add(yVal);

        super.add((long) avgX, element.getValue());
    }

    /**
     * @return
     */
    public Iterator<Entry<Long, AbstractGraphPanelChartElement>> iterator() {
        hasNext = true;
        return this;
    }

    public boolean hasNext() {
        return hasNext;
    }

    public Entry<Long, AbstractGraphPanelChartElement> next() {
        hasNext = false;
        return this;
    }

    public void remove() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Long getKey() {
        return Math.round(avgX);
    }

    public GraphPanelChartAverageElement getValue() {
        return element;
    }

    public AbstractGraphPanelChartElement setValue(AbstractGraphPanelChartElement value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int size() {
        return 1;
    }

    @Override
    public AbstractGraphPanelChartElement getElement(long value) {
        if (getKey() == value) {
            return element;
        } else {
            return null;
        }
    }
}
