#!/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.modules import brightness_choose_n_action, heating_function, switched_light
from function.rooms import room, room_collection
import logging
import task

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 ground_floor_west(room_collection):
    def __init__(self, mqtt_client):
        super().__init__(mqtt_client)
        self.dirk = ground_floor_west_dirk(mqtt_client)
        self.floor = ground_floor_west_floor(mqtt_client)
        self.marion = ground_floor_west_marion(mqtt_client)


class ground_floor_west_dirk(rooms.gfw_dirk, room):
    STATE_ACTIVE_DEVICE_MAIN_LIGHT = 0
    STATE_ACTIVE_DEVICE_DESK_LIGHT = 1
    STATE_ACTIVE_DEVICE_AMPLIFIER = 2
    STATE_ACTIVE_DEVICE_MAX_VALUE = STATE_ACTIVE_DEVICE_AMPLIFIER
    #
    AUDIO_SOURCE_PC = 0
    AUDIO_SOURCE_CD = 1
    AUDIO_SOURCE_RASPI = 2
    AUDIO_SOURCE_BT = 3
    AUDIO_SOURCE_PHONO = 4

    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_desk_light.connect_sw_device(self.light_desk_light, self.light_desk_light.KEY_OUTPUT_0)
        self.videv_desk_light.connect_br_device(self.light_desk_light, self.light_desk_light.KEY_BRIGHTNESS)
        self.videv_desk_light.connect_ct_device(self.light_desk_light, self.light_desk_light.KEY_COLOR_TEMP)
        #
        self.videv_amplifier.connect_sw_device(self.switch_powerplug_4, self.KEY_POWERPLUG_AMPLIFIER)
        self.videv_bluetooth.connect_sw_device(self.switch_powerplug_4, self.KEY_POWERPLUG_BT)
        self.videv_cd_player.connect_sw_device(self.switch_powerplug_4, self.KEY_POWERPLUG_CD_PLAYER)
        self.videv_phono.connect_sw_device(self.switch_powerplug_4, self.KEY_POWERPLUG_PHONO)
        #
        self.videv_pc_dock.connect_sw_device(self.switch_pc_dock, self.switch_pc_dock.KEY_OUTPUT_0)

        # amplifier on, if playing device on
        self.switch_powerplug_4.add_callback(self.KEY_POWERPLUG_PHONO, None, self.switch_powerplug_4.set_output_0_mcb, True)
        self.switch_powerplug_4.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.switch_powerplug_4.set_output_0_mcb, True)
        self.switch_powerplug_4.add_callback(self.KEY_POWERPLUG_BT, None, self.switch_powerplug_4.set_output_0_mcb, True)
        # amplifier on, if player on
        self.audio_status_bluetooth.add_callback(self.audio_status_bluetooth.KEY_STATE, None, self.switch_powerplug_4.set_output_0_mcb, True)
        self.audio_status_mpd.add_callback(self.audio_status_mpd.KEY_STATE, None, self.switch_powerplug_4.set_output_0_mcb, True)
        self.audio_status_spotify.add_callback(self.audio_status_spotify.KEY_STATE, None, self.switch_powerplug_4.set_output_0_mcb, True)

        # Audio source selection
        self.switch_powerplug_4.add_callback(self.KEY_POWERPLUG_AMPLIFIER, True, self.audio_source_selector, True)
        self.switch_powerplug_4.add_callback(self.KEY_POWERPLUG_CD_PLAYER, True, self.audio_source_selector, True)
        self.switch_powerplug_4.add_callback(self.KEY_POWERPLUG_BT, True, self.audio_source_selector, True)
        self.switch_powerplug_4.add_callback(self.KEY_POWERPLUG_PHONO, True, self.audio_source_selector, True)
        self.audio_status_bluetooth.add_callback(self.audio_status_bluetooth.KEY_STATE, True, self.audio_source_selector, True)
        self.audio_status_mpd.add_callback(self.audio_status_mpd.KEY_STATE, True, self.audio_source_selector, True)
        self.audio_status_spotify.add_callback(self.audio_status_spotify.KEY_STATE, True, self.audio_source_selector, True)
        self.audio_source = self.AUDIO_SOURCE_PC
        self.delayed_task_remote = task.delayed(1.0, self.send_audio_source)

        # input device functions
        # Brightness functionality
        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_desk_light, self.light_desk_light, self.light_desk_light.KEY_OUTPUT_0)
        self.brightness_functions.add(self.remote_ctrl, self.switch_powerplug_4, self.KEY_POWERPLUG_AMPLIFIER)
        # 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 - Desk light
        self.input_device.add_callback(self.input_device.KEY_ACTION, self.input_device.ACTION_RIGHT,
                                       self.light_desk_light.toggle_output_0_mcb)
        # Button - Amplifier
        self.input_device.add_callback(self.input_device.KEY_ACTION, self.input_device.ACTION_LEFT_LONG,
                                       self.switch_powerplug_4.toggle_output_0_mcb)
        # Button - CD player
        self.input_device.add_callback(self.input_device.KEY_ACTION, self.input_device.ACTION_RIGHT_LONG,
                                       self.switch_powerplug_4.toggle_output_2_mcb)
        # Button - PC dock
        self.input_device.add_callback(self.input_device.KEY_ACTION, self.input_device.ACTION_LEFT,
                                       self.switch_pc_dock.toggle_output_0_mcb)

        # additional videv connections
        self.videv_multistate.connect_br_function(self.brightness_functions, brightness_choose_n_action.KEY_ACTIVE_DEVICE, 3)
        #
        self.videv_audio_player.connect_audio_device(self.audio_status_bluetooth)
        self.videv_audio_player.connect_audio_device(self.audio_status_mpd)
        self.videv_audio_player.connect_audio_device(self.audio_status_spotify)

        # 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 audio_source_selector(self, device, key, data):
        if device == self.switch_powerplug_4 and key == self.KEY_POWERPLUG_CD_PLAYER:
            # switch on of cd player
            self.audio_source = self.AUDIO_SOURCE_CD
        elif device == self.switch_powerplug_4 and key == self.KEY_POWERPLUG_BT:
            # switch on of bluetooth
            self.audio_source = self.AUDIO_SOURCE_BT
        elif device == self.switch_powerplug_4 and key == self.KEY_POWERPLUG_PHONO:
            # switch on of bluetooth
            self.audio_source = self.AUDIO_SOURCE_PHONO
        elif device in [self.audio_status_spotify, self.audio_status_mpd, self.audio_status_bluetooth]:
            # switch on raspi-source
            self.audio_source = self.AUDIO_SOURCE_RASPI
        elif device == self.switch_powerplug_4 and key == self.KEY_POWERPLUG_AMPLIFIER:
            # switch on of amplifier -> select source and reset stored source value
            self.delayed_task_remote.run()

    def send_audio_source(self):
        if self.audio_source == self.AUDIO_SOURCE_PC:
            logger.info("Sending IR command to change audio source to pc")
            self.remote_ctrl.set_line3()
        elif self.audio_source == self.AUDIO_SOURCE_CD:
            logger.info("Sending IR command to change audio source to cd")
            self.remote_ctrl.set_cd()
        elif self.audio_source == self.AUDIO_SOURCE_BT:
            logger.info("Sending IR command to change audio source to bluetooth")
            self.remote_ctrl.set_line2()
        elif self.audio_source == self.AUDIO_SOURCE_PHONO:
            logger.info("Sending IR command to change audio source to phono")
            self.remote_ctrl.set_phono()
        elif self.audio_source == self.AUDIO_SOURCE_RASPI:
            logger.info("Sending IR command to change audio source to raspi")
            self.remote_ctrl.set_line1()
        self.audio_source = self.AUDIO_SOURCE_PC


class ground_floor_west_floor(rooms.gfw_floor, room):
    def __init__(self, mqtt_client):
        super().__init__(mqtt_client)
        room.__init__(self, mqtt_client)
        #
        # Request silvercrest data of lead light after power on
        switched_light(self.switch_main_light, self.switch_main_light.KEY_OUTPUT_0, self.light_main_light)
        # 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)


class ground_floor_west_marion(rooms.gfw_marion, 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_window_light.connect_sw_device(self.light_window_light, self.light_window_light.KEY_OUTPUT_0)
        self.videv_window_light.connect_br_device(self.light_window_light, self.light_window_light.KEY_BRIGHTNESS)
        self.videv_window_light.connect_ct_device(self.light_window_light, self.light_window_light.KEY_COLOR_TEMP)

        # main light -> window_light
        self.switch_main_light.add_callback(self.switch_main_light.KEY_OUTPUT_0, None, self.light_window_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)
