from django.core.handlers.wsgi import WSGIRequest
from django.shortcuts import render
from django.views.generic import TemplateView
from django_weasyprint import WeasyTemplateView
from django.utils.timezone import datetime
from markdownx.utils import markdownify

from schickmacher.renderer.forms import RendererForm


def form_view(request: WSGIRequest):
    ctx = {"form": (RendererForm())}
    return render(request, 'renderer/render_form.html', ctx)


class HtmlRenderView(TemplateView):
    template_name = 'renderer/rendered.html'

    def get_context_data(self, **kwargs):
        form = RendererForm(self.request.POST)
        if form.is_valid():
            return {
                'date': form.cleaned_data['date'],
                'show_title': form.cleaned_data['show_title'],
                'content': markdownify(form.cleaned_data['render_content']),
                'title': form.cleaned_data['title']
            }
        else:
            return {
                'date': 'Error',
                'content': 'Error',
                'title': 'Error'
            }

    def post(self, request, *args, **kwargs):
        return self.get(request, *args, **kwargs)


class RenderView(HtmlRenderView, WeasyTemplateView):
    pass


class SampleHTMLView(TemplateView):
    template_name = 'renderer/rendered.html'

    sample_title = 'Test Title 123'
    sample_date = datetime.now()
    sample_content = markdownify(open('test.md').read())

    def get_context_data(self, **kwargs):
        return {
            'title': self.sample_title,
            'date': self.sample_date,
            'content': self.sample_content
        }


class SamplePDFView(SampleHTMLView, WeasyTemplateView):
    pass
