# -*- coding: UTF-8 -*-

import base64
import binascii
import hashlib
import logging
import os
import uuid

from django_simple_files import settings
from django_simple_files.settings import SIMPLE_FILES

logger = logging.getLogger('django')


def crc32(file_blob):
    """
    从进制获取文件crc32信息
    :param file_blob:
    :return:
    """
    crc_code = binascii.crc32(file_blob)
    if crc_code > 0:
        crc_int = crc_code
    else:
        crc_int = ~ crc_code ^ 0xffffffff
    return '%x' % crc_int


def crc32_from_path(file_path):
    """
    从路径文件crc32信息
    :param file_path:
    :return:
    """
    with open(file_path, 'rb') as f:
        return crc32(f.read())


def blob_from_h5base64(blob_from_h5base64_data):
    """
    从前端预览上传的Base64编码获取data数据
    :param blob_from_h5base64_data:
    :return:
    """
    return blob_from_base64(blob_from_h5base64_data.split(',')[-1])


def blob_from_base64(base64_data):
    """
    从Base64完整data中获取二进制data数据
    :param base64_data:
    :return:
    """
    return base64.b64decode(base64_data)


def md5(file_blob):
    """
    从二进制对象获取md5信息
    :param file_blob:
    :return:
    """
    return hashlib.md5(file_blob).hexdigest()


def md5_from_path(file_path):
    """
    从文件获取md5信息
    :param file_path:
    :return:
    """
    with open(file_path, "rb") as f:
        return md5(f.read())


def sha1(file_blob):
    """
    从二进制对象获取md5信息
    :param file_blob:
    :return:
    """
    return hashlib.sha1(file_blob).hexdigest()


def sha1_from_path(file_path):
    """
    从文件获取md5信息
    :param file_path:
    :return:
    """
    with open(file_path, "rb") as f:
        return sha1(f.read())


def hash_uuid():
    return str(uuid.uuid1()).replace('-', '')


def get_object_name(obj):
    return '%s.%s' % (obj.__module__, obj.__class__.__name__)


def get_file_hash(content):
    hash_fun = get_file_fun_name()
    if hash_fun == "CRC32":
        return crc32(content)
    elif hash_fun == "SHA1":
        return sha1(content)
    else:
        return md5(content)


def get_uuid_name(name):
    hash_name = hash_uuid()
    file_ext = os.path.splitext(name)[-1]
    return hash_name + file_ext


# settings

def get_setting_value(name):
    simple_files_name = 'SIMPLE_FILES'
    if hasattr(settings, simple_files_name):
        simple_files_dict = getattr(settings, simple_files_name)
        if isinstance(simple_files_dict, dict):
            return simple_files_dict.get(name) or SIMPLE_FILES.get(name)


def get_file_fun_name():
    file_hash_fun_key = 'FILE_HASH_FUN'
    file_hash_fun = get_setting_value(file_hash_fun_key)
    return file_hash_fun if file_hash_fun and file_hash_fun == 'CRC32' else SIMPLE_FILES.get(file_hash_fun_key)


def get_upload_to():
    file_upload_to_key = 'FILE_UPLOAD_TO'
    upload_to = get_setting_value(file_upload_to_key)
    return upload_to if upload_to else SIMPLE_FILES.get(file_upload_to_key)


def get_file_auto_hashname():
    file_auto_hashname_key = 'FILE_AUTO_HASHNAME'
    file_auto_hashname = get_setting_value(file_auto_hashname_key)
    return file_auto_hashname if file_auto_hashname else SIMPLE_FILES.get(file_auto_hashname_key)
