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

import cz.muni.fi.pa165.soccermanager.dto.request.UpdateTeamDto;
import cz.muni.fi.pa165.soccermanager.factory.TeamFactory;
import cz.muni.fi.pa165.soccermanager.entity.Team;
import org.junit.jupiter.api.Assertions;
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.NullSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
 * @author Petr Balazy (uco: 527362)
 */
@ExtendWith(MockitoExtension.class)
public class TeamMapperTest {

    @Mock
    private TeamFactory teamFactory;

    @InjectMocks
    private TeamMapperImpl teamMapper;

    @ParameterizedTest
    @NullSource
    public void mapNullTest(Team team) {
        assertNull(teamMapper.map(team));
    }

    @ParameterizedTest
    @NullSource
    public void mapNullTest(List<Team> teams) {
        assertNull(teamMapper.map(teams));
    }

    @ParameterizedTest
    @NullSource
    public void mapNullTest(UpdateTeamDto updateTeamDTO) {
        assertNull(teamMapper.map(updateTeamDTO));
    }

    @Test
    public void mapDTOHappy() {
        final var dto = new UpdateTeamDto();
        dto.setId(1L);
        dto.setName("MFK Karvina");
        dto.setGround("Mestsky stadion Karvina");

        when(teamFactory.prepare(any(UpdateTeamDto.class))).thenReturn(new Team());

        final var res = teamMapper.map(dto);

        Assertions.assertEquals(res.getName(), "MFK Karvina");
        Assertions.assertEquals(res.getGround(), "Mestsky stadion Karvina");
    }

    @Test
    public void mapSourceHappy() {
        final var source = new Team();
        source.setId(1L);
        source.setName("MFK Karvina");
        source.setGround("Mestsky stadion Karvina");

        final var res = teamMapper.map(source);

        Assertions.assertEquals(res.getId(), 1L);
        Assertions.assertEquals(res.getName(), "MFK Karvina");
        Assertions.assertEquals(res.getGround(), "Mestsky stadion Karvina");
    }

    @Test
    public void mapSourceListHappy() {
        final var team = new Team();
        team.setId(1L);
        team.setName("MFK Karvina");
        team.setGround("Mestsky stadion Karvina");

        final List<Team> list = new ArrayList<>();
        list.add(team);

        final var res = teamMapper.map(list);

        assertEquals(res.size(), 1);
        Assertions.assertEquals(res.get(0).getId(), 1L);
        Assertions.assertEquals(res.get(0).getName(), "MFK Karvina");
        Assertions.assertEquals(res.get(0).getGround(), "Mestsky stadion Karvina");
    }

    @Test
    public void updateMapTest() {
        final var team = new Team();
        team.setId(1L);
        team.setName("MFK Karvina");
        team.setGround("Mestsky stadion Karvina");

        final var dto = new UpdateTeamDto();
        dto.setId(1L);
        dto.setName("FC Karvina");
        dto.setGround("Regionalni stadion Karvina");

        teamMapper.mapOnUpdate(team, dto);

        assertEquals(team.getId(), dto.getId());
        assertEquals(team.getName(), dto.getName());
        assertEquals(team.getGround(), dto.getGround());
    }
}
