from .serializers import *

class RequestValidatorResultCode(Enum):
    SUCCESS = 0x00
    FAIL    = 0x01

class RequestValidatorResult:
    '''
    This class represent the object result class returned by the validators.
    '''
    def __init__(self, id, command, code, message) -> None:
        self.id = id
        self.command = command
        self.code = code
        self.message = message

    def toDictionary(self) -> dict:
        return {
            "type" : "request_validation_result",
            "id" : self.id,
            "command" : self.command,
            "code value" : self.code.value,
            "code name" : self.code.name,
            "message" : self.message
        }

#####################################################################
# ----------------------------- SYSTEM ------------------------------
#####################################################################

def getUsbStringValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = getUsbStringSerializer(metadata["id"], metadata["subcommand"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "GET USB STRING requests success")

def setI3cBusVoltageValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.

    requests, response = setI3cBusVoltSerializer(metadata["id"], metadata["i3cBusVoltage"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "SET I3C BUS VOLTAGE requests success")

def resetDeviceValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = resetDeviceSerializer(metadata["id"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "RESET DEVICE requests success")

def enterBootModeValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = enterBootModeSerializer(metadata["id"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "ENTER BOOT MODE requests success")

def setI2cSpiUartBusVoltValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.

    requests, response = setI2cSpiUartBusVoltSerializer(metadata["id"], metadata["i2cSpiUartBusVolt"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "SET I2C SPI UART BUS VOLTAGE requests success")

#####################################################################
# ------------------------------ I2C --------------------------------
#####################################################################

def i2cSetParametersValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i2cSetParametersSerializer(metadata["id"], metadata["cancelTransfer"], metadata["baudrate"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I2C SET PARAMETERS requests success")

def i2cWriteValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i2cWriteSerializer(metadata["id"], metadata["command"], metadata["slaveAddress"], metadata["registerAddress"], metadata["data"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I2C WRITE requests success")

def i2cWriteNonStopValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i2cWriteSerializer(metadata["id"], metadata["command"], metadata["slaveAddress"], metadata["registerAddress"], metadata["data"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I2C WRITE NON STOP requests success")

def i2cReadValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i2cReadSerializer(metadata["id"], metadata["command"], metadata["slaveAddress"], metadata["dataLength"], metadata["registerAddress"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I2C READ requests success")

def i2cReadFromValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i2cReadSerializer(metadata["id"], metadata["command"], metadata["slaveAddress"], metadata["dataLength"], metadata["registerAddress"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I2C READ FROM requests success")

#####################################################################
# ------------------------------ I3C --------------------------------
#####################################################################

def i3cInitBusValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cInitBusSerializer(metadata["id"], metadata["targetDeviceTable"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "INITIALIZE I3C BUS requests success")

def i3cGetTargetDeviceTableValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cGetTargetDeviceTableSerializer(metadata["id"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C GET TARGET DEVICE TABLE requests success")

def i3cGetCapabilityValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cGetCapabilitySerializer(metadata["id"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C GET CAPABILITY requests success")

def i3cClearFeatureValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cClearFeatureSerializer(metadata["id"], metadata["selector"], metadata["targetAddress"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C CLEAR FEATURE requests success")

def i3cSetFeatureValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cSetFeatureSerializer(metadata["id"], metadata["selector"], metadata["targetAddress"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C SET FEATURE requests success")

def i3cChangeDynamicAddressValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cChangeDynamicAddressSerializer(metadata["id"], metadata["currentAddress"], metadata["newAddress"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C CHANGE DYNAMIC ADDRESS requests success")

def i3cSetTargetDeviceConfigValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cSetTargetDeviceConfigSerializer(metadata["id"], metadata["entries"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C SET TARGET DEVICE CONFIG requests success")

def i3cWriteValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cWriteSerializer(metadata["id"], metadata["targetAddress"], metadata["mode"], metadata["pushPullRate"], metadata["openDrainRate"], metadata["registerAddress"], metadata["data"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C WRITE requests success")

def i3cReadValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cReadSerializer(metadata["id"], metadata["targetAddress"], metadata["mode"], metadata["pushPullRate"], metadata["openDrainRate"], metadata["registerAddress"], metadata["length"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C READ requests success")

def i3cSendCccValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cSendCccSerializer(metadata["id"], metadata["commandType"], metadata["isReadOrWrite"],  metadata["targetAddress"], metadata["mode"], metadata["pushPullRate"], metadata["openDrainRate"], metadata["defByte"], metadata["ccc"], metadata["length"], metadata["data"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C SEND CCC requests success")

def i3cTargetResetValidator(metadata: dict) :

    # TODO: Check data validation. Use module inspect and/or try-except block.
    # TODO: Ckeck command value.

    requests, response = i3cTargetResetSerializer(metadata["id"], metadata["isReadOrWrite"], metadata["targetAddress"], metadata["pushPullRate"], metadata["openDrainRate"], metadata["defByte"])
    return requests, response, RequestValidatorResult(metadata["id"], metadata["command"], RequestValidatorResultCode.SUCCESS, "I3C TARGET RESET requests success")
