package ru.dbotthepony.mc.otm.core.chart

import net.minecraft.network.RegistryFriendlyByteBuf
import ru.dbotthepony.mc.otm.core.collect.reduce
import ru.dbotthepony.mc.otm.core.math.Decimal
import java.util.function.Supplier

class CombinedDecimalHistoryChart(
	provider: Supplier<Iterator<DecimalHistoryChart>>,
	resolution: Int,
	width: Int,
) : AbstractCombinedHistoryChart<Decimal, DecimalHistoryChart>(provider, width, resolution) {
	constructor(provider: Supplier<Iterator<DecimalHistoryChart>>, ticks: Int) : this(provider, 1, ticks)

	override fun sum(values: List<Decimal>): Decimal {
		return values.iterator().reduce(Decimal.ZERO, Decimal::plus)
	}

	override fun calculateAverage(): Decimal {
		return this.iterator().reduce(Decimal.ZERO, Decimal::plus) / Decimal(width)
	}

	override fun factory(stream: RegistryFriendlyByteBuf): DecimalHistoryChart {
		return DecimalHistoryChart(stream.readVarInt(), stream.readVarInt())
	}

	override fun networkMetadata(stream: RegistryFriendlyByteBuf, chart: DecimalHistoryChart) {
		stream.writeVarInt(chart.resolution)
		stream.writeVarInt(chart.width)
	}
}
