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

import config
from devdi import topic as props
from devices import group
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
from function.rooms import room, room_collection
from function.videv import videv_switching, videv_switch_brightness, videv_switching_timer, videv_switch_brightness_color_temp, videv_heating, videv_multistate
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__)

loc = props.LOC_FFE


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


class first_floor_east_floor(room):
    def __init__(self, mqtt_client, pd, vd):
        roo = props.ROO_FLO
        #
        # Device initialisation
        #
        # http://shelly1l-3C6105E4E629
        # main light
        self.main_light_shelly = pd.get(props.STG_SHE, loc, roo, props.FUN_MAL)
        super().__init__(mqtt_client, pd, vd)

        #
        # Virtual Device Interface
        #
        # main light
        self.main_light = videv_switching(
            mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_VIDEV,
            self.main_light_shelly, self.main_light_shelly.KEY_OUTPUT_0
        )


class first_floor_east_kitchen(room):
    def __init__(self, mqtt_client, pd, vd):
        roo = props.ROO_KIT
        #
        # Device initialisation
        #
        # http://shelly1l-8CAAB5616C01
        # main light
        self.main_light_shelly = pd.get(props.STG_SHE, loc, roo, props.FUN_MAL)
        # http://shelly1-e89f6d85a466
        # circulation pump
        self.circulation_pump_shelly = pd.get(props.STG_SHE, loc, roo, props.FUN_CIR)
        # heating function
        self.heating_valve = pd.get(props.STG_ZFE, loc, roo, props.FUN_HEA)

        super().__init__(mqtt_client, pd, vd)

        #
        # Functionality initialisation
        #
        # circulation pump
        self.circulation_pump = timer_on_activation(self.circulation_pump_shelly, self.circulation_pump_shelly.KEY_OUTPUT_0, 10*60)
        self.circulation_pump_shelly.add_callback(self.circulation_pump_shelly.KEY_OUTPUT_0, True, self.main_light_shelly.flash_0_mcb, True)
        # heating function
        self.heating_function = heating_function(
            self.heating_valve,
            config.DEFAULT_TEMPERATURE,
            **get_radiator_data(self.heating_valve.topic)
        )
        self.heating_function.add_callback(None, None, set_radiator_data, True)

        #
        # Virtual Device Interface
        #
        # main light
        self.main_light_videv = videv_switching(
            mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_VIDEV,
            self.main_light_shelly, self.main_light_shelly.KEY_OUTPUT_0
        )
        # circulation pump
        self.circulation_pump_videv = videv_switching_timer(
            mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_VIDEV,
            self.circulation_pump_shelly, self.circulation_pump_shelly.KEY_OUTPUT_0,
            self.circulation_pump, timer_on_activation.KEY_TIMER
        )
        # heating function
        self.heating_function_videv = videv_heating(
            mqtt_client, config.TOPIC_FFE_KITCHEN_HEATING_VALVE_VIDEV,
            self.heating_function
        )


class first_floor_east_dining(room):
    def __init__(self, mqtt_client, pd, vd):
        roo = props.ROO_DIN
        #
        # Device initialisation
        #
        self.day_events = day_event((6, 0), (22, 0), 30, -30)

        # http://shelly1l-84CCA8ADD055
        # main light
        self.main_light_shelly = pd.get(props.STG_SHE, loc, roo, props.FUN_MAL)
        # floor lamp
        self.floorlamp_powerplug = pd.get(props.STG_ZFE, loc, roo, props.FUN_FLL)
        # heating function
        self.heating_valve = pd.get(props.STG_ZFE, loc, roo, props.FUN_HEA)
        # garland
        if config.CHRISTMAS:
            self.garland_powerplug = pd.get(props.STG_ZFE, loc, roo, props.FUN_GAR)
        super().__init__(mqtt_client, pd, vd)

        #
        # Functionality initialisation
        #
        self.day_events.add_callback(None, True, self.__day_events__, True)

        # main light
        self.main_light_shelly.add_callback(self.main_light_shelly.KEY_OUTPUT_0, None, self.floorlamp_powerplug.set_output_0_mcb, True)

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

        #
        # Virtual Device Interface
        #
        # main light
        self.main_light_videv = videv_switching(
            mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_VIDEV,
            self.main_light_shelly, self.main_light_shelly.KEY_OUTPUT_0
        )
        # floor lamp
        self.floorlamp_videv = videv_switching(
            mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_VIDEV,
            self.floorlamp_powerplug, self.floorlamp_powerplug.KEY_OUTPUT_0
        )
        # heating function
        self.heating_function_videv = videv_heating(
            mqtt_client, config.TOPIC_FFE_DININGROOM_HEATING_VALVE_VIDEV,
            self.heating_function
        )
        # garland
        if config.CHRISTMAS:
            self.garland_videv = videv_switching(
                mqtt_client, config.TOPIC_FFE_DININGROOM_GARLAND_VIDEV,
                self.garland_powerplug, self.garland_powerplug.KEY_OUTPUT_0
            )

    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.garland_powerplug.set_output_0(True)
        elif key in (self.day_events.KEY_START_OF_NIGHT, self.day_events.KEY_SUNRISE):
            if config.CHRISTMAS:
                self.garland_powerplug.set_output_0(False)


class first_floor_east_sleep(room):
    def __init__(self, mqtt_client, pd, vd):
        roo = props.ROO_SLP
        #
        # Device initialisation
        #
        # http://shelly1l-E8DB84A254C7
        # main light
        self.main_light_shelly = pd.get(props.STG_SHE, loc, roo, props.FUN_MAL)
        self.main_light_tradfri = pd.get(props.STG_ZFE, loc, roo, props.FUN_MAL)
        # bed light
        self.bed_light_di_tradfri = pd.get(props.STG_ZFE, loc, roo, props.FUN_BLD)
        self.bed_light_ma_powerplug = pd.get(props.STG_ZFE, loc, roo, props.FUN_BLM)
        # heating function
        self.heating_valve = pd.get(props.STG_ZFE, loc, roo, props.FUN_HEA)
        # button
        self.button_tradfri = pd.get(props.STG_ZFE, loc, roo, props.FUN_INP)
        # wardrobe light
        self.wardrobe_light = pd.get(props.STG_ZFE, loc, roo, props.FUN_WLI)
        self.wardrobe_light.disable_all_off()   # Always on - Off by light sensor

        super().__init__(mqtt_client, pd, vd)

        #
        # Functionality initialisation
        #
        # button / brightness function
        self.brightness_functions = brightness_choose_n_action(self.button_tradfri)
        self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, self.main_light_shelly.KEY_OUTPUT_0)
        self.brightness_functions.add(self.bed_light_di_tradfri, self.bed_light_di_tradfri, self.bed_light_di_tradfri.KEY_OUTPUT_0)
        # button / main light
        self.button_tradfri.add_callback(self.button_tradfri.KEY_ACTION, self.button_tradfri.ACTION_TOGGLE,
                                         self.main_light_shelly.toggle_output_0_mcb)
        # button / bed light
        self.button_tradfri.add_callback(self.button_tradfri.KEY_ACTION, self.button_tradfri.ACTION_LEFT,
                                         self.bed_light_di_tradfri.toggle_output_0_mcb)
        self.button_tradfri.add_callback(self.button_tradfri.KEY_ACTION, self.button_tradfri.ACTION_LEFT_LONG,
                                         self.bed_light_ma_powerplug.toggle_output_0_mcb)

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

        #
        # Virtual Device Interface
        #
        # main light
        self.main_light_videv = videv_switch_brightness_color_temp(
            mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_VIDEV,
            self.main_light_shelly, self.main_light_shelly.KEY_OUTPUT_0,
            self.main_light_tradfri, self.main_light_tradfri.KEY_BRIGHTNESS,
            self.main_light_tradfri, self.main_light_tradfri.KEY_COLOR_TEMP
        )
        # bed light
        self.bed_light_di_videv = videv_switch_brightness(
            mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_VIDEV,
            self.bed_light_di_tradfri, self.bed_light_di_tradfri.KEY_OUTPUT_0,
            self.bed_light_di_tradfri, self.bed_light_di_tradfri.KEY_BRIGHTNESS,
        )
        self.bed_light_ma_videv = videv_switching(
            mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_MA_VIDEV,
            self.bed_light_ma_powerplug, self.bed_light_ma_powerplug.KEY_OUTPUT_0
        )
        # heating function
        self.heating_function_videv = videv_heating(
            mqtt_client, config.TOPIC_FFE_SLEEP_HEATING_VALVE_VIDEV,
            self.heating_function
        )
        # button
        self.brightness_functions_device_videv = videv_multistate(
            mqtt_client, config.TOPIC_FFE_SLEEP_ACTIVE_BRIGHTNESS_DEVICE_VIDEV,
            brightness_choose_n_action.KEY_ACTIVE_DEVICE, self.brightness_functions, 2
        )
        self.wardrobe_light_videv = videv_switch_brightness(
            mqtt_client, config.TOPIC_FFE_SLEEP_WARDROBE_LIGHT_VIDEV,
            self.wardrobe_light, self.wardrobe_light.KEY_OUTPUT_0,
            self.wardrobe_light, self.wardrobe_light.KEY_BRIGHTNESS,
        )


class first_floor_east_living(room):
    def __init__(self, mqtt_client, pd, vd):
        roo = props.ROO_LIV
        #
        # Device initialisation
        #
        # http://shelly1l-3C6105E3F910
        # main light
        self.main_light_shelly = pd.get(props.STG_SHE, loc, roo, props.FUN_MAL)
        self.main_light_tradfri = pd.get(props.STG_ZFE, loc, roo, props.FUN_MAL)
        # floor lamp
        self.floorlamp_tradfri = pd.get(props.STG_ZFE, loc, roo, props.FUN_FLL)
        # heating function
        self.heating_valve = pd.get(props.STG_ZFE, loc, roo, props.FUN_HEA)
        # xmas tree
        if config.CHRISTMAS:
            self.powerplug_xmas_tree = pd.get(props.STG_ZFE, loc, roo, props.FUN_XTR)
            self.powerplug_xmas_star = pd.get(props.STG_ZFE, loc, roo, props.FUN_XST)

        super().__init__(mqtt_client, pd, vd)

        #
        # Functionality initialisation
        #
        # floor lamp synchronisation with main_light
        self.main_light_shelly.add_callback(self.main_light_shelly.KEY_OUTPUT_0, None, self.floorlamp_tradfri.set_output_0_mcb, True)
        # heating function
        self.heating_function = heating_function(
            self.heating_valve,
            config.DEFAULT_TEMPERATURE,
            **get_radiator_data(self.heating_valve.topic)
        )
        self.heating_function.add_callback(None, None, set_radiator_data, True)

        #
        # Virtual Device Interface
        #
        # main light
        self.main_light_videv = videv_switch_brightness_color_temp(
            mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_VIDEV,
            self.main_light_shelly, self.main_light_shelly.KEY_OUTPUT_0,
            self.main_light_tradfri, self.main_light_tradfri.KEY_BRIGHTNESS,
            self.main_light_tradfri, self.main_light_tradfri.KEY_COLOR_TEMP
        )
        # floor lamp
        self.floorlamp_videv = videv_switch_brightness_color_temp(
            mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_VIDEV,
            self.floorlamp_tradfri, self.floorlamp_tradfri.KEY_OUTPUT_0,
            self.floorlamp_tradfri, self.floorlamp_tradfri.KEY_BRIGHTNESS,
            self.floorlamp_tradfri, self.floorlamp_tradfri.KEY_COLOR_TEMP
        )
        # heating function
        self.heating_function_videv = videv_heating(
            mqtt_client, config.TOPIC_FFE_LIVINGROOM_HEATING_VALVE_VIDEV,
            self.heating_function
        )
        # xmas tree
        if config.CHRISTMAS:
            self.xmas_tree_videv = videv_switching(
                mqtt_client, config.TOPIC_FFE_LIVINGROOM_XMAS_TREE_VIDEV,
                self.powerplug_xmas_tree, self.powerplug_xmas_tree.KEY_OUTPUT_0
            )