import base64
from django import template
from core.qr_generator import generate_qr_code_svg, format_cz_payment_data

register = template.Library()


@register.filter(name='qr_code')
def qr_code(registration):
    # check if registration is class Registration
    if not hasattr(registration, 'event'):
        return ''

    try:
        data = format_cz_payment_data(
            account_number=registration.event.bank_account_number,
            recipient_name=registration.event.bank_account_name,
            amount=str(int(registration.event.fee)),
            message=registration.variable_symbol
        )
        tmp_code = generate_qr_code_svg(data)
        return base64.b64encode(tmp_code.getvalue()).decode('utf-8')
    except Exception as e:
        return ''


