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

import cz.muni.fi.pa165.soccermanager.dto.PlayerDto;
import cz.muni.fi.pa165.soccermanager.dto.request.CreatePlayerRequest;
import cz.muni.fi.pa165.soccermanager.dto.request.UpdatePlayerDto;
import cz.muni.fi.pa165.soccermanager.entity.Player;
import cz.muni.fi.pa165.soccermanager.mapper.PlayerMapper;
import cz.muni.fi.pa165.soccermanager.repository.PlayerRepository;
import cz.muni.fi.pa165.soccermanager.service.impl.PlayerServiceImpl;
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.time.LocalDate;
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 PlayerServiceTest {

    private final LocalDate DATE1 = LocalDate.of(1996, 1, 13);

    @Mock
    private PlayerRepository playerRepository;

    @Mock
    private PlayerMapper playerMapper;

    @InjectMocks
    private PlayerServiceImpl playerService;

    private Player player;
    private PlayerDto playerDto;

    @BeforeEach
    public void setUp() {
        player = new Player();
        player.setId(1L);
        player.setName("1");
        player.setBirthDate(DATE1);
        player.setHeight(180.0);
        player.setWeight(80.0);

        playerDto = new PlayerDto();
        playerDto.setId(2L);
        playerDto.setName("2");
        playerDto.setHeight(181);
        playerDto.setWeight(81.0);
    }

    @Test
    public void updateTest() {
        when(playerRepository.findById(player.getId())).thenReturn(Optional.of(player));
        UpdatePlayerDto updatePlayerDto = new UpdatePlayerDto();
        updatePlayerDto.setId(2L);
        updatePlayerDto.setName("2");
        updatePlayerDto.setHeight(181);
        updatePlayerDto.setWeight(81.0);
        playerService.update(1L,updatePlayerDto);
        verify(playerMapper, times(1)).mapOnUpdate(player, updatePlayerDto);

    }

    @Test
    public void removeTest() {
        when(playerRepository.findById(player.getId())).thenReturn(Optional.of(player));
        playerService.remove(player.getId());

        verify(playerRepository, times(1)).delete(player);
    }

    @ParameterizedTest
    @ValueSource(longs = {1L, 2L, 3L})
    public void findPlayerByIdTest(Long id) {
        when(playerRepository.findById(any(Long.class))).thenReturn(Optional.of(player));
        when(playerMapper.map(any(Player.class))).thenReturn(playerDto);

        assertTrue(playerService.findPlayerById(id).isPresent());

        final var p1 = ArgumentCaptor.forClass(Player.class);

        verify(playerRepository, times(1)).findById(id);
        verify(playerMapper, times(1)).map(p1.capture());

        assertEquals(player, p1.getValue());
    }

    @Test
    public void savePlayerTest() {
        final var playerRequest = new CreatePlayerRequest();

        when(playerRepository.saveAndFlush(any(Player.class))).thenReturn(new Player());
        when(playerMapper.map(any(CreatePlayerRequest.class))).thenReturn(new Player());
        playerService.savePlayer(playerRequest);

        verify(playerMapper).map(playerRequest);
        verify(playerRepository, times(1)).saveAndFlush(any(Player.class));
    }

    @Test
    public void getAllPlayersTest(){
        when(playerRepository.findAll(any(PageRequest.class))).thenReturn(Page.empty());
        assertEquals(playerService.getAllPlayers(10,10,"id"), Page.empty());
    }

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

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

        when(playerRepository.findByTeam(any(Long.class))).thenReturn(players);
        assertEquals(playerService.findPlayersByTeam(1L), playerDtoList);
    }

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

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

        when(playerRepository.findFreeAgents()).thenReturn(players);
        assertEquals(playerService.findFreeAgents(), playerDtoList);
    }
}
