import datetime as dt

import pytest

from blc2.functions.chaserstep import ChaserStep
from blc2.topology import Fixture
from blc2.functions.scene import Scene
from blc2.functions.audio import Audio
from blc2.workspace import Workspace
from blc2.constants import INHERIT, MANUAL, EXTERNAL, INTERNAL, INFTY

class DummyChaser:
    def register_step(self, *args, **kwargs):
        if self.throw:
            raise Exception("blah")
        self.count += 1

    def delete_step(self, *args, **kwargs):
        self.count -= 1

    def __init__(self, w):
        self.w = w
        self.count = 0
        self.throw = False

@pytest.fixture
def cw():
    w = Workspace("", "", "", dt.datetime.now())
    f = Fixture(w=w, id_=0, channel_count=1)
    c0, = f.channels
    f2 = Fixture(w=w, id_=1, channel_count=4)
    Scene(w=w, id_=0, values={c0: 255})
    Scene(w=w, id_=1, values={c: 255-i for i, c in enumerate(f2.channels)})
    Audio(w=w, id_=2, filename="tests/silence.m4a")

    return w

def test_chaserstep(cw):
    c = DummyChaser(cw)
    s0 = cw.functions[0]
    s1 = cw.functions[1]
    a = cw.functions[2]
    c0, = cw.fixtures[0].channels

    ## Try adding a negative index
    with pytest.raises(Exception):
        ChaserStep(c, id_=42069, function=s0, duration_mode=INHERIT, index=-10)
    assert c.count == 0
    assert 42069 not in cw.functions

    ## Test a failed add
    with pytest.raises(Exception):
        c.throw = True
        ChaserStep(c, id_=42069, function=s0, duration_mode=INHERIT)
    assert 42069 not in cw.functions
    c.throw = False

    ## Test how it handles inherit
    cs1 = ChaserStep(c, function=s0, duration_mode=INHERIT)
    assert cs1.duration == INFTY
    assert cs1.actual_duration == INFTY
    assert cs1.duration_mode == INHERIT
    assert cs1.fade_out_mode == s0.fade_out_mode
    assert cs1.scope == s0.scope
    assert cs1.audio_scope == s0.audio_scope

    cs1.fade_in = 1000
    cs1.fade_out = 1000
    assert cs1.duration == INFTY
    assert cs1.actual_duration == INFTY

    data = cs1._get_data(0, 0)
    lc, ac, data = cs1.render(500, data)
    assert not ac
    assert lc == ((c0, 127),)

    lc, ac, data = cs1.render(1000, data)
    assert not ac
    assert lc == ((c0, 255),)

    data.end_time = 1500
    lc, ac, data = cs1.render(2000, data)
    assert not ac
    assert lc == ((c0, 127),)

    lc, ac, data = cs1.render(2501, data)
    assert not ac
    assert not lc

    ## Test how it handles manual mode
    cs1.duration_mode = MANUAL
    cs1.duration = 1500
    assert cs1.duration == 1500
    assert cs1.actual_duration == 2500

    data = cs1._get_data(0, 0)
    lc, ac, data = cs1.render(500, data)
    assert not ac
    assert lc == ((c0, 127),)

    lc, ac, data = cs1.render(1000, data)
    assert not ac
    assert lc == ((c0, 255),)

    lc, ac, data = cs1.render(2000, data)
    assert not ac 
    assert lc == ((c0, 127),)

    lc, ac, data = cs1.render(2501, data)
    assert not ac
    assert not lc

    ## Test how it handles inherit and a function change
    cs1.duration_mode = INHERIT
    assert cs1.duration == INFTY
    assert cs1.actual_duration == INFTY

    data = cs1._get_data(0, 0)
    
    cs1.fade_out = 0
    cs1.fade_in = 0
    cs1.function = a
    if a.actual_duration != 3024:
        raise ValueError("silence.m4a is wrong duration, fix the tests")
    assert cs1.audio_scope == a.audio_scope
    assert cs1.scope == a.scope
    assert cs1.duration == a.actual_duration
    assert cs1.actual_duration == a.actual_duration
    cs1.fade_out = 1000
    cs1.fade_in = 1000
    assert cs1.duration == a.actual_duration-1000

    lc, ac, data = cs1.render(500, data)
    assert not lc
    assert len(ac) == 1
    assert ac[0][1:] == ("tests/silence.m4a", 0, 1000, 2024, 1000)
    a.fade_out = 2000
    lc, ac, data = cs1.render(501, data)
    assert not lc
    assert len(ac) == 1
    assert ac[0][1:] == ("tests/silence.m4a", 0, 1000, 1024, 2000)

    lc, ac, data = cs1.render(3025, data)
    assert not lc
    assert not ac

    cs1.function = None
    assert cs1.duration == 0
    assert cs1.actual_duration == 0

    cs1.fade_in = 100
    with pytest.raises(ValueError):
        cs1.fade_in = -100
    assert cs1.fade_in == 100

    cs1.fade_out = 100
    with pytest.raises(ValueError):
        cs1.fade_out = -100
    assert cs1.fade_out == 100

    cs1.function = s0
    s0.delete()
    assert cs1.function is None
    assert cs1.audio_scope == frozenset()
    assert cs1.scope == frozenset()
    cs1.duration_mode = INHERIT
    with pytest.raises(AttributeError):
        cs1.duration = 50
    assert cs1.render(10000, data)[:2] == ((), ())

    cs1.duration_mode = MANUAL
    cs1.duration = 500
    with pytest.raises(ValueError):
        cs1.duration = -100
    assert cs1.duration == 500