package br.ufpr.tcc.gregs.resources;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.CrossOrigin;
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.RestController;

import br.ufpr.tcc.gregs.configurations.security.JwtTokenUtil;
import br.ufpr.tcc.gregs.dto.requests.LoginRequest;
import br.ufpr.tcc.gregs.dto.responses.JwtResponse;
import br.ufpr.tcc.gregs.dto.responses.UsuarioResponse;
import br.ufpr.tcc.gregs.models.Retorno;
import br.ufpr.tcc.gregs.models.Usuario;
import br.ufpr.tcc.gregs.service.UsuarioService;

@RestController
@CrossOrigin
public class LoginResource {

	@Autowired
	private AuthenticationManager authenticationManager;

	@Autowired
	private JwtTokenUtil jwtTokenUtil;

	@Autowired
	private UsuarioService usuarioService;

	@PostMapping(value = "/login")
	public ResponseEntity<Retorno> createAuthenticationToken(@RequestBody LoginRequest authenticationRequest)
			throws Exception {
		//authenticate(authenticationRequest.getEmail(), authenticationRequest.getPassword());

		final UserDetails userDetails = usuarioService.loadUserByUsername(authenticationRequest.getEmail());
		final String token = jwtTokenUtil.generateToken(userDetails);

		return new ResponseEntity<>(new Retorno("Login Efetuado com Sucesso", new JwtResponse(token)), HttpStatus.OK);
	}

	// Authentication injection works by magic
	@GetMapping(value = "/login")
	public ResponseEntity<?> usuarioLogado(Authentication authentication) {
		Usuario usuario = usuarioService.findByEmail(authentication.getName());
		return new ResponseEntity<>(new Retorno("Login Efetuado com Sucesso", new UsuarioResponse(usuario)),
				HttpStatus.OK);
	}
	
	
	private void authenticate(String email, String password) throws Exception {
		try {
			authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(email, password));
		} catch (DisabledException e) {
			throw new Exception("USER_DISABLED", e);
		} catch (BadCredentialsException e) {
			throw new Exception("INVALID_CREDENTIALS", e);
		}
	}
}
