import uuid
import logging

from defpi.Connection import Connection
from defpi.Service import Service

from .InflexibleControllerEfi20ConnectionHandler import InflexibleControllerEfi20ConnectionHandler
from service.model.InflexibleControllerEfi20.xsd import CurtailmentProfileElement, CurtailmentProfile, \
    CurtailmentQuantity, InflexibleInstruction


class InflexibleControllerEfi20ConnectionHandlerImpl(InflexibleControllerEfi20ConnectionHandler):
    """ InflexibleControllerEfi20ConnectionHandlerImpl
        Handles incoming messages from dEF-Pi connections
        Generated by Python code generator for dEF-Pi at May 31, 2018, 9:34:25 AM by maarten

        NOTE: This file is generated as a stub, and has to be implemented by the user. Re-running the codegen plugin
        will not change the contents of this file.

        Template by FAN, 2017 """
    logger = logging.getLogger("ConnectionHandler")

    def __init__(self, connection: Connection, service: Service):
        """ Auto-generated constructor building the manager for the provided service """
        self.service = service
        self.connection = connection

    def onSuspend(self):
        """ Called when the connection is suspended
            Auto-generated method stub """
        pass

    def resumeAfterSuspend(self):
        """ Called when the connection is being suspended
            Auto-generated method stub """
        pass

    def onInterrupt(self):
        """ Called when the connection is interrupted
            Auto-generated method stub """
        pass

    def resumeAfterInterrupt(self):
        """ Called when the connection is restored after an interruption
            Auto-generated method stub """
        pass

    def terminated(self):
        """ Called when the connection is terminated
            Auto-generated method stub """
        pass

    def handleMeasurementMessage(self, message):
        self.logger.info("Measurement value received: {}".format(message.electricityMeasurement.power))

    def handleInflexibleCurtailmentOptionsMessage(self, message):
        self.logger.info("Received curtailment options: {}".format(message.toDOM().toprettyxml()))
        minimalPower = message.curtailmentOptions.curtailmentOption[0].curtailmentRange[0].lowerBound
        curtailMent = CurtailmentProfileElement(value_=minimalPower,
                                                duration="PT24H")

        profile = CurtailmentProfile(startTime=message.validFrom,
                                     curtailmentQuantity=CurtailmentQuantity.ELECTRICITY_POWER,
                                     curtailmentProfileElement=curtailMent)

        instruction = InflexibleInstruction(efiVersion="2.0",
                                            instructionId=str(uuid.uuid4()),
                                            curtailmentProfile=profile)
        self.connection.send(instruction)

    def handleInflexibleRegistrationMessage(self, message):
        self.logger.info("Received registration of " + message.deviceDescription.label)
