from django.http import HttpResponse
from django.urls import path
from django.views.decorators.cache import cache_control

from tcommon.settings import STATIC_DIR


def get_static_serve(path: str, content_type: str):
    def x(request):
        return HttpResponse(
            open(STATIC_DIR / path, "rb").read(),
            content_type=content_type
        )

    x.__name__ = path
    return x

file_associations = {
    "js": "text/javascript",
    "css": "text/css",
    "ttf": "font/ttf"
}

urlpatterns = [path(i, cache_control(**{"max-age": 60 * 60 * 24 * 30})(get_static_serve(i, file_associations[i.split(".")[-1]]))) for i in [
    "css/base.css",
    "js/base.js",
    "js/theme.js",
    "font/DejaVuSans-Bold.ttf",
    "font/DejaVuSans-BoldItalic.ttf",
    "font/DejaVuSans-Italic.ttf",
    "font/DejaVuSans-Oblique.ttf",
    "font/DejaVuSans.ttf",
    "font/UbuntuMono-Bold.ttf",
    "font/UbuntuMono-BoldItalic.ttf",
    "font/UbuntuMono-Italic.ttf",
    "font/UbuntuMono-Regular.ttf"
]]
