#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

import config
from devdi import rooms
from function.db import get_radiator_data, set_radiator_data
from function.helpers import day_event
from function.modules import brightness_choose_n_action, timer_on_activation, heating_function, switched_light
from function.rooms import room, room_collection
import logging

try:
    from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
    ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)


class first_floor_east(room_collection):
    def __init__(self, mqtt_client,):
        super().__init__(mqtt_client)
        self.dining = first_floor_east_dining(mqtt_client)
        self.floor = first_floor_east_floor(mqtt_client)
        self.kitchen = first_floor_east_kitchen(mqtt_client)
        self.livingroom = first_floor_east_living(mqtt_client)
        self.sleep = first_floor_east_sleep(mqtt_client)


class first_floor_east_floor(rooms.ffe_floor, room):
    def __init__(self, mqtt_client):
        super().__init__(mqtt_client)
        room.__init__(self, mqtt_client)
        #
        # connect videv and switch
        self.videv_main_light.connect_sw_device(self.switch_main_light, self.switch_main_light.KEY_OUTPUT_0)


class first_floor_east_kitchen(rooms.ffe_kitchen, room):
    def __init__(self, mqtt_client):
        super().__init__(mqtt_client)
        room.__init__(self, mqtt_client)
        #
        # light <-> videv
        self.videv_main_light.connect_sw_device(self.switch_main_light, self.switch_main_light.KEY_OUTPUT_0)
        self.videv_main_light.connect_br_device(self.light_main_light, self.light_main_light.KEY_BRIGHTNESS)
        self.videv_main_light.connect_ct_device(self.light_main_light, self.light_main_light.KEY_COLOR_TEMP)

        # Request hue data of lead light after power on
        switched_light(self.switch_main_light, self.switch_main_light.KEY_OUTPUT_0, self.light_main_light)

        # circulation pump
        self.circulation_pump = timer_on_activation(self.switch_circulation_pump, self.switch_circulation_pump.KEY_OUTPUT_0, 10*60)
        self.switch_circulation_pump.add_callback(self.switch_circulation_pump.KEY_OUTPUT_0, True, self.switch_main_light.flash_0_mcb, True)
        self.videv_circulation_pump.connect_sw_device(self.switch_circulation_pump, self.switch_circulation_pump.KEY_OUTPUT_0)
        self.videv_circulation_pump.connect_tm_device(self.circulation_pump, timer_on_activation.KEY_TIMER)

        # heating function
        self.heating_function = heating_function(
            self.valve_heating,
            config.DEFAULT_TEMPERATURE,
            **get_radiator_data(self.valve_heating.topic)
        )
        self.heating_function.add_callback(None, None, set_radiator_data, True)
        self.videv_heating.connect_heating_function(self.heating_function)


class first_floor_east_dining(rooms.ffe_diningroom, room):
    def __init__(self, mqtt_client):
        super().__init__(mqtt_client)
        room.__init__(self, mqtt_client)
        #
        self.day_events = day_event((6, 0), (22, 0), 30, -30)
        self.day_events.add_callback(None, True, self.__day_events__, True)

        # light <-> videv
        self.videv_main_light.connect_sw_device(self.switch_main_light, self.switch_main_light.KEY_OUTPUT_0)
        self.videv_floor_light.connect_sw_device(self.switch_floor_light, self.switch_floor_light.KEY_OUTPUT_0)
        if config.CHRISTMAS:
            self.videv_garland_light.connect_sw_device(self.switch_garland_light, self.switch_garland_light.KEY_OUTPUT_0)

        # main light -> floor_light
        self.switch_main_light.add_callback(self.switch_main_light.KEY_OUTPUT_0, None, self.switch_floor_light.set_output_0_mcb, True)

        # heating function
        self.heating_function = heating_function(
            self.valve_heating,
            config.DEFAULT_TEMPERATURE,
            **get_radiator_data(self.valve_heating.topic)
        )
        self.heating_function.add_callback(None, None, set_radiator_data, True)
        # heating function <-> videv
        self.videv_heating.connect_heating_function(self.heating_function)

    def __day_events__(self, device, key, data):
        if key in (self.day_events.KEY_SUNSET, self.day_events.KEY_START_OF_DAY):
            if config.CHRISTMAS:
                self.switch_garland_light.set_output_0(True)
        elif key in (self.day_events.KEY_START_OF_NIGHT, self.day_events.KEY_SUNRISE):
            if config.CHRISTMAS:
                self.switch_garland_light.set_output_0(False)


class first_floor_east_sleep(rooms.ffe_sleep, room):
    def __init__(self, mqtt_client):
        super().__init__(mqtt_client)
        room.__init__(self, mqtt_client)
        #
        self.light_wardrobe_light.disable_all_off()   # Always on - Off by light sensor
        # light <-> videv
        self.videv_main_light.connect_sw_device(self.switch_main_light, self.switch_main_light.KEY_OUTPUT_0)
        self.videv_main_light.connect_br_device(self.light_main_light, self.light_main_light.KEY_BRIGHTNESS)
        self.videv_main_light.connect_ct_device(self.light_main_light, self.light_main_light.KEY_COLOR_TEMP)
        #
        self.videv_bed_dirk_light.connect_sw_device(self.light_bed_dirk_light, self.light_bed_dirk_light.KEY_OUTPUT_0)
        self.videv_bed_dirk_light.connect_br_device(self.light_bed_dirk_light, self.light_bed_dirk_light.KEY_BRIGHTNESS)
        #
        self.videv_bed_marion_light.connect_sw_device(self.switch_bed_marion_light, self.switch_bed_marion_light.KEY_OUTPUT_0)
        #
        self.videv_wardrobe_light.connect_sw_device(self.light_wardrobe_light, self.light_wardrobe_light.KEY_OUTPUT_0)
        self.videv_wardrobe_light.connect_br_device(self.light_wardrobe_light, self.light_wardrobe_light.KEY_BRIGHTNESS)

        # button / brightness function
        self.brightness_functions = brightness_choose_n_action(self.input_device)
        self.brightness_functions.add(self.light_main_light, self.switch_main_light, self.switch_main_light.KEY_OUTPUT_0)
        self.brightness_functions.add(self.light_bed_dirk_light, self.light_bed_dirk_light, self.light_bed_dirk_light.KEY_OUTPUT_0)
        # button / main light
        self.input_device.add_callback(self.input_device.KEY_ACTION, self.input_device.ACTION_TOGGLE, self.switch_main_light.toggle_output_0_mcb)
        # button / bed light
        self.input_device.add_callback(self.input_device.KEY_ACTION, self.input_device.ACTION_LEFT,  self.light_bed_dirk_light.toggle_output_0_mcb)
        self.input_device.add_callback(self.input_device.KEY_ACTION, self.input_device.ACTION_LEFT_LONG,
                                       self.switch_bed_marion_light.toggle_output_0_mcb)
        # button
        self.videv_multistate.connect_br_function(self.brightness_functions, brightness_choose_n_action.KEY_ACTIVE_DEVICE, 2)

        # heating function
        self.heating_function = heating_function(
            self.valve_heating,
            config.DEFAULT_TEMPERATURE,
            **get_radiator_data(self.valve_heating.topic)
        )
        self.heating_function.add_callback(None, None, set_radiator_data, True)
        self.videv_heating.connect_heating_function(self.heating_function)


class first_floor_east_living(rooms.ffe_livingroom, room):
    def __init__(self, mqtt_client):
        super().__init__(mqtt_client)
        room.__init__(self, mqtt_client)
        #
        # light <-> videv
        self.videv_main_light.connect_sw_device(self.switch_main_light, self.switch_main_light.KEY_OUTPUT_0)
        self.videv_main_light.connect_br_device(self.light_main_light, self.light_main_light.KEY_BRIGHTNESS)
        self.videv_main_light.connect_ct_device(self.light_main_light, self.light_main_light.KEY_COLOR_TEMP)
        #
        self.videv_floor_light.connect_sw_device(self.light_floor_light, self.light_floor_light.KEY_OUTPUT_0)
        self.videv_floor_light.connect_br_device(self.light_floor_light, self.light_floor_light.KEY_BRIGHTNESS)
        self.videv_floor_light.connect_ct_device(self.light_floor_light, self.light_floor_light.KEY_COLOR_TEMP)
        #
        if config.CHRISTMAS:
            self.videv_xmas_tree_light.connect_sw_device(self.switch_xmas_tree_light, self.switch_xmas_tree_light.KEY_OUTPUT_0)

        # main light -> floor_light
        self.switch_main_light.add_callback(self.switch_main_light.KEY_OUTPUT_0, None, self.light_floor_light.set_output_0_mcb, True)

        # heating function
        self.heating_function = heating_function(
            self.valve_heating,
            config.DEFAULT_TEMPERATURE,
            **get_radiator_data(self.valve_heating.topic)
        )
        self.heating_function.add_callback(None, None, set_radiator_data, True)
        self.videv_heating.connect_heating_function(self.heating_function)
