package org.codiki.account;

import java.io.IOException;
import java.util.Optional;

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

import org.codiki.core.entities.dto.UserDTO;
import org.codiki.core.entities.persistence.User;
import org.codiki.core.repositories.UserRepository;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

// FIXME
@Ignore
@RunWith(MockitoJUnitRunner.class)
public class AccountServiceTest {
	
	@InjectMocks
	private AccountService service;
	
	@Mock
	private UserRepository repository;
	
	@Mock
	private HttpServletRequest request;
	
	@Mock
	private HttpServletResponse response;
	
	@Before
	public void before() {
		MockitoAnnotations.initMocks(AccountServiceTest.class);
	}
	
	private Optional<User> buildUser() {
		final User u = new User();
		u.setId(1L);
		u.setName("name");
		u.setEmail("test@test.te");
		return Optional.of(u);
	}
	
	@Test
	public void test_updateUser_ok_diff_name() throws IOException {
		final UserDTO userDto = new UserDTO();
		userDto.setName("anotherName");
		userDto.setEmail("test@test.te");
		
		final Optional<User> connectedUser = buildUser();
		final Optional<User> userFromDb = buildUser();
		
//		Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(connectedUser);
		Mockito.when(repository.findByEmail(Mockito.anyString())).thenReturn(userFromDb);
		
//		service.updateUser(userDto, request, response);
		Mockito.verify(response, Mockito.times(0)).sendError(Mockito.anyInt());
	}
	
	@Test
	public void test_updateUser_ok_diff_email() throws IOException {
		final UserDTO userDto = new UserDTO();
		userDto.setName("name");
		userDto.setEmail("autre_adresse@test.te");
		
		final Optional<User> connectedUser = buildUser();
		final Optional<User> userFromDb = buildUser();
		
//		Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(connectedUser);
		Mockito.when(repository.findByEmail(Mockito.anyString())).thenReturn(userFromDb);
		
//		service.updateUser(userDto, request, response);
		Mockito.verify(response, Mockito.times(0)).sendError(Mockito.anyInt());
	}

	@Test
	public void test_updateUser_ok_diff_all() throws IOException {
		final UserDTO userDto = new UserDTO();
		userDto.setName("anotherName");
		userDto.setEmail("another_adress@test.te");
		
		final Optional<User> connectedUser = buildUser();
		final Optional<User> userFromDb = buildUser();
		
//		Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(connectedUser);
		Mockito.when(repository.findByEmail(Mockito.anyString())).thenReturn(userFromDb);
		
//		service.updateUser(userDto, request, response);
		Mockito.verify(response, Mockito.times(0)).sendError(Mockito.anyInt());
	}
	
	@Test
	public void test_updateUser_error_email() throws IOException {
		final UserDTO userDto = new UserDTO();
		userDto.setName("name");
		userDto.setEmail("test@test.te");
		
		final Optional<User> connectedUser = buildUser();
		final Optional<User> userFromDb = buildUser();
		userFromDb.get().setEmail("another_adress@test.te");
		
//		Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(connectedUser);
		Mockito.when(repository.findByEmail(Mockito.anyString())).thenReturn(userFromDb);

//		service.updateUser(userDto, request, response);
		Mockito.verify(response, Mockito.times(1)).sendError(Mockito.anyInt());
	}
	
	@Test
	public void test_updateUser_error_disconnected() throws IOException {
//		Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(Optional.empty());
		
//		service.updateUser(null, request, response);
		Mockito.verify(response, Mockito.times(1)).sendError(Mockito.anyInt());
	}
}
