package main

import (
	"time"
)

func SM2Next(ts time.Time, history []int, unit time.Duration) time.Time {
	n, ef, i := 0, 2.5, 1
	for j := range history {
		n, ef, i = sm2Next(history[j], n, ef, i)
	}
	return ts.Add(time.Duration(i) * unit)
}

// After all scheduled reviews are complete, SuperMemo asks the user to re-review any cards they marked with a grade less than 4 repeatedly until they give a grade ≥ 4.
func sm2Next(thisGrade int, consecutivePassesBeforeThis int, efBefore float64, lastInterval int) (int, float64, int) {
	q := thisGrade
	n := consecutivePassesBeforeThis
	ef := efBefore
	i := lastInterval

	if q >= 3 {
		switch n {
		case 0:
			i = 1
		case 1:
			i = 6
		default:
			i = int(float64(i) * ef)
		}
		n += 1
	} else {
		n = 0
		i = 1
	}

	ef = ef + (.1 - (5.0-float64(q))*(.08+(5.0-float64(q))*.02))
	if ef < 1.3 {
		ef = 1.3
	}

	return n, ef, i
}
