package be.naaturel.boardmateapi.repository.mappings;

import be.naaturel.boardmateapi.common.models.Game;
import be.naaturel.boardmateapi.repository.dtos.GameDto;
import be.naaturel.boardmateapi.repository.dtos.PlayerDto;
import be.naaturel.boardmateapi.repository.dtos.TimeControlDto;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GameMapper {

    public static GameDto toDto(Game game){
        GameDto dto = new GameDto();

        String id = game.getId();
        dto.setId(id);

        PlayerDto white =  PlayerMapper.toDto(game.getWhiteName());
        PlayerDto black =  PlayerMapper.toDto(game.getBlackName());
        Map<String, PlayerDto> players = new HashMap<>();
        players.put("white", white);
        players.put("black", black);
        dto.setPlayers(players);

        TimeControlDto timeControlDto = TimeControlMapper.toDto(game.getTimeValue(), game.getIncrement());
        dto.setTimeControl(timeControlDto);

        List<String> moves =
                game.getMoves()
                .stream()
                .toList();

        dto.setMoves(moves);
        return dto;
    }

    public static Game toModel(GameDto dto){
        String id = dto.getId();
        String whiteName = dto.getPlayers().get("white").getName();
        String blackName = dto.getPlayers().get("black").getName();
        int timeValue = dto.getTimeControl().getValue();
        int increment = dto.getTimeControl().getIncrement();
        List<String> moves = dto.getMoves();
        return new Game(id, whiteName, blackName, timeValue, increment, moves);
    }
}
