package premium_store.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import premium_store.controller.DTO.ClientDTO;
import premium_store.controller.DTO.SupportClientDTO;
import premium_store.model.GameClient;
import premium_store.model.Tank;
import premium_store.repository.GameClientRepository;
import premium_store.repository.TankRepository;
import premium_store.service.exception.ClientNotFoundException;
import premium_store.util.validation.ValidatorUtil;

import java.util.List;
import java.util.Optional;

@Service
public class GameClientService {
    private final GameClientRepository gameClientRepository;
    private final TankRepository tankRepository;
    private final ValidatorUtil validatorUtil;

    public GameClientService(GameClientRepository gameClientRepository, TankRepository tankRepository, ValidatorUtil validatorUtil){
        this.gameClientRepository = gameClientRepository;
        this.tankRepository = tankRepository;
        this.validatorUtil = validatorUtil;
    }

    @Transactional
    public GameClient addClient(String newNickName, String newEmail, Integer newBallance) {
        final GameClient gameClient = new GameClient(newNickName, newEmail, newBallance);
        validatorUtil.validate(gameClient);

        return gameClientRepository.save(gameClient);
    }

    @Transactional(readOnly = true)
    public GameClient findClient(Long id) {
        final Optional<GameClient> client = gameClientRepository.findById(id);

        return client.orElseThrow(() -> new ClientNotFoundException(id));
    }

    @Transactional(readOnly = true)
    public List<GameClient> findAllClients() {
        return gameClientRepository.findAll();
    }

    @Transactional
    public GameClient updateClient(Long id, String newNickName, String newEmail, Integer newBalance, Tank newTank) {
        if (id <= 0) {
            throw new IllegalArgumentException("Invalid id");
        }

        final GameClient currentGameClient = findClient(id);

        if (StringUtils.hasText(newNickName)){
            currentGameClient.setNickName(newNickName);
        }

        if(StringUtils.hasText(newEmail)){
            currentGameClient.setEmail(newEmail);
        }

        if(newBalance != null){
            currentGameClient.setBalance(newBalance);
        }

        if(newTank != null){
            currentGameClient.setTanks(newTank);
        }

        return gameClientRepository.save(currentGameClient);
    }

    @Transactional
    public GameClient updateClient(SupportClientDTO clientDTO) {
        if (clientDTO.getId() <= 0) {
            throw new IllegalArgumentException("Invalid id");
        }

        final GameClient currentGameClient = findClient(clientDTO.getId());

        currentGameClient.setNickName(clientDTO.getNickName());
        currentGameClient.setEmail(clientDTO.getEmail());
        currentGameClient.setBalance(clientDTO.getBalance());

        if(clientDTO.getTankId() != null){
            currentGameClient.setTanks(tankRepository.getById(clientDTO.getTankId()));
        }

        return gameClientRepository.save(currentGameClient);
    }

    @Transactional
    public GameClient deleteClient(Long id) {
        final GameClient currentGameClient = findClient(id);
        gameClientRepository.delete(currentGameClient);

        return currentGameClient;
    }

    //прямой sql-запрос на удаление всех записей в таблице
    @Transactional
    public void deleteAllClients() {
        gameClientRepository.deleteAll();
    }
}
