package com.alterdekim.game.service;

import com.alterdekim.game.component.ReferenceLoader;
import com.alterdekim.game.entity.OnlineStatus;
import com.alterdekim.game.entity.User;
import com.alterdekim.game.repository.OnlineStatusRepository;
import com.alterdekim.game.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

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

@Service
public class OnlineStatusService {

    @Autowired
    private OnlineStatusRepository repository;

    @Autowired
    private ReferenceLoader referenceLoader;

    public List<OnlineStatus> findByUserId(Integer userId) {
        return this.repository.findByUserId(userId);
    }

    public void setOnlineFor(Integer userId) {
        this.repository.save(new OnlineStatus(referenceLoader.getUserReference(userId), System.currentTimeMillis() / 1000L));
    }

    public Boolean isOnline(Integer userId) {
        return !this.findByUserId(userId).isEmpty();
    }

    public List<OnlineStatus> listPingsOlderThan(@Param("pingTime") Long pingTime) {
        return this.repository.listPingsOlderThan(pingTime);
    }

    public void delete(OnlineStatus s) {
        this.repository.delete(s);
    }
}
