from fastapi import Request
from folkugat_web.model import playlists as playlists_model
from folkugat_web.model.pagines import Pages
from folkugat_web.services import playlists as playlists_service
from folkugat_web.templates import templates


def llistes_pagina(request: Request, logged_in: bool):
    playlists = playlists_service.get_all_playlists(logged_in=logged_in)
    return templates.TemplateResponse(
        "fragments/llistes/pagina.html",
        {
            "request": request,
            "logged_in": logged_in,
            "playlists": playlists,
            "Pages": Pages,
            "menu_selected_id": Pages.Llistes,
            "playlist_list_id": "playlist-list",
        }
    )


def llistes_editor_insert_row(request: Request):
    new_playlist = playlists_service.create_playlist(name=playlists_model.DEFAULT_PLAYLIST_NAME)
    return llistes_editor_row(request, new_playlist)


def llistes_editor_row(request: Request, playlist: playlists_model.Playlist):
    return templates.TemplateResponse(
        "fragments/llistes/playlist_entry.html",
        {
            "request": request,
            "playlist": playlist,
            "logged_in": True,
        }
    )


def llistes_editor_delete_row(playlist_id: int):
    playlists_service.delete_playlist(playlist_id)
    from fastapi.responses import HTMLResponse
    return HTMLResponse()
