import json
from os.path import abspath, dirname

from django import template
from django.conf import settings
from django.core.wsgi import get_wsgi_application
from django.template import Context, Template
from django.template.engine import Engine

from ak_vendor.report import Report

settings.configure()
application = get_wsgi_application()

CUR_DIR = dirname(abspath(__file__))
template.Library()


class ReportHTMLExporter:
    def __init__(self, report):
        self.report = report

    def to_html(self):
        tpl = open(f"{CUR_DIR}/templates/report_template.html").read()
        template = Template(
            tpl, engine=Engine(libraries={"i18n": "django.templatetags.i18n"})
        )
        context = Context({"report": self.report})
        content = template.render(context)
        return content

    def to_html_file(self, path=""):
        with open(f"{path}/output.html", "w") as file:
            tpl = self.to_html()
            file.write(tpl)


data = json.load(open(f"{CUR_DIR}/report_sample1.json"))
report_obj = Report.from_json(data)
ReportHTMLExporter(report_obj).to_html_file(CUR_DIR)
