import copy
from ..errors import BlobStorageNotImplementedError


class BaseStorage(object):

    use_transactions = True

    blobstorage = None

    @property
    def model(self):
        raise NotImplementedError

    def __init__(self, request, blobstorage=None):
        self.request = request
        self.app = request.app
        self.blobstorage = blobstorage

    def set_identifier(self, obj, identifier):
        for f, v in zip(
                self.app.get_identifierfields(self.model.schema),
                identifier.split(
                    self.app.get_compositekey_separator())):
            obj[f] = v

    def set_schema_defaults(self, data):
        obj = self.model.schema(data)
        return obj.to_json()

    def create(self, data):
        raise NotImplementedError

    def search(self, query=None, limit=None):
        raise NotImplementedError

    def get(self, identifier):
        raise NotImplementedError

    def get_by_uuid(self, uuid):
        raise NotImplementedError

    def update(self, identifier, data):
        raise NotImplementedError

    def delete(self, identifier, model):
        raise NotImplementedError

    def _blob_guard(self):
        if self.blobstorage is None:
            raise BlobStorageNotImplementedError(
                'Storage does not implement blobstorage')

    def get_blob(self, uuid):
        self._blob_guard()
        return self.blobstorage.get(uuid)

    def put_blob(self, fileobj, filename, mimetype=None, size=None, encoding=None):
        self._blob_guard()
        blob = self.blobstorage.put(
            fileobj, filename, mimetype, size, encoding)
        return blob

    def delete_blob(self, uuid):
        self._blob_guard()
        self.blobstorage.delete(uuid)
