from django import template
from django.template.defaultfilters import stringfilter
from django.conf import settings

register = template.Library()

# from django.core.serializers import serialize
# from django.db.models.query import QuerySet
# # import simplejson
# from django.template import Library
# from django.utils.html import mark_safe

# @register.filter('get_msg_side')

@register.simple_tag
def get_ws_address():
    return settings.WS_ADDRESS

@register.simple_tag
def get_filesize(size):
    if size:
        unit = 'B'
        if size / 1024 > 1:
            unit = 'KB'
            size = size / 1024
            if size / 1024 > 1:
                unit = 'MB'
                size = size / 1024
                if size / 1024 > 1:
                    unit = 'GB'
                    size = size / 1024
                    if size / 1024 > 1:
                        unit = 'TB'
                        size = size / 1024
        size = round(size, 2)
        return f'{str(size)}{unit}'
    else:
        return ''


@register.simple_tag
def get_msg_side(cur_user, ticket, msg):
    if msg:
        if msg.sender == cur_user:
            return 'right'
        else:
            return 'left'
    elif ticket:
        if ticket.owner == cur_user:
            return 'right'
        else:
            return 'left'
    else:
        return 'right'

    # return 'left'