import bitmath

from marshmallow import fields, Schema


class StorageUnit(fields.Field):
    def _serialize(self, value, attr, obj, **kwargs):
        return str(value)

    def _deserialize(self, value, attr, data, **kwargs):
        return bitmath.parse_string_unsafe(value)


class SpecsSchema(Schema):
    cpu = fields.Int()
    ram = StorageUnit()
    os_ssd = StorageUnit(data_key="os-ssd", attribute="os-ssd")
    hdd = fields.List(StorageUnit())


class VMSchema(Schema):
    name = fields.Str()
    owner = fields.Str()
    owner_realm = fields.Str()
    specs = fields.Nested(SpecsSchema)
    status = fields.Str()
    log = fields.List(fields.Str())
    vnc_socket = fields.Str()
    image_uuid = fields.Str()
    hostname = fields.Str()
    metadata = fields.Dict()
    network = fields.List(
        fields.Tuple((fields.Str(), fields.Str(), fields.Int()))
    )
    in_migration = fields.Bool()


class NetworkSchema(Schema):
    _id = fields.Int(data_key="id", attribute="id")
    _type = fields.Str(data_key="type", attribute="type")
    ipv6 = fields.Str()
