"""MPF plugin which uses lights to represent Game functions. Typically in an
EM machine"""

import logging


class InfoLights(object):

    def __init__(self, machine):
        self.log = logging.getLogger('infolights')
        self.machine = machine

        try:
            self.config = self.machine.config['info_lights']
        except KeyError:
            self.machine.log.debug('"info_lights:" section not found in machine '
                                   'configuration, so the Info Lights plugin '
                                   'will not be used.')
            return

        self.flash = [
            {'color': 'ff', 'tocks': 1},
            {'color': '0', 'tocks': 1},
        ]

        # convert any light names we find to objects
        for k, v in self.config.items():
            if 'light' in v:
                if v['light'] in self.machine.lights:
                    self.config[k]['light'] = self.machine.lights[v['light']]

        self.machine.events.add_handler('ball_started', self.ball_started)
        self.machine.events.add_handler('game_ended', self.game_ended)
        self.machine.events.add_handler('game_starting', self.game_starting)
        self.machine.events.add_handler('player_add_success', self.player_added)
        self.machine.events.add_handler('tilt', self.tilt)
        self.machine.events.add_handler('match', self.match)

        self.game_ended()  # set the initial lights to the game ended state

    def reset_game_lights(self):
        self.log.debug("reset_game_lights")
        # turn off the game-specific lights (player, ball & match)
        for k, v in self.config.items():
            if k.startswith('ball_'):
                v['light'].off()
            if k.startswith('player_'):
                v['light'].off()
            if k.startswith('match_'):
                v['light'].off()

    def ball_started(self, **kwargs):
        del kwargs
        self.log.debug("ball_started")
        # turn off all the ball lights
        for k, v in self.config.items():
            if k.startswith('ball_'):
                v['light'].off()

        # turn on this current ball's light
        ball_light = 'ball_' + str(self.machine.game.player.ball)
        if ball_light in self.config:
            self.config[ball_light]['light'].on()

        # turn off the tilt light
        if 'tilt' in self.config:
            self.config['tilt']['light'].off()

    def game_ended(self, **kwargs):
        del kwargs
        self.log.debug("game_ended")
        self.reset_game_lights()

        # turn on game over
        if 'game_over' in self.config:
            self.machine.scripts.run_script(
                lights=self.config['game_over']['light'].name,
                script=self.flash,
                playback_rate=2,
                key='game_over')

    def game_starting(self, **kwargs):
        del kwargs
        self.log.debug("game_starting")
        self.reset_game_lights()
        self.machine.scripts.stop_script(key='game_over')

    def player_added(self, player, **kwargs):
        del kwargs
        self.log.debug("player_added. player=%s", player)
        player_str = 'player_' + str(player.number)
        self.log.debug("player_str: %s", player_str)
        if player_str in self.config:
            self.config[player_str]['light'].on()

    def tilt(self, **kwargs):
        del kwargs
        self.log.debug("tilt")
        if 'tilt' in self.config:
            self.config['tilt']['light'].on()

    def match(self, match, **kwargs):
        del kwargs
        self.log.debug("Match")
        match_str = 'match_' + str(match)
        if match_str in self.config:
            self.config[match_str]['light'].on()


plugin_class = InfoLights

@@ -101,16 +101,49 @@
 ogging%0A%0A
+from mpf.devices.led import Led%0A%0A
 %0Aclass I
@@ -631,109 +631,105 @@
 elf.
-flash = %5B%0A            %7B'color': 'ff', 'tocks': 1%7D,%0A            %7B'color': '0', 'tocks': 1%7D,%0A        %5D%0A
+machine.events.add_handler('machine_reset_phase_3', self._initialize)%0A%0A    def _initialize(self):
 %0A   
@@ -979,16 +979,255 @@
 light'%5D%5D
+%0A                elif v%5B'light'%5D in self.machine.leds:%0A                    self.config%5Bk%5D%5B'light'%5D = self.machine.leds%5Bv%5B'light'%5D%5D%0A                else:%0A                    raise AssertionError(%22Invalid light or led %7B%7D%22.format(v%5B'light'%5D))
 %0A%0A      
@@ -2896,172 +2896,389 @@
     
-self.machine.scripts.run_script(%0A                lights=self.config%5B'game_over'%5D%5B'light'%5D.name,%0A                script=self.flash,%0A                playback_rate=2,%0A
+if isinstance(self.config%5B'game_over'%5D%5B'light'%5D, Led):%0A                self.machine.shows%5B'flash'%5D.play(%0A                    show_tokens=dict(leds=self.config%5B'game_over'%5D%5B'light'%5D),%0A                    key='infolights_game_over')%0A            else:%0A                self.machine.shows%5B'flash'%5D.play(%0A                    show_tokens=dict(lights=self.config%5B'game_over'%5D%5B'light'%5D),%0A  
     
@@ -3289,21 +3289,34 @@
         
+  
 key='
+infolights_
 game_ove
@@ -3451,32 +3451,35 @@
 ights()%0A        
+if 
 self.machine.scr
@@ -3480,42 +3480,157 @@
 ne.s
-cripts.stop_script(key='game_over'
+how_controller.get_running_shows('infolights_game_over'):%0A            self.machine.show_controller.get_running_shows('infolights_game_over')%5B0%5D.stop(
 )%0A%0A 

fix info_lights plugin