package com.imprimelibros.erp.home;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.imprimelibros.erp.configurationERP.VariableService;
import com.imprimelibros.erp.i18n.TranslationService;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;

import java.util.List;
import java.util.Locale;
import java.util.Map;

@Controller
public class HomeController {

    @Autowired
    private TranslationService translationService;
    @Autowired
    private VariableService variableService;

    @GetMapping("/")
    public String index(Model model, Authentication authentication, Locale locale) {

        boolean isAuthenticated = authentication != null && authentication.isAuthenticated()
                && !(authentication instanceof AnonymousAuthenticationToken);

        if (!isAuthenticated) {

            List<String> keys = List.of(
                    "presupuesto.plantilla-cubierta",
                    "presupuesto.plantilla-cubierta-text",
                    "presupuesto.impresion-cubierta",
                    "presupuesto.impresion-cubierta-help");

            Map<String, String> translations = translationService.getTranslations(locale, keys);
            model.addAttribute("languageBundle", translations);
            model.addAttribute("pod", variableService.getValorEntero("POD"));
            model.addAttribute("ancho_alto_min", variableService.getValorEntero("ancho_alto_min"));
            model.addAttribute("ancho_alto_max", variableService.getValorEntero("ancho_alto_max"));
        }
        else{
            // empty translations for authenticated users
            Map<String, String> translations = Map.of();
            model.addAttribute("languageBundle", translations);
        }
        return "imprimelibros/home";
    }
}
