import midi
import sys
from graphmodel.utils import MidiUtils

__author__ = 'Adisor'


class Analyzer(object):
    """
    Class is used for analyzing, input curation, validation, and pre-processing a midi file before execution.
    With this class, we can capture events that are not being processed or midi patterns that break our
    rules or assumptions. If any pattern would break our rules or assumptions, the program exits.
    """
    DO_EXIT = True

    def __init__(self, midi_file_name):
        self.pattern = midi.read_midifile(midi_file_name)

    def perform_analysis(self):
        # check for unprocessed events
        for track in self.pattern:
            channel = -1
            for event in track:
                if MidiUtils.is_channel_event(event):
                    if channel == -1:
                        channel = event.channel
                    if channel != event.channel:
                        print "TRACK HAS MULTIPLE CHANNELS"
                        if Analyzer.DO_EXIT:
                            sys.exit(-1)

        # global meta events should be in the first track
        for i in range(1, len(self.pattern), 1):
            for event in self.pattern[i]:
                if MidiUtils.is_song_meta_event(event):
                    print "GLOBAL META EVENTS NEED TO BE IN THE FIRST TRACK", event
                    if Analyzer.DO_EXIT:
                        sys.exit(-1)
