from django.shortcuts import render
from django.utils.safestring import mark_safe
from django.template.loader import render_to_string
from django.http import Http404

from pathlib import Path

import json
import os
# Create your views here.

REFERENZ_DIR = Path(__file__).resolve().parent

STACK_LINK = {
    'django': "<a class=\"hover:text-white transition\" href=\"https://docs.djangoproject.com\" target=\"_blank\">django</a>",
    'bootstrap': "<a class=\"hover:text-white transition\" href=\"https://getbootstrap.com/\" target=\"_blank\">bootstrap</a>",
    'Vue.js': "<a class=\"hover:text-white transition\" href=\"https://vuejs.org/\" target=\"_blank\">Vue.js</a>",
    'tailwindcss': "<a class=\"hover:text-white transition\" href=\"https://tailwindcss.com/\" target=\"_blank\">tailwindcss</a>",
    'nginx': "<a class=\"hover:text-white transition\" href=\"https://nginx.org/\" target=\"_blank\">nginx</a>",
    'gunicorn': "<a class=\"hover:text-white transition\" href=\"https://gunicorn.org/\" target=\"_blank\">gunicorn</a>",
    'daphne': "<a class=\"hover:text-white transition\" href=\"https://github.com/django/daphne\" target=\"_blank\">daphne</a>",
    'redis': "<a class=\"hover:text-white transition\" href=\"https://redis.io/\" target=\"_blank\">redis</a>",
    'SQLite': "<a class=\"hover:text-white transition\" href=\"https://www.sqlite.org\" target=\"_blank\">SQLite</a>",
    'PostgreSQL': "<a class=\"hover:text-white transition\" href=\"https://www.postgresql.org/\" target=\"_blank\">PostgreSQL</a>",
    'ubuntu': "<a class=\"hover:text-white transition\" href=\"https://ubuntu.com/\" target=\"_blank\">ubuntu</a>",
}


def index(request):

    referenzList: list = []

    for root, dirs, files in os.walk(os.path.join(REFERENZ_DIR, 'referenzen')):
        print(root, dirs, files)
        for file in files:
            if file[-5:] == '.json':
                with open(os.path.join(root, file), "r") as f:
                    data:dict = json.loads(f.read())
                    referenzList.append({
                        'title': data['title'],
                        'tags': data['tags'],
                        'short_description': data['short_description'],
                        'participant': data['participant'],
                        'time': data['time'],
                        'filename': file[:-5]
                    })

    return render(request, 'referenzen.html', {'referenzList': referenzList})



def referenz(request, name):

    if not os.path.exists(os.path.join(REFERENZ_DIR, 'referenzen', f'{name}.json')):
        raise Http404 


    with open(os.path.join(REFERENZ_DIR, 'referenzen', f'{name}.json'), "r") as f:
        context = json.loads(f.read())

    context.update({
        'filename': name,
        'participant_head': mark_safe(context['participant_head']),
        'case_study': render_to_string(name+'.html', {}),
    })

    save_stack_list:list = []
    for stack in context['stack_list']:
        save_stack_list.append(mark_safe(STACK_LINK.get(stack, stack)))
    context['stack_list'] = save_stack_list

    return render(request, 'referenz.html', context)
