package com.printhub.printhub.controller.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.printhub.printhub.model.Breadcrum;
import com.printhub.printhub.service.configuration.PrinterService;
import com.printhub.printhub.utils.datatables.DataTableRequest;

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

@Controller
@RequestMapping("/configuration/printers")
public class PrintersController {

    @Autowired
    private MessageSource messageSource;
    @Autowired
    private PrinterService printerService;

    @GetMapping
    public String index(Model model, Locale locale) {

        Breadcrum breadcrumb = new Breadcrum();
        breadcrumb.addItem(
                messageSource.getMessage("t-printers", null, locale),
                "t-printers",
                "/configuration/printers");
        breadcrumb.addItem(
                messageSource.getMessage("t-printers-list", null, locale),
                "t-printers-list",
                null);
        model.addAttribute("breadcrumb", breadcrumb.getBreadcrumb());

        model.addAttribute("keyTitle", "t-printers");
        model.addAttribute("title", messageSource.getMessage("t-printers", null, locale));


        return "printhub/configuration/printers";
    }

    @GetMapping("/datatable")
    @ResponseBody
    public Map<String, Object> datatable(@RequestParam Map<String, String> params) {
        DataTableRequest dt = new DataTableRequest(params);
        return printerService.listDataTable(dt); 
    }
}
