from pulling_spec import Rev1Test, Rev2Test, Rev3Test, Rev4Test, Rev5Test
from klon_core.models import ActionType
from utils import IntegrationRunner

runner = IntegrationRunner(test_file=__file__, access_pw="some password")
hub = runner.init_hub()

with hub.running():
    hub.mock_time(0)
    node = runner.init_node(node_id="my first node")
    Rev1Test.hub_dir, Rev1Test.node_dir = hub.path, node.path
    Rev2Test.hub_dir, Rev2Test.node_dir = hub.path, node.path
    Rev3Test.hub_dir, Rev3Test.node_dir = hub.path, node.path
    Rev4Test.hub_dir, Rev4Test.node_dir = hub.path, node.path
    Rev5Test.hub_dir, Rev5Test.node_dir = hub.path, node.path

    with runner.section(title="rev 0 => rev 1: adding one song in root dir", test_cls=Rev1Test):
        with hub.add_revision(timestamp=1) as rev:
            rev.add_action(
                action_type=ActionType.add, path="song1.klon-mock", last_mod=1337, size=1337, bitrate=320,
                title="song1"
            )
        hub.mock_time(timestamp=1)
        node.pull()

    with runner.section(title="rev 1 => rev 2: adding one song in nested dir", test_cls=Rev2Test):
        with hub.add_revision(timestamp=2) as rev:
            rev.add_action(
                action_type=ActionType.add, path="a_long_path/to_that_next_song/here_it_is/song2.klon-mock",
                last_mod=1337, size=1337, bitrate=320,
                title="song2"
            )
        hub.mock_time(timestamp=2)
        node.pull()

    with runner.section(title="rev 2 => rev 3: editing a song", test_cls=Rev3Test):
        with hub.add_revision(timestamp=3) as rev:
            rev.add_action(
                action_type=ActionType.edit, path="a_long_path/to_that_next_song/here_it_is/song2.klon-mock",
                artist="song2_artist"
            )
        hub.mock_time(timestamp=3)
        node.pull()

    with runner.section(title="rev 3 => rev 4: replacing a song", test_cls=Rev4Test):
        with hub.add_revision(timestamp=4) as rev:
            rev.add_action(
                action_type=ActionType.replace, path="song1.klon-mock", artist="song1_artist"
            )
        hub.mock_time(timestamp=4)
        node.pull()

    with runner.section(title="rev 4 => rev 5: removing previous songs", test_cls=Rev5Test):
        with hub.add_revision(timestamp=5) as rev:
            rev.add_action(action_type=ActionType.remove, path="song1.klon-mock")
            rev.add_action(action_type=ActionType.remove, path="a_long_path/to_that_next_song/here_it_is/song2.klon-mock")
        hub.mock_time(timestamp=5)
        node.pull()

    # TODO test edit action that moves a file
    # TODO test replace action that moves a file
