package atree

import (
	"fmt"

	"gordenko.dev/dima/diploma"
)

type ContinueFullScanReq struct {
	MetricType     diploma.MetricType
	FracDigits     byte
	ResponseWriter diploma.AtreeMeasureConsumer
	LastPageNo     uint32
}

func (s *Atree) ContinueFullScan(req ContinueFullScanReq) error {
	buf, err := s.fetchDataPage(req.LastPageNo)
	if err != nil {
		return fmt.Errorf("fetchDataPage(%d): %s", req.LastPageNo, err)
	}

	treeCursor, err := NewBackwardCursor(BackwardCursorOptions{
		PageNo:     req.LastPageNo,
		PageData:   buf,
		Atree:      s,
		FracDigits: req.FracDigits,
		MetricType: req.MetricType,
	})
	if err != nil {
		return err
	}
	defer treeCursor.Close()

	for {
		timestamp, value, done, err := treeCursor.Prev()
		if err != nil {
			return err
		}
		if done {
			return nil
		}
		req.ResponseWriter.Feed(timestamp, value)
	}
}

type ContinueRangeScanReq struct {
	MetricType     diploma.MetricType
	FracDigits     byte
	ResponseWriter diploma.AtreeMeasureConsumer
	LastPageNo     uint32
	Since          uint32
}

func (s *Atree) ContinueRangeScan(req ContinueRangeScanReq) error {
	buf, err := s.fetchDataPage(req.LastPageNo)
	if err != nil {
		return fmt.Errorf("fetchDataPage(%d): %s", req.LastPageNo, err)
	}

	treeCursor, err := NewBackwardCursor(BackwardCursorOptions{
		PageNo:     req.LastPageNo,
		PageData:   buf,
		Atree:      s,
		FracDigits: req.FracDigits,
		MetricType: req.MetricType,
	})
	if err != nil {
		return err
	}
	defer treeCursor.Close()

	for {
		timestamp, value, done, err := treeCursor.Prev()
		if err != nil {
			return err
		}
		if done {
			return nil
		}
		req.ResponseWriter.Feed(timestamp, value)
		if timestamp < req.Since {
			return nil
		}
	}
}

type RangeScanReq struct {
	MetricType     diploma.MetricType
	FracDigits     byte
	ResponseWriter diploma.AtreeMeasureConsumer
	RootPageNo     uint32
	Since          uint32
	Until          uint32
}

func (s *Atree) RangeScan(req RangeScanReq) error {
	pageNo, buf, err := s.findDataPage(req.RootPageNo, req.Until)
	if err != nil {
		return err
	}

	cursor, err := NewBackwardCursor(BackwardCursorOptions{
		PageNo:     pageNo,
		PageData:   buf,
		Atree:      s,
		FracDigits: req.FracDigits,
		MetricType: req.MetricType,
	})
	if err != nil {
		return err
	}
	defer cursor.Close()

	for {
		timestamp, value, done, err := cursor.Prev()
		if err != nil {
			return err
		}
		if done {
			return nil
		}

		//fmt.Printf("atree range scan: %s, %v\n", time.Unix(int64(timestamp), 0).Format("2006-01-02 15:04:05"), value)

		if timestamp <= req.Until {
			req.ResponseWriter.Feed(timestamp, value)

			if timestamp < req.Since {
				return nil
			}
		}
	}
}
