import datetime
import posixpath
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.utils.encoding import force_str


class ResumableStorage(object):

    def __init__(self):
        self.persistent_storage_class_name = getattr(settings, 'ADMIN_RESUMABLE_STORAGE', None) or \
                                        getattr(settings, 'DEFAULT_FILE_STORAGE',
                                                'django.core.files.storage.FileSystemStorage')

        # get_storage_class removed in Django 5.1:
        # self.chunk_storage_class_name = getattr(
        #     settings,
        #     'ADMIN_RESUMABLE_CHUNK_STORAGE',
        #     'django.core.files.storage.FileSystemStorage'
        # )

    def get_chunk_storage(self, *args, **kwargs):
        """
        Returns storage class specified in settings as ADMIN_RESUMABLE_CHUNK_STORAGE.
        Defaults to django.core.files.storage.FileSystemStorage.
        Chunk storage should be highly available for the server as saved chunks must be copied by the server
        for saving merged version in persistent storage.
        """
        # get_storage_class removed in Django 5.1:
        # storage_class = get_storage_class(self.chunk_storage_class_name)
        # return storage_class(*args, **kwargs)
        return FileSystemStorage(*args, **kwargs)

    def get_persistent_storage(self, *args, **kwargs):
        """
        Returns storage class specified in settings as ADMIN_RESUMABLE_STORAGE
        or DEFAULT_FILE_STORAGE if the former is not found.

        Defaults to django.core.files.storage.FileSystemStorage.
        """
        # get_storage_class removed in Django 5.1:
        # storage_class = get_storage_class(self.persistent_storage_class_name)
        # return storage_class(*args, **kwargs)
        return FileSystemStorage(*args, **kwargs)

    def full_filename(self, filename, upload_to, instance=None):
        if callable(upload_to):
            filename = upload_to(instance, filename)
        else:
            dirname = force_str(datetime.datetime.now().strftime(force_str(upload_to)))
            filename = posixpath.join(dirname, filename)
        return self.get_persistent_storage().generate_filename(filename)
