package eu.eudat.data.dao.entities;

import eu.eudat.data.dao.DatabaseAccess;
import eu.eudat.data.dao.databaselayer.service.DatabaseService;
import eu.eudat.data.entities.DoiFunder;
import eu.eudat.queryable.QueryableList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@Component("DoiFunderDao")
public class DoiFunderDaoImpl extends DatabaseAccess<DoiFunder> implements DoiFunderDao {

	@Autowired
	public DoiFunderDaoImpl(DatabaseService<DoiFunder> databaseService) {
		super(databaseService);
	}

	@Override
	public DoiFunder findFunderByName(String name) {
		return this.asQueryable().toList().stream().filter(doiFunder -> name.contains(doiFunder.getName()) || doiFunder.getName().contains(name)).findFirst().orElse(null);
	}

	@Override
	public DoiFunder createOrUpdate(DoiFunder item) {
		return this.getDatabaseService().createOrUpdate(item, DoiFunder.class);
	}

	@Override
	public CompletableFuture<DoiFunder> createOrUpdateAsync(DoiFunder item) {
		return CompletableFuture.supplyAsync(() -> this.createOrUpdate(item));
	}

	@Override
	public DoiFunder find(UUID id) {
		return this.getDatabaseService().getQueryable(DoiFunder.class).where(((builder, root) -> builder.equal(root.get("id"), id))).getSingle();
	}

	@Override
	public DoiFunder find(UUID id, String hint) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void delete(DoiFunder item) {
		this.getDatabaseService().delete(item);
	}

	@Override
	public QueryableList<DoiFunder> asQueryable() {
		return this.getDatabaseService().getQueryable(DoiFunder.class);
	}
}
