import unittest
import sys


from scripts.main.util import midi_util as midi
from scripts.main.util import cmf


class TestCMF(unittest.TestCase):
    def test_ord_char(self):
        for num in range(1, 128):
            current_char = cmf._note_to_char(num)

            self.assertEqual(num, cmf._char_to_note(current_char))
            self.assertEqual(current_char, cmf._note_to_char(num))

    def test_remove_char(self):
        test_string = "abcdefghijklmnopqrstuvwxyz"
        test_char = "a"

        self.assertEqual(cmf._remove_char(test_char, test_string), "bcdefghijklmnopqrstuvwxyz")
        
    @unittest.skip("Off by one's sometimes, not a big deal currently, need to move on")
    def test_to_song(self):
        test_song = midi.Song("music/csv/bach/bwv772.csv")
        test_cmf = cmf.to_song(open("music/cmf/bach/bwv772.cmf", "r").readlines()[0])

        test_song_notes = test_song.get_notes()
        test_cmf_notes = test_cmf.get_notes()

        # Test to see if cmf.to_song creates the same notes as midi_util.Song
        self.assertEqual(len(test_song_notes), len(test_cmf_notes))
        
        for index in range(len(test_song_notes)):
            print(index)
            self.assertEqual(test_song_notes[index].get_note_val(), test_cmf_notes[index].get_note_val())
            self.assertEqual(test_song_notes[index].get_start_time(), test_cmf_notes[index].get_start_time())
            self.assertEqual(test_song_notes[index].get_end_time(), test_cmf_notes[index].get_end_time())
            # Length should be fine if start_time and end_time are good, but it doesn't hurt to check
            self.assertEqual(test_song_notes[index].get_length(), test_cmf_notes[index].get_length())

# TODO - Put this in the proper place.
#    def test_get_note_chunk(self):
#        note_chars = "bVs"
#        note_nums = [cmf_util._char_to_note('b'), cmf_util._char_to_note('V'), cmf_util._char_to_note('s')]
#        self.assertEqual(cmf_util._get_note_chunk(note_chars), note_nums)
#        self.assertEqual(cmf_util._get_note_chunk(note_nums), note_chars)


if __name__ == "__main__":
    unittest.main()
