from datetime import timedelta
import os
import pytest

from django.conf import settings
from django.utils import timezone as tz

from aircox import models


@pytest.fixture
def path_infos():
    return {
        "test/20220101_10h13_1_sample_1.mp3": {
            "year": 2022,
            "month": 1,
            "day": 1,
            "hour": 10,
            "minute": 13,
            "n": 1,
            "name": "Sample 1",
        },
        "test/20220102_10h13_sample_2.mp3": {
            "year": 2022,
            "month": 1,
            "day": 2,
            "hour": 10,
            "minute": 13,
            "n": None,
            "name": "Sample 2",
        },
        "test/20220103_1_sample_3.mp3": {
            "year": 2022,
            "month": 1,
            "day": 3,
            "hour": None,
            "minute": None,
            "n": 1,
            "name": "Sample 3",
        },
        "test/20220104_sample_4.mp3": {
            "year": 2022,
            "month": 1,
            "day": 4,
            "hour": None,
            "minute": None,
            "n": None,
            "name": "Sample 4",
        },
        "test/20220105.mp3": {
            "year": 2022,
            "month": 1,
            "day": 5,
            "hour": None,
            "minute": None,
            "n": None,
            "name": "20220105",
        },
    }


class TestSoundQuerySet:
    @pytest.mark.django_db
    def test_downloadable(self, sounds):
        query = models.Sound.objects.downloadable().values_list("is_downloadable", flat=True)
        assert set(query) == {True}

    @pytest.mark.django_db
    def test_broadcast(self, sounds):
        query = models.Sound.objects.broadcast().values_list("broadcast", flat=True)
        assert set(query) == {True}

    @pytest.mark.django_db
    def test_playlist(self, sounds):
        expected = [os.path.join(settings.MEDIA_ROOT, s.file.path) for s in sounds]
        assert models.Sound.objects.all().playlist() == expected


class TestSound:
    @pytest.mark.django_db
    def test_read_path(self, path_infos):
        for path, expected in path_infos.items():
            result = models.Sound.read_path(path)
            assert expected == result

    @pytest.mark.django_db
    def test__as_name(self):
        name = "some_1_file"
        assert models.Sound._as_name(name) == "Some 1 File"

    def _setup_diff(self, program, info):
        episode = models.Episode(program=program, title="test-episode")
        at = tz.datetime(**{k: info[k] for k in ("year", "month", "day", "hour", "minute") if info.get(k)})
        at = tz.make_aware(at)
        diff = models.Diffusion(episode=episode, start=at, end=at + timedelta(hours=1))
        episode.save()
        diff.save()
        return diff

    @pytest.mark.django_db(transaction=True)
    def test_find_episode(self, program, path_infos):
        for path, infos in path_infos.items():
            diff = self._setup_diff(program, infos)
            sound = models.Sound(program=diff.program, file=path)
            result = sound.find_episode(infos)
            assert diff.episode == result

    @pytest.mark.django_db
    def test_find_playlist(self):
        raise NotImplementedError("test is not implemented")

    @pytest.mark.django_db
    def test_get_upload_dir(self):
        raise NotImplementedError("test is not implemented")

    @pytest.mark.django_db
    def test_sync_fs(self):
        raise NotImplementedError("test is not implemented")

    @pytest.mark.django_db
    def test_read_metadata(self):
        raise NotImplementedError("test is not implemented")
