# Standard library imports
import datetime as dt
import os
import shutil
import tempfile
import unittest

# PyQt imports

# Third party imports

# App imports
from helpers import (
    fade_point,
    get_audio_segment,
    get_tags,
    get_relative_date,
    leading_silence,
    ms_to_mmss,
    normalise_track,
)


class TestMMHelpers(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_fade_point(self):
        test_track_path = "testdata/isa.mp3"
        test_track_data = "testdata/isa.py"

        audio_segment = get_audio_segment(test_track_path)
        assert audio_segment

        fade_at = fade_point(audio_segment)

        # Get test data
        with open(test_track_data) as f:
            testdata = eval(f.read())

        # Volume detection can vary, so ± 1 second is OK
        assert fade_at < testdata["fade_at"] + 1000
        assert fade_at > testdata["fade_at"] - 1000

    def test_get_tags(self):
        test_track_path = "testdata/mom.mp3"
        test_track_data = "testdata/mom.py"

        tags = get_tags(test_track_path)

        # Get test data
        with open(test_track_data) as f:
            testdata = eval(f.read())

        assert tags.artist == testdata["artist"]
        assert tags.title == testdata["title"]

    def test_get_relative_date(self):
        assert get_relative_date(None) == "Never"
        today_at_10 = dt.datetime.now().replace(hour=10, minute=0)
        today_at_11 = dt.datetime.now().replace(hour=11, minute=0)
        assert get_relative_date(today_at_10, today_at_11) == "Today 10:00"
        eight_days_ago = today_at_10 - dt.timedelta(days=8)
        assert get_relative_date(eight_days_ago, today_at_11) == "1 week, 1 day"
        sixteen_days_ago = today_at_10 - dt.timedelta(days=16)
        assert get_relative_date(sixteen_days_ago, today_at_11) == "2 weeks, 2 days"

    def test_leading_silence(self):
        test_track_path = "testdata/isa.mp3"
        test_track_data = "testdata/isa.py"

        audio_segment = get_audio_segment(test_track_path)
        assert audio_segment

        silence_at = leading_silence(audio_segment)

        # Get test data
        with open(test_track_data) as f:
            testdata = eval(f.read())

        # Volume detection can vary, so ± 1 second is OK
        assert silence_at < testdata["leading_silence"] + 1000
        assert silence_at > testdata["leading_silence"] - 1000

    def test_ms_to_mmss(self):
        assert ms_to_mmss(None) == "-"
        assert ms_to_mmss(59600) == "0:59"
        assert ms_to_mmss((5 * 60 * 1000) + 23000) == "5:23"

    def test_normalise(self):
        """Make copies to normalise to avoid corrupting source"""

        _, mp3_temp_path = tempfile.mkstemp(suffix=".mp3")
        shutil.copyfile("testdata/isa.mp3", mp3_temp_path)
        normalise_track(mp3_temp_path)
        os.unlink(mp3_temp_path)

        _, flac_temp_path = tempfile.mkstemp(suffix=".flac")
        shutil.copyfile("testdata/isa.flac", flac_temp_path)
        normalise_track(flac_temp_path)
        os.unlink(flac_temp_path)
