/*
 * Copyright (c) 2025 Rostock University Library
 * Licensed under the MIT License. See LICENSE file in the project root.
 */
package de.uni.rostock.ub.wsrb.edition.webapp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import de.uni.rostock.ub.wsrb.edition.webapp.service.XSLTDocumentationService;

@Controller
public class SiteController {

    @Autowired
    XSLTDocumentationService docsService;

    @Autowired
    XSLTDocumentationService xsltDocumentationService;

    @GetMapping(path = "/", produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView showIndex() {
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("htmlBody", docsService.getHtmlBody().get("startseite"));
        mav.addObject("html4BiblPopups", xsltDocumentationService.getHtmlBiblPopups("./.."));
        return mav;
    }

    @GetMapping(path = "/site/{docid}", produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView showSite(@PathVariable(name = "docid") String docId) {
        ModelAndView mav = new ModelAndView("site");

        String pageId = docId.contains("__") ? docId.substring(0, docId.indexOf("__")) : docId;
        mav.addObject("pageId", pageId);
        mav.addObject("subpageId", docId);

        List<String> subPages = docsService.getSubPages().containsKey(pageId)
            ? docsService.getSubPages().get(pageId) : List.of();
        mav.addObject("subPages", subPages);

        mav.addObject("pageLabels", docsService.getPageLabels());
        mav.addObject("htmlBody", docsService.getHtmlBody().get(docId));
        mav.addObject("html4BiblPopups", xsltDocumentationService.getHtmlBiblPopups("./.."));

        return mav;
    }
}
