package com.exodiashop.shop.Controller;

import com.exodiashop.shop.Model.User;
import com.exodiashop.shop.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView showLogin(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView("login");

        return mav;
    }

    @RequestMapping(value = "/loginProcess", method = RequestMethod.POST)
    public ModelAndView loginProcess(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = null;

        User user = userService.validateUser(request.getParameter("username"),request.getParameter("password"));

        if (null != user) {
            mav = new ModelAndView("../redirections/to_dashboard");
            mav.addObject("loggedUser", user);
            mav.addObject("loggedUsername", user.getUsername());
        } else {
            mav = new ModelAndView("../redirections/to_login");
            mav.addObject("message", "Username or Password is wrong!!");
        }

        return mav;
    }

}
