package org.academiadecodigo.enuminatti.helloworldspringmvc.controller;

import org.academiadecodigo.enuminatti.helloworldspringmvc.models.User;
import org.academiadecodigo.enuminatti.helloworldspringmvc.services.UserService;
import org.academiadecodigo.enuminatti.helloworldspringmvc.utils.Attributes;
import org.academiadecodigo.enuminatti.helloworldspringmvc.utils.Routes;
import org.academiadecodigo.enuminatti.helloworldspringmvc.utils.Views;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

@Controller
@SessionAttributes(Attributes.LOGGED_USER)
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET, value = {Routes.USERS, Routes.USERS + Routes.LIST})
    public String showUsers(Model model) {
        if (!model.containsAttribute(Attributes.LOGGED_USER)) {
            return Routes.redirect(Routes.ROOT);
        }

        model.addAttribute(Attributes.USER, new User());

        model.addAttribute(Attributes.USERS, userService.getUserList());
        model.addAttribute(Attributes.COUNT, userService.count());

        return Views.USERS;
    }

    @RequestMapping(method = RequestMethod.GET, value = Routes.USERS + Routes.EDIT + "/{username}")
    public String editUser(Model model, @PathVariable String username) {


        if (!model.containsAttribute(Attributes.LOGGED_USER)) {
            return Routes.redirect(Routes.ROOT);
        }

        model.addAttribute(Attributes.USERS, userService.getUserList());
        model.addAttribute(Attributes.COUNT, userService.count());
        model.addAttribute(Attributes.USER, userService.findByName(username));

        return Views.USERS;
    }

    @RequestMapping(method = RequestMethod.POST, value = Routes.USERS + Routes.EDIT)
    public String editUser(Model model, @ModelAttribute User user,
                               RedirectAttributes redirectAttributes) {

        if (!model.containsAttribute(Attributes.LOGGED_USER)) {
            return Routes.redirect(Routes.ROOT);
        }

        userService.editUser(user);
        redirectAttributes.addFlashAttribute(Attributes.EDIT_MESSAGE,
                "Edited user " + user.getUsername() + " successfully!");

        return Routes.redirect(Routes.USERS);
    }

    @RequestMapping(method = RequestMethod.POST, value = Routes.USERS + Routes.ADD)
    public String addUser(Model model, @ModelAttribute User user,
                          RedirectAttributes redirectAttributes) {

        if (!model.containsAttribute(Attributes.LOGGED_USER)) {
            return Routes.redirect(Routes.ROOT);
        }

        userService.addUser(user);
        redirectAttributes.addFlashAttribute(Attributes.ADD_MESSAGE,
                "Added user " + user.getUsername() + " successfully!");

        return Routes.redirect(Routes.USERS);
    }

    @RequestMapping(method = RequestMethod.GET, value = Routes.USERS + Routes.DELETE + "/{username}")
    public String deleteUser(Model model, @PathVariable String username) {

        if (!model.containsAttribute(Attributes.LOGGED_USER)) {
            return Routes.redirect(Routes.ROOT);
        }

        userService.remove(username);
        model.addAttribute(Attributes.DELETE_MESSAGE,
                "Removed user " + username + " successfully!");

        return Routes.redirect(Routes.USERS);
    }
}
