"""Handles all light updates."""
import asyncio
from typing import Dict

from mpf.core.machine import MachineController
from mpf.core.settings_controller import SettingEntry

from mpf.core.rgb_color import RGBColorCorrectionProfile

from mpf.core.mpf_controller import MpfController


class LightController(MpfController):

    """Handles light updates."""

    def __init__(self, machine: MachineController) -> None:
        """Initialise lights controller."""
        super().__init__(machine)

        # Generate and add color correction profiles to the machine
        self.light_color_correction_profiles = dict()       # type: Dict[str, RGBColorCorrectionProfile]

        # will only get initialised if there are lights
        self._initialised = False

        self._monitor_update_task = None                    # type: asyncio.Task

    def initialise_light_subsystem(self):
        """Initialise the light subsystem."""
        if self._initialised:
            return
        self._initialised = True
        self.machine.validate_machine_config_section('light_settings')

        if self.machine.config['light_settings']['color_correction_profiles'] is None:
            self.machine.config['light_settings']['color_correction_profiles'] = (
                dict())

        # Create the default color correction profile and add it to the machine
        default_profile = RGBColorCorrectionProfile.default()
        self.light_color_correction_profiles['default'] = default_profile

        # Add any user-defined profiles specified in the machine config file
        for profile_name, profile_parameters in (
                self.machine.config['light_settings']
                ['color_correction_profiles'].items()):
            self.machine.config_validator.validate_config(
                'color_correction_profile',
                self.machine.config['light_settings']['color_correction_profiles']
                [profile_name], profile_parameters)

            profile = RGBColorCorrectionProfile(profile_name)
            profile.generate_from_parameters(
                gamma=profile_parameters['gamma'],
                whitepoint=profile_parameters['whitepoint'],
                linear_slope=profile_parameters['linear_slope'],
                linear_cutoff=profile_parameters['linear_cutoff'])
            self.light_color_correction_profiles[profile_name] = profile

        # add setting for brightness
        self.machine.settings.add_setting(SettingEntry("brightness", "Brightness", 100, "brightness", 1.0,
                                                       {0.25: "25%", 0.5: "50%", 0.75: "75%", 1.0: "100% (default)"}))

    def monitor_lights(self):
        """Update the color of lights for the monitor."""
        if not self._monitor_update_task:
            self._monitor_update_task = self.machine.clock.loop.create_task(self._monitor_update_lights())

    @asyncio.coroutine
    def _monitor_update_lights(self):
        colors = {}
        while True:

            for light in self.machine.lights:
                color = light.get_color()
                old = colors.get(light, None)
                if old != color:
                    self.machine.device_manager.notify_device_changes(light, "color", old, color)
                    self.colors[light] = color
            yield from asyncio.sleep(1 / 30, loop=self.machine.clock.loop)

@@ -2893,16 +2893,227 @@
 ights())
+%0A            self._monitor_update_task.add_done_callback(self._done)%0A%0A    def _done(self, future: asyncio.Future):%0A        try:%0A            future.result()%0A        except asyncio.CancelledError:%0A            pass
 %0A%0A    @a
@@ -3207,17 +3207,16 @@
 e True:%0A
-%0A
         
@@ -3472,16 +3472,16 @@
  color)%0A
+
         
@@ -3492,21 +3492,16 @@
         
-self.
 colors%5Bl

fix light updates for monitor