package io.khasang.wlogs.controller;

import io.khasang.wlogs.model.InsertDataTable;
import io.khasang.wlogs.model.LogManager;
import io.khasang.wlogs.model.LogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import io.khasang.wlogs.model.ViewDataTable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;

@Controller
public class AppController {
    @RequestMapping("/backup")
    public String backup(Model model) {
    model.addAttribute("backup", "Success");
    return "backup";
    }


    final public static Integer DEFAULT_LIMIT = 100;
    @Autowired
    private LogManager logManager;
    @Autowired
    private LogRepository logRepository;

    public void setLogManager(LogManager logManager) {
        this.logManager = logManager;
    }

    public void setLogRepository(LogRepository logRepository) {
        this.logRepository = logRepository;
    }

    @RequestMapping(value = "/", name = "home")
    public String index(HttpServletRequest request, Model model) {
        model.addAttribute("recordsTotal", logRepository.countAll());
        model.addAttribute("recordsPerPage", DEFAULT_LIMIT.toString());
        String offsetParam = request.getParameter("offset");
        Integer offset = 0;
        if (offsetParam != null) {
            model.addAttribute("currentOffset", offset = Integer.valueOf(offsetParam));
        } else {
            model.addAttribute("currentOffset", 0);
        }
        model.addAttribute("logs", logRepository.findAll(DEFAULT_LIMIT, offset));
        return "index";
    }

    @RequestMapping("/index")
    public String homePage() {
        return "forward:/";
    }

    @RequestMapping("/delete")
    public String deleteForm(Model model) {
        model.addAttribute("dateIntervalTypesMap", logManager.getDateIntervalMap());
        model.addAttribute("logRecordsTotal", logRepository.countAll());
        return "delete";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String deleteAction(HttpServletRequest request, RedirectAttributes redirectAttributes, Model model) {
        try {
            Integer affectedRows = logManager.delete(request.getParameterMap());
            redirectAttributes.addFlashAttribute("success", "Success! Count of deleted rows: " + affectedRows);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", e.getMessage());
        }
        return "redirect:delete";
    }

    @RequestMapping("/import")
    public String importLogs(Model model) {
        return "import";
    }

    @RequestMapping("/export")
    public String exportLogs(Model model) {
        return "export";
    }

    @RequestMapping("/logout")
    public String logout(Model model) {
        return "logout";
    }

    @RequestMapping("/admin")
    public String admin(Model model) {
        model.addAttribute("admin", "You are number 1!");
        return "admin";
    }

    @RequestMapping("/createtable")
    public String crateTable(Model model) {
        InsertDataTable sql = new InsertDataTable();
        model.addAttribute("createtable", sql.sqlInsertCheck());
        return "createtable";
    }

    @RequestMapping("/tableview")
    public String tableView(Model model) {
        ViewDataTable viewDataTable = new ViewDataTable();
        model.addAttribute("tableview", viewDataTable.outData());
        return "tableview";
    }
}
@@ -59,71 +59,27 @@
 ogs.
-model.InsertDataTable;%0Aimport io.khasang.wlogs.model.LogManager
+form.DeleteDataForm
 ;%0Aim
@@ -106,29 +106,17 @@
 s.model.
-LogRepository
+*
 ;%0Aimport
@@ -176,53 +176,8 @@
 ed;%0A
-import io.khasang.wlogs.model.ViewDataTable;%0A
 impo
@@ -222,16 +222,16 @@
 roller;%0A
-
 import o
@@ -259,16 +259,139 @@
 .Model;%0A
+import org.springframework.validation.annotation.Validated;%0Aimport org.springframework.web.bind.annotation.ModelAttribute;%0A
 import o
@@ -2050,60 +2050,205 @@
 te(%22
-dateIntervalTypesMap%22, logManager.getDateIntervalMap
+errorSources%22, this.logRepository.getErrorSources());%0A        model.addAttribute(%22errorLevels%22, this.logRepository.getErrorLevels());%0A        model.addAttribute(%22deleteDataForm%22, new DeleteDataForm
 ());
@@ -2456,35 +2456,113 @@
 ion(
-HttpServletRequest request,
+@ModelAttribute DeleteDataForm deleteDataForm, HttpServletRequest request,%0A                              
  Red
@@ -2683,33 +2683,22 @@
 ete(
-request.getParameterMap()
+deleteDataForm
 );%0A 

use new manager method to process user data