package bg.car_wash.controllers.userController;

import bg.car_wash.configurations.SiteTitleNames;
import bg.car_wash.configurations.UserConfiguration;
import bg.car_wash.entities.User;
import bg.car_wash.entities.enumerations.UserType;
import bg.car_wash.models.bindingModels.UserLoginBindingModel;
import bg.car_wash.models.bindingModels.UserRegisterBindingModel;
import bg.car_wash.models.viewModels.UserSessionViewModel;
import bg.car_wash.services.UserService;
import bg.car_wash.utils.parser.interfaces.ModelParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private ModelParser modelParser;
//
//	@GetMapping("/status")
//	public String getUserStatusPage(HttpSession httpSession) {
//		if(httpSession.getAttribute("username") != null) {
//			return "user-status";
//		}
//
//		return "redirect:/login";
//	}

	@GetMapping("/login")
	public String getLoginPage(Model model, @ModelAttribute UserLoginBindingModel userLoginBindingModel) {
		model.addAttribute("pageTitle", SiteTitleNames.LOGIN_PAGE);
		return "user/user-login";
	}

	@PostMapping("/login")
	public String loginUser(@Valid @ModelAttribute UserLoginBindingModel userLoginBindingModel, BindingResult bindingResult, HttpSession httpSession) {
		if(bindingResult.hasErrors()) {
			return "user/user-login";
		}

		UserSessionViewModel user = this.userService.getUserByFullNameAndPassword(userLoginBindingModel);
		if(user != null) {
			httpSession.setAttribute("user-email", user.getEmail());
			httpSession.setAttribute("user-full-name", user.getFullName());

			return "redirect:/status";
		}

		//if not get user from database
		return "user/user-login";
	}

	@GetMapping("/register")
	public String getRegisterPage(Model model, @ModelAttribute UserRegisterBindingModel userRegisterBindingModel) {
		model.addAttribute("pageTitle", SiteTitleNames.REGISTER_PAGE);
		return "user/user-register";
	}

	@PostMapping("/register")
	public String registerUser(@Valid @ModelAttribute UserRegisterBindingModel userRegisterBindingModel, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			return "user/user-register";
		}

		User user = this.modelParser.convert(userRegisterBindingModel, User.class);
		List<User> usersFromDb = this.userService.findAllUsers();

		if(usersFromDb.isEmpty()) {
			user.setUserType(UserType.DIRECTOR);
			user.setRemuneration(new BigDecimal(UserConfiguration.DIRECTOR_REMUNERATION));
		} else if (usersFromDb.size() == 1) {
			user.setUserType(UserType.ADMIN);
			user.setRemuneration(new BigDecimal(UserConfiguration.ADMIN_REMUNERATION));
		} else {
			user.setUserType(UserType.WORKER);
			user.setRemuneration(new BigDecimal(UserConfiguration.WORKER_REMUNERATION));
		}

		this.userService.createUser(user);

		return "redirect:/user/login";
	}
}

@@ -266,24 +266,29 @@
 ndingModels.
+user.
 UserLoginBin
@@ -334,24 +334,29 @@
 ndingModels.
+user.
 UserRegister
@@ -406,16 +406,21 @@
 wModels.
+user.
 UserSess
@@ -979,16 +979,143 @@
 pping;%0A%0A
+import javax.servlet.http.Cookie;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A
 import j
@@ -1390,21 +1390,17 @@
 Parser;%0A
-//%0A//
+%0A
 %09@GetMap
@@ -1415,18 +1415,16 @@
 tatus%22)%0A
-//
 %09public 
@@ -1471,22 +1471,59 @@
 pSession
+, HttpServletRequest httpServletRequest
 ) %7B%0A
-//
 %09%09if(htt
@@ -1567,18 +1567,16 @@
 null) %7B%0A
-//
 %09%09%09retur
@@ -1596,19 +1596,68 @@
 s%22;%0A
-//
 %09%09%7D%0A
-//%0A//
+%0A%09%09Cookie%5B%5D cookies = httpServletRequest.getCookies();%0A%0A
 %09%09re
@@ -1680,18 +1680,16 @@
 login%22;%0A
-//
 %09%7D%0A%0A%09@Ge
@@ -2013,33 +2013,40 @@
 ginBindingModel,
- 
+%0A%09%09%09%09%09%09%09
 BindingResult bi
@@ -2057,17 +2057,24 @@
 gResult,
- 
+%0A%09%09%09%09%09%09%09
 HttpSess
@@ -2088,16 +2088,64 @@
 pSession
+,%0A%09%09%09%09%09%09%09HttpServletResponse httpServletResponse
 ) %7B%0A%09%09if
@@ -2318,17 +2318,17 @@
 if(user 
-!
+=
 = null) 
@@ -2330,16 +2330,49 @@
 ull) %7B%0A%09
+%09%09return %22user/user-login%22;%0A%09%09%7D%0A%0A
 %09%09httpSe
@@ -2422,17 +2422,16 @@
 ail());%0A
-%09
 %09%09httpSe
@@ -2492,103 +2492,113 @@
 ));%0A
-%0A
 %09%09
-%09return %22redirect:/status%22;%0A%09%09%7D%0A%0A%09%09//if not get user from database%0A%09%09return %22user/user-login
+httpServletResponse.addCookie(new Cookie(%22user%22, user.getFullName()));%0A%0A%09%09return %22redirect:/user/status
 %22;%0A%09

update controller