package eu.eudat.elastic.repository;

import eu.eudat.elastic.criteria.DatasetCriteria;
import eu.eudat.elastic.entities.Dataset;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * Created by ikalyvas on 7/5/2018.
 */
@Service("datasetRepository")
public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteria> {

    @Autowired
    public DatasetRepository(RestHighLevelClient client) {
        super(client);
    }

    @Override
    public Dataset createOrUpdate(Dataset entity) throws IOException {
        XContentBuilder builder = XContentFactory.jsonBuilder();
        IndexRequest request = new IndexRequest("datasets", "doc", entity.getId()).source(entity.toElasticEntity(builder));
        this.getClient().index(request);
        return entity;
    }

    @Override
    public Dataset findDocument(String id) throws IOException {
        GetRequest request = new GetRequest("datasets","doc",id);
        GetResponse response = this.getClient().get(request);
        return new Dataset().fromElasticEntity(response.getSourceAsMap());
    }

    @Override
    public List<Dataset> query(DatasetCriteria criteria) throws ExecutionException, InterruptedException, IOException {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .should(QueryBuilders.termsQuery("tags.name.keyword", criteria.getTags().stream().map(x -> x.getName()).collect(Collectors.toList())));
        searchSourceBuilder.query(boolQuery);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = this.getClient().search(searchRequest);
        return Arrays.stream(response.getHits().getHits()).map(x -> this.transformFromString(x.getSourceAsString(), Dataset.class)).collect(Collectors.toList());
    }

    @Override
    public boolean exists() throws IOException {
        GetIndexRequest request = new GetIndexRequest();
        request.indices("datasets");
        return this.getClient().indices().exists(request);
    }
}
