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

import cz.muni.fi.pa165.soccermanager.dto.ChampionshipDto;
import cz.muni.fi.pa165.soccermanager.dto.request.CreateChampionshipRequest;
import cz.muni.fi.pa165.soccermanager.mapper.ChampionshipMapper;
import cz.muni.fi.pa165.soccermanager.service.impl.ChampionshipServiceImpl;
import cz.muni.fi.pa165.soccermanager.enums.Nation;
import cz.muni.fi.pa165.soccermanager.entity.Championship;
import cz.muni.fi.pa165.soccermanager.repository.ChampionshipRepository;
import java.util.Optional;
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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;

/**
 * @author Stanislav Koshchienko (uco: 506549)
 */
@ExtendWith(MockitoExtension.class)
public class ChampionshipServiceTest {

    @Mock
    private ChampionshipRepository championshipRepository;

    @Mock
    private ChampionshipMapper championshipMapper;

    @InjectMocks
    private ChampionshipServiceImpl championshipService;

    @Test
    public void createChampionshipTest() {
        final var championship = new CreateChampionshipRequest();
        when(championshipRepository.saveAndFlush(any(Championship.class))).thenReturn(new Championship());
        when(championshipMapper.map(any(CreateChampionshipRequest.class))).thenReturn(new Championship());
        championshipService.createChampionship(championship);
        verify(championshipMapper).map(championship);
        verify(championshipRepository, times(1)).saveAndFlush(any(Championship.class));
    }

    @ParameterizedTest
    @ValueSource(longs = {1L, 2L, 3L})
    public void getChampionshipByIdTest(Long id) {
        final var c = new Championship();
        final var dto = new ChampionshipDto();

        when(championshipRepository.findById(any(Long.class))).thenReturn(Optional.of(c));
        when(championshipMapper.map(any(Championship.class))).thenReturn(dto);

        assertTrue(championshipService.findChampionshipById(id).isPresent());

        final var c1 = ArgumentCaptor.forClass(Championship.class);

        verify(championshipRepository, times(1)).findById(id);
        verify(championshipMapper, times(1)).map(c1.capture());

        assertEquals(c, c1.getValue());
    }

    @Test
    public void updateTest() {
        Championship championship = new Championship();
        championship.setName("1");
        championship.setCountry(Nation.ALBANIA);
        championship.setId(1L);

        ChampionshipDto dto = new ChampionshipDto();
        dto.setCountry(Nation.CZECH_REPUBLIC);
        dto.setName("2");

        when(championshipRepository.findById(championship.getId())).thenReturn(Optional.of(championship));

        championshipService.update(1L,dto);
        verify(championshipMapper, times(1)).mapOnUpdate(championship,dto);
    }

    @Test
    public void removeTest() {
        Championship championship = new Championship();
        championship.setId(1L);
        championship.setName("name");
        when(championshipRepository.findById(any(Long.class))).thenReturn(Optional.of(championship));
        championshipService.remove(championship.getId());
        verify(championshipRepository, times(1)).delete(championship);
    }

    @Test
    public void getAllChampionships() {
        when(championshipRepository.findAll(any(PageRequest.class))).thenReturn(Page.empty());
        assertEquals(championshipService.getAllChampionships(10,10,"id"), Page.empty());
    }
}
