import os
from unittest import TestCase

from musicscore.musictree.midi import C

from musurgia.fractaltree.fractalmusic import FractalMusic
from musurgia.testcomparefiles import TestCompareFiles

path = str(os.path.abspath(__file__).split('.')[0])


class Test(TestCase):
    def setUp(self) -> None:
        self.fm = FractalMusic(proportions=[1, 2, 3, 4], tree_permutation_order=[3, 1, 4, 2], quarter_duration=20,
                               tempo=70)
        self.fm.midi_generator.midi_range = [36, 60]

    def test_1(self):
        self.fm.add_layer()
        score = self.fm.get_score(layer_number=1)

        for leaf in self.fm.traverse_leaves():
            leaf.set_chord(leaf.get_choral(range_factor=1.5, direction=1, last=True))

        sf = self.fm.get_simple_format()

        v = sf.to_stream_voice(1)
        v.add_to_score(score, 1, 2)

        xml_path = path + '_test_1.xml'
        score.write(path=xml_path)

        TestCompareFiles().assertTemplate(file_path=xml_path)

    def test_2(self):
        self.fm.midi_generator.midi_range = [C(4).value, C(6).value]
        self.fm.midi_generator.directions = [1, -1, 1]
        self.fm.add_layer()
        score = self.fm.get_score(layer_number=1)

        for leaf in self.fm.traverse_leaves():
            leaf.set_chord(leaf.get_choral(range_factor=1, direction=1, last=False))
        sf = self.fm.get_simple_format()

        v = sf.to_stream_voice(1)
        v.add_to_score(score, 1, 2)

        for leaf in self.fm.traverse_leaves():
            leaf.add_layer()

        sf = self.fm.get_simple_format()

        v = sf.to_stream_voice(1)
        v.add_to_score(score, 1, 3)

        xml_path = path + '_test_2.xml'
        score.write(path=xml_path)

        TestCompareFiles().assertTemplate(file_path=xml_path)
