package cz.muni.fi.pa165.soccermanager.service;

import cz.muni.fi.pa165.soccermanager.dto.MatchDto;
import cz.muni.fi.pa165.soccermanager.dto.PlayerDto;
import cz.muni.fi.pa165.soccermanager.dto.UserDto;
import cz.muni.fi.pa165.soccermanager.entity.*;
import cz.muni.fi.pa165.soccermanager.mapper.MatchMapper;
import cz.muni.fi.pa165.soccermanager.mapper.PlayerMapper;
import cz.muni.fi.pa165.soccermanager.mapper.UserMapper;
import cz.muni.fi.pa165.soccermanager.repository.MatchRepository;
import cz.muni.fi.pa165.soccermanager.repository.UserRepository;
import cz.muni.fi.pa165.soccermanager.service.impl.UserServiceImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class UserServiceTest {
    @Mock
    private UserRepository userRepository;

    @Mock
    private UserMapper userMapper;

    @Mock
    private MatchMapper matchMapper;

    @Mock
    private PlayerMapper playerMapper;

    @Mock
    private PlayerService playerService;

    @Mock
    private MatchRepository matchRepository;

    @InjectMocks
    private UserServiceImpl userService;

    private User user;
    private UserDto userDto;

    @BeforeEach
    public void setup() {
        user = new User();
        user.setId(1L);
        Team team = new Team();
        team.setId(10L);
        League league = new League();
        league.setId(20L);
        team.setLeague(league);
        user.setTeam(team);

        userDto = new UserDto();
        userDto.setId(2L);
    }



    @Test
    public void getAllUsers(){
        when(userRepository.findAll(any(PageRequest.class))).thenReturn(Page.empty());
        assertEquals(userService.getAllUsers(10,10,"id"), Page.empty());
    }

    @ParameterizedTest
    @ValueSource(longs = {1L, 2L, 3L})
    public void findUserByIdTest(Long id) {
        when(userRepository.findById(any(Long.class))).thenReturn(Optional.of(user));
        when(userMapper.map(any(User.class))).thenReturn(userDto);

        assertTrue(userService.findUserById(id).isPresent());

        final var user2 = ArgumentCaptor.forClass(User.class);

        verify(userRepository, times(1)).findById(id);
        verify(userMapper, times(1)).map(user2.capture());

        assertEquals(user, user2.getValue());
    }

    @Test
    public void removeTeamTest() {
        Team team = new Team();
        team.setId(1L);
        team.setName("name1");
        team.setPoints(1);
        team.setGround("ground1");
        team.setLeague(mock(League.class));
        user.setTeam(team);

        when(userRepository.findById(any(Long.class))).thenReturn(Optional.of(user));

        userService.removeTeam(user.getId());

        verify(userRepository, times(1)).findById(user.getId());
    }

    @Test
    public void findUserMatchesTest() {
        Match match = new Match();
        List<Match> matches = new ArrayList<>();
        matches.add(match);

        MatchDto matchDto = matchMapper.map(match);
        List<MatchDto> matchDtoList = matches.stream().map(matchMapper::map).collect(Collectors.toList());

        when(userRepository.findById(any(Long.class))).thenReturn(Optional.of(user));
        when(matchRepository.findMatchesByUserIdAndLeagueId(user.getTeam().getLeague().getId(),
                user.getId())).thenReturn(matches);
        when(matchMapper.map(match)).thenReturn(matchDto);

        assertEquals(matchDtoList, userService.findUserMatches(user.getId()));
    }

    @Test
    public void findUserPlayersTest() {
        Player player = new Player();
        List<Player> players = new ArrayList<>();
        players.add(player);

        List<PlayerDto> playerDtoList = players.stream().map(playerMapper::map).collect(Collectors.toList());

        when(userRepository.findById(any(Long.class))).thenReturn(Optional.of(user));
        when(playerService.findPlayersByTeam(user.getTeam().getId())).thenReturn(playerDtoList);
        assertEquals(playerDtoList, userService.findUserPlayers(user.getId()));
    }
}
