from siege import game
from siege.log import Log
from siege.util import minutes, Random, seconds, Timer


class MusicSystem(object):
    NAME = "music"

    def getName(self):
        return self.NAME

    def __init__(self):
        self.tracks = ['mods/core/audio/tracks/track1.ogg',
                       'mods/core/audio/tracks/track2.ogg',
                       'mods/core/audio/tracks/track3.ogg'
                       ]
        self.playTimer = Timer(Random.get(seconds(5), seconds(10)))
        self.index = -1
        Log.info("Initializing MusicSystem")

    def update(self, frameTime):
        self.playTimer.update(frameTime)
        if self.playTimer.expired():
            indices = range(len(self.tracks))
            if self.index != -1:
                del indices[self.index]
            self.index = Random.get(indices)
            track = self.tracks[self.index]
            game.audio.playTrack(track)
            time = game.audio.getTrackDuration() + Random.get(seconds(30), minutes(2))
            Log.info("Playing track {}".format(track))
            Log.info("Next track will play in {}".format(time))
            self.playTimer.reset(time)

    @staticmethod
    def register():
        game.registerSystem(MusicSystem.NAME, MusicSystem())
