from enum import Enum
import base64
import cloudpickle


class ClientTaskType(Enum):
    FUNCTION = 0
    ACTOR = 1
    METHOD = 2
    STATIC_METHOD = 3


class ClientTask:
    def __init__(self):
        self.type = 0
        self.name = ""
        self.payload_id = b''
        self.args = []

    def toJsonable(self):
        return {
            "type": self.type.value,
            "name": self.name,
            "payload_id": base64.standard_b64encode(self.payload_id).decode(),
            "args": [x.toJsonable() for x in self.args]
        }


class Locality(Enum):
    INTERNED = 0
    REFERENCE = 1


class Arg:
    def __init__(self):
        self.local = Locality.INTERNED
        self.reference_id = b''
        self.data = b''
        self.type = 0

    def toJsonable(self):
        return {
            "local": self.local.value,
            "reference_id": base64.standard_b64encode(self.reference_id).decode(),
            "data": base64.standard_b64encode(self.data).decode(),
            "type": self.type,
        }


def loads(b64):
    data = base64.standard_b64decode(b64)
    return cloudpickle.loads(data)


def dumps(obj):
    data = cloudpickle.dumps(obj)
    return base64.standard_b64encode(data).decode()
