package org.ros.chatto.controller;

import java.security.Principal;
import java.util.List;
import java.util.Optional;

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

import org.ros.chatto.dto.ActiveUserDTO;
import org.ros.chatto.model.ChatMessage;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.MessageCipher;
import org.ros.chatto.model.UserRole;
import org.ros.chatto.repository.ChatMessageRepository;
import org.ros.chatto.repository.MessageCipherRepository;
import org.ros.chatto.repository.RoleRepository;
import org.ros.chatto.repository.UserRepository;
import org.ros.chatto.repository.UserRepositoryCustom;
import org.ros.chatto.repository.UserRoleRepository;
import org.ros.chatto.service.UserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/demo")
@RequiredArgsConstructor
@Lazy
// @CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
public class DemoRestController {

	private final UserRepository userRepository;

	private final UserRepositoryCustom userRepositoryCustom;

	private final RoleRepository roleRepository;

	private final UserRoleRepository userRoleRepository;

	private final MessageCipherRepository messageCipherRepository;

	private final ChatMessageRepository chatMessageRepository;

	private final UserService userService;

	@GetMapping("/users")
	public List<ChatUser> getAllUsers() {
		return userRepository.findAll();
	}

	@GetMapping("/usernames")
	public List<String> getUserNames() {
		return userRepositoryCustom.getAllUserNames("hmm");
	}

	@GetMapping("/user_old")
	public Optional<ChatUser> getUser() {
		return userRepository.findByUserName("hmm");
	}

	@GetMapping("/user")
	public Optional<ChatUser> currentUserName(Principal principal) {
		Optional<ChatUser> user = userRepository
				.findByUserName(principal.getName());
		return user;
	}

	@GetMapping("/roles")
	public List<UserRole> getAllRoles() {
		return userRoleRepository.findAll();
	}

	@GetMapping("/ciphers")
	public List<MessageCipher> getAllCiphers() {
		return messageCipherRepository.findAll();
	}

	@GetMapping("/messages")
	public List<ChatMessage> getAllMessages() {
		return chatMessageRepository.findAll();
	}

	@GetMapping("/regular-users")
	public List<String> getAllRegularUsers() {
		return userRoleRepository.getAllRegularUser();
	}

	@PostMapping(value = "/post-message", consumes = { "application/json" })
	public ResponseEntity<MessageCipher> postMessage(
			@RequestBody MessageCipher messageCipher) {
		System.out.println("Message cipher = " + messageCipher);
		messageCipherRepository.save(messageCipher);
		return new ResponseEntity<MessageCipher>(HttpStatus.OK);
	}

	@GetMapping("/logout")
	public ModelAndView logoutPage() {
		ModelAndView modelAndView = new ModelAndView("restLogout");
		return modelAndView;
	}

	@RequestMapping(value = "perform_logout", method = RequestMethod.POST)
	public String performLogout(HttpServletRequest request,
			HttpServletResponse response) {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		if (auth != null) {
			new SecurityContextLogoutHandler().logout(request, response, auth);
		}
		return "redirect:/users";
	}

	@GetMapping(value = "/loggedUsers2")
	public List<ActiveUserDTO> getOtherActiveUsers(Principal principal) {
		return userService.getOtherActiveUsers(principal.getName());
	}
}
