# t: t
# encoding:utf-8
# title: AnimationSteps().pace() interval mapping
#
# That's really the least worthwhile thing to test,
# but the only thing that can realistically be.
# (Generating SVGs and asserting binary GIF output is way beyond scope.)
#

import pytest
import export_gif as xg
from argparse import Namespace

# lazier @dataclass
class AnimTest():
    def __init__(self, **kwargs):
        self.attrib = kwargs

# stub out with parent attributes
class PaceTest(xg.AnimationSteps):
    def __init__(self):
        self.frames = 10
        self.delay = 10
        self.layer = Namespace(
            tags = {"pace"},
            args = {"delay": 10},
        )
        self.gif = Namespace(
            options = Namespace(
                all_pace = True,
                delay = 10,
            )
        )

# apply callback to r_time list, and round to two decimals
def _roundy_times(vary, max=10, xd=0.0):
    return [round(vary(r/max + xd), 2) for r in range(0, max+1)]


# simple interval 2s-4s within 10s frame
def delay_dur():
    anim = AnimTest(begin="2s", dur="4s")
    vary = PaceTest().pace(anim)
    assert _roundy_times(vary, 10) == [0.0, 0.0, 0.0, 0.17, 0.33, 0.5, 0.67, 1.0, 1.0, 1.0, 1.0]

# oscillate twice in 10s frame
def repeat():
    anim = AnimTest(begin="0s", dur="10s", repeatCount="2")
    vary = PaceTest().pace(anim)
    assert _roundy_times(vary, 10, +0.0000) == [0.0, 0.2, 0.4, 0.6, 0.8, 0.0, 0.2, 0.4, 0.6, 0.8, 0.0]
    assert _roundy_times(vary, 10, -0.0001) == [0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 0.2, 0.4, 0.6, 0.8, 1.0]

# combine both
def combo():
    anim = AnimTest(begin="5s", dur="10s", repeatCount="5")
    vary = PaceTest().pace(anim)
    assert _roundy_times(vary, 10) == [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.33, 0.67, 0.0, 0.33, 0.67]

# paced curve
def paced_curve():
    anim = AnimTest(calcMode="paced")
    vary = PaceTest().pace(anim)
    assert _roundy_times(vary, 10) == [0.01, 0.02, 0.05, 0.12, 0.25, 0.45, 0.67, 0.83, 0.92, 0.97, 0.99]

# keytimes, spline
def spline_times():
    anim = AnimTest(calcMode="spline", keyTimes="0; 0.1; 0.8; 1.0")  # a third per ascent 10%+70%+20%, (or back to len+1?)
    vary = PaceTest().pace(anim)
    #print(_roundy_times(vary, 100))
    assert _roundy_times(vary, 100) == [
        0.0, 0.0, 0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.02, 0.03, 0.03, 0.03, 0.04, 0.04, 0.04, 0.05, 0.05, 0.05, 0.05, 0.06, 0.06, 0.06, 0.07, 0.07, 0.07, 0.08, 0.08, 0.08, 0.08, 0.09, 0.09, 0.09,
        0.1, 0.1, 0.11, 0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.35, 0.37, 0.39, 0.41, 0.43, 0.45, 0.47, 0.49, 0.51, 0.53, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.77, 0.79,
        0.8, 0.81, 0.81, 0.82, 0.83, 0.83, 0.84, 0.84, 0.85, 0.86, 0.86, 0.87, 0.87, 0.88, 0.89, 0.89, 0.9, 0.9, 0.91, 0.92, 0.92, 0.93, 0.93, 0.94, 0.95, 0.95, 0.96, 0.96, 0.97, 0.98, 0.98, 0.99, 0.99, 1.0
    ]


