package org.academiadecodigo.enuminatti.helloworldspringmvc.controller;

import org.academiadecodigo.enuminatti.helloworldspringmvc.models.Bootcamp;
import org.academiadecodigo.enuminatti.helloworldspringmvc.services.BootcampService;
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 BootcampController {

    @Autowired
    private BootcampService bootcampService;

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

        model.addAttribute(Attributes.BOOTCAMP, new Bootcamp());
        model.addAttribute(Attributes.BOOTCAMPS, bootcampService.getBootcampList());
        model.addAttribute(Attributes.COUNT, bootcampService.count());

        return Views.BOOTCAMPS;
    }

    @RequestMapping(method = RequestMethod.GET, value = Routes.BOOTCAMPS + Routes.EDIT + "/{id}")
    public String editBootcamp(Model model, @PathVariable Integer id) {


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

        model.addAttribute(Attributes.BOOTCAMPS, bootcampService.getBootcampList());
        model.addAttribute(Attributes.COUNT, bootcampService.count());
        model.addAttribute(Attributes.BOOTCAMP, bootcampService.getById(id));

        return Views.BOOTCAMPS;
    }

    @RequestMapping(method = RequestMethod.POST, value = Routes.BOOTCAMPS + Routes.EDIT)
    public String editBootcamp(Model model, @ModelAttribute Bootcamp bootcamp,
                               RedirectAttributes redirectAttributes) {

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

        bootcampService.editBootcamp(bootcamp);
        redirectAttributes.addFlashAttribute(Attributes.EDIT_MESSAGE,
                "Edited bootcamp " + bootcamp.getId() + " successfully!");

        return Routes.redirect(Routes.BOOTCAMPS);
    }

    @RequestMapping(method = RequestMethod.POST, value = Routes.BOOTCAMPS + Routes.ADD)
    public String addBootcamp(Model model, @ModelAttribute Bootcamp bootcamp,
                               RedirectAttributes redirectAttributes) {

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

        bootcampService.addBootcamp(bootcamp);
        redirectAttributes.addFlashAttribute(Attributes.ADD_MESSAGE,
                "Added bootcamp " + bootcamp.getId() + " successfully!");

        return Routes.redirect(Routes.BOOTCAMPS);
    }

    @RequestMapping(method = RequestMethod.GET, value = Routes.BOOTCAMPS + Routes.DELETE + "/{id}")
    public String deleteBootcamp(Model model, @PathVariable Integer id) {

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

        bootcampService.remove(id);
        model.addAttribute(Attributes.DELETE_MESSAGE,
                "Removed bootcamp " + id + " successfully!");

        return Routes.redirect(Routes.BOOTCAMPS);
    }

}
