# -*- coding: UTF-8 -*-
import logging
import os

from django_simple_files import models, utils, storage

logger = logging.getLogger('django')


def save(name, content, uploader_uid=0, obj_id=0):
    """
    保存文件，同时保存文件至数据库
    :param obj_id:
    :param name:
    :param content:
    :param uploader_uid:
    :return:
    """

    file_name = name

    if utils.get_file_auto_hashname():
        name = utils.get_uuid_name(name)

    name = '%s_%s' % (uploader_uid, name)

    logger.debug(name)

    if obj_id > 0:
        file_db = models.Files.objects.get(pk=obj_id)
        name = file_db.file_uri.name
        if storage.exists(name):
            storage.delete(name)
        file_db.file_uri.name = storage.save(name, content)
    else:
        file_db = models.Files()
        file_db.uploader_uid = uploader_uid
        storage.save(name, content, file_db)

    file_db.file_name = file_name
    file_db.file_hash = utils.get_file_hash(content)
    file_db.file_size = len(content)

    file_db.save()
    return file_db


def save_by_uuid(file_name, content, uploader_uid=0):
    """
    随机生成文件名保存
    :param file_name:
    :param content:
    :param uploader_uid:
    :return:
    """
    hash_name = utils.hash_uuid()
    file_ext = os.path.splitext(file_name)[-1]
    return save(hash_name + file_ext, content, uploader_uid)


def save_related(file_db, related_object):
    """
    保存关系表
    :param file_db:
    :param related_object:
    :return:
    """
    file_related = models.FilesRelated()
    file_related.file = file_db
    file_related.file_uri = file_db.file_uri
    file_related.related_object_id = related_object.pk
    file_related.related_object = utils.get_object_name(related_object)
    file_related.save()
    return file_related


def get_related_by_object(related_object, page=1, page_size=20):
    """
    根据对象名与对象pk返回相关数据
    :param related_object:
    :param page:
    :param page_size:
    :return:
    """
    return _file_related_filter_by_object(related_object)[(page - 1) * page_size:page_size]


def get_related(related_object, page=1, page_size=20):
    """
    根据对象名返回相关数据
    :param related_object:
    :param page:
    :param page_size:
    :return:
    """
    return _file_related_filter(utils.get_object_name(related_object))[(page - 1) * page_size:page_size]


def delete_related_by_object(related_object):
    _file_related_filter(related_object).delete()


def _file_related_filter_by_object(related_object, file_id=0):
    related_object_name = utils.get_object_name(related_object)
    related_object_id = related_object.pk if hasattr(related_object, 'pk') else None
    return _file_related_filter(related_object_name, related_object_id, file_id)


def _file_related_filter(related_object_name, related_object_id=None, file_id=0):
    query = {'related_object': related_object_name}
    if related_object_id:
        query['related_object_id'] = related_object_id
    if file_id:
        query['file_id'] = file_id
    return models.FilesRelated.objects.filter(**query)
