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

from pymax.cube import Cube

from Homevee.Helper import Logger
from Homevee.Item import Item
from Homevee.Item.Gateway import Gateway, MAX_CUBE
from Homevee.Item.Status import *
from Homevee.Utils.Database_NEW import Database


def get_devices(ip):
    thermostats = []
    try:
        with Cube(ip) as cube:
            for room in cube.rooms:
                for device in room.devices:
                    Logger.log(device)
                    thermostat = {'title': device.name, 'id': device.serial, 'room': room.name}

                    thermostats.append(thermostat)
    except:
        thermostats = []

    return thermostats


def set_temp(id, value, db: Database = None):
    if db is None:
        db = Database()
    gateway = Item.load_from_db(Gateway, MAX_CUBE)

    try:
        with Cube(gateway.ip) as cube:
            for room in cube.rooms:
                for device in room.devices:
                    if id == device.serial:
                        result = cube.set_mode_manual(room.room_id, room.rf_address, float(value))

                        Logger.log(result)

                        db.update("UPDATE MAX_THERMOSTATS SET LAST_TEMP = :temp WHERE ID == :id",
                                    {'temp': value, 'id': id})

                        return Status(type=STATUS_OK).get_dict()
    except:
        return Status(type=STATUS_ERROR).get_dict()