package com.libreecm.db.controllers;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AnonymousAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.libreecm.commonAPI.auxiliars.exceptions.AddUserOkException;
import com.libreecm.commonAPI.auxiliars.exceptions.AlreadyExistingUserException;
import com.libreecm.commonAPI.configuration.ComponentConfig;
import com.libreecm.db.auxiliars.Mapper;
import com.libreecm.db.consumers.UserConsumer;
import com.libreecm.db.jpa.users.LibreECMUser;
import com.libreecm.db.jpa.users.repositories.UserRepository;
  
@RestController
public class UserController extends BaseController {
 
    private  UserRepository userRepository;
     
    /**
     * A class that saves a Repository to the DDBB
     * @param user
     * @return
     * @throws AlreadyExistingUserException 
     * @throws AddUserOkException 
     */
    @RequestMapping(value="/user", method = RequestMethod.POST)
    public @ResponseBody LibreECMUser saveUser(@RequestBody LibreECMUser user) throws AlreadyExistingUserException, AddUserOkException {
    	//validateUser();
    	try{
			if (this.userRepository.findByUsername(user.getUsername()) != null){
				throw new AlreadyExistingUserException(user.getUsername());
    		} 
			//  	user.setPassword(passwordEncoder.encode(user.getPassword()));
				user = userRepository.saveAndFlush(user);
			} catch (Exception e){
			
			}
    	//As the ID is set before the save method, we need to update user
    	return this.userRepository.findByUsername(user.getUsername());
    	//throw new AddUserOkException(user.getUsername());
    }
    
    @Autowired
	void UserController(UserRepository userRepository) {
		this.userRepository = userRepository;
	}
       
    /**
     * A class that gets a User from the DDBB
     * @param id String id
     * @return 
     */
    @RequestMapping(value="/user", method = RequestMethod.GET)
    public @ResponseBody LibreECMUser getUser(@RequestParam String id) {
    	//validateUser();
    	
    	return this.userRepository.getOne(Long.parseLong(id)) ;
    }

    /**
     * A class that gets the current User from the client
     * @return 
     */
    @RequestMapping(value="/currentuser", method = RequestMethod.GET)
    public @ResponseBody LibreECMUser getCurrentUser() {
    	//validateUser();
    	UserConsumer consumer = new UserConsumer();
    	return Mapper.map(consumer.getCurrentUser(ComponentConfig.getConfig().getMainCurrentUserUrl()));
    }
    
}