/*
 * 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.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

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

@Controller
public class TranskriptionController {

    @Autowired
    XSLTTranskriptionService transkriptionService;

    @Value("${wsrb.transkription.ids}")
    private List<String> transkriptionList;

    @GetMapping(path = "/transkription")
    ModelAndView transcription(
        /**id may by a transcription id (e.g. "wsrb_tr1-ws1") or a text entity id (e.g. "wsrb_tr1-ws1_te007") */
        @RequestParam(name = "id", defaultValue = "wsrb_tr1-ws1") String id,
        @RequestParam(name = "te", defaultValue = "") String te) {
        ModelAndView mav = new ModelAndView("transkription");

        mav.addObject("transkriptionList", transkriptionList);
        String theId = id.contains("__") ? id.substring(0, id.indexOf("__")) : id;
        String theAnchor = id.contains("__") ? id : "";

        mav.addObject("currentTranskription", theId);
        mav.addObject("currentAnchor", theAnchor);
        mav.addObject("transkriptionHTML", transkriptionService.createTranskription(theId, te, theAnchor));

        return mav;
    }
}
