package database

import (
	octopus "gordenko.dev/dima/qb"
	"gordenko.dev/dima/qb/chunkenc"
	"gordenko.dev/dima/qb/conbuf"
)

// METRIC

type _metric struct {
	MetricType    octopus.MetricType
	FracDigits    byte
	RootPageNo    uint32
	LastPageNo    uint32
	SinceValue    float64
	Since         uint32
	UntilValue    float64
	Until         uint32
	TimestampsBuf *conbuf.ContinuousBuffer
	ValuesBuf     *conbuf.ContinuousBuffer
	Timestamps    octopus.TimestampCompressor
	Values        octopus.ValueCompressor
}

func (s *_metric) ReinitBy(timestamp uint32, value float64) {
	s.TimestampsBuf = conbuf.New(nil)
	s.ValuesBuf = conbuf.New(nil)
	//
	s.Timestamps = chunkenc.NewReverseTimeDeltaOfDeltaCompressor(
		s.TimestampsBuf, 0)

	if s.MetricType == octopus.Cumulative {
		s.Values = chunkenc.NewReverseCumulativeDeltaCompressor(
			s.ValuesBuf, 0, s.FracDigits)
	} else {
		s.Values = chunkenc.NewReverseInstantDeltaCompressor(
			s.ValuesBuf, 0, s.FracDigits)
	}

	s.Timestamps.Append(timestamp)
	s.Values.Append(value)

	s.Since = timestamp
	s.SinceValue = value
	s.Until = timestamp
	s.UntilValue = value
}

func (s *_metric) DeleteMeasures() {
	s.TimestampsBuf = conbuf.New(nil)
	s.ValuesBuf = conbuf.New(nil)
	//
	s.Timestamps = chunkenc.NewReverseTimeDeltaOfDeltaCompressor(
		s.TimestampsBuf, 0)

	if s.MetricType == octopus.Cumulative {
		s.Values = chunkenc.NewReverseCumulativeDeltaCompressor(
			s.ValuesBuf, 0, s.FracDigits)
	} else {
		s.Values = chunkenc.NewReverseInstantDeltaCompressor(
			s.ValuesBuf, 0, s.FracDigits)
	}

	s.RootPageNo = 0
	s.LastPageNo = 0
	s.Since = 0
	s.SinceValue = 0
	s.Until = 0
	s.UntilValue = 0
}
