package cn.reghao.tnb.search.app.es;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryStringQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Highlight;
import co.elastic.clients.elasticsearch.core.search.HighlightField;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-03-12 11:16:48
 */
@Slf4j
@Service
public class QueryService<T> {
    private final ElasticsearchClient esClient;

    public QueryService(ElasticService elasticService) {
        this.esClient = elasticService.getElasticsearchClient();
    }

    public Page<T> queryWithHighlight(String index, String queryString, int pn, int ps, Class<T> clazz) {
        String highlightFieldName1 = "title";
        String highlightFieldName = "caseName";
        List<String> otherFiledNames = List.of("caseName", "cause", "parties");
        List<String> otherFiledNames1 = List.of();

        // 1.构建查询的对象
        QueryStringQuery stringQuery = new QueryStringQuery.Builder()
                // 查询的字段
                .fields(highlightFieldName, otherFiledNames.toArray(new String[0]))
                .query(queryString)
                .build();
        Query query = new Query.Builder()
                .queryString(stringQuery)
                .build();

        Query query1 = new Query.Builder()
                .bool(BoolQuery.of(b -> b
                        .must(m -> m.term(t -> t.field("vip").value(FieldValue.of(""))))
                        .must(m -> m.queryString(stringQuery))))
                .build();

        // 2.高亮显示
        HighlightField highlightField = new HighlightField.Builder()
                .matchedFields(highlightFieldName)
                .preTags("<span style=\"color:red\">")
                .postTags("</span>")
                .build();
        Highlight highlight = new Highlight.Builder()
                .fields(highlightFieldName, highlightField)
                .requireFieldMatch(false)
                .build();

        // 3.搜索请求
        int start = (pn-1)*ps;
        SearchRequest searchRequest = new SearchRequest.Builder()
                .index(index)
                .from(start)
                .size(ps)
                .query(query)
                .highlight(highlight)
                .build();
        try {
            SearchResponse<T> searchResponse = esClient.search(searchRequest, clazz);
            HitsMetadata<T> hitsMetadata = searchResponse.hits();
            long total = hitsMetadata.total().value();
            List<T> list = hitsMetadata.hits().stream().map(mapper -> {
                Map<String, List<String>> highlightMap = mapper.highlight();
                String highlightStr = "";
                if (!highlightMap.isEmpty()) {
                    highlightStr = mapper.highlight()
                            .get(highlightFieldName)
                            .get(0);
                }

                T t = mapper.source();
                try {
                    Field field = clazz.getDeclaredField(highlightFieldName);
                    field.setAccessible(true);

                    if (t == null) {
                        Object object = clazz.getDeclaredConstructors()[0].newInstance();
                        field.set(object, highlightStr);
                    } else {
                        field.set(t, highlightStr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return mapper.source();
            }).collect(Collectors.toList());

            //return list;
            PageRequest pageRequest = PageRequest.of(pn-1, ps);
            return new PageImpl<>(list, pageRequest, total);
        } catch (IOException e) {
            log.error("search By Query Highlight error", e);
        }

        return Page.empty();
    }

    public Page<T> queryWithHighlight(String index, Query query, int pn, int ps, Class<T> clazz) {
        String highlightFieldName = "title";
        // 1.构建查询的对象
        // 2.高亮显示
        HighlightField highlightField = new HighlightField.Builder()
                .matchedFields(highlightFieldName)
                .preTags("<span style=\"color:red\">")
                .postTags("</span>")
                .build();
        Highlight highlight = new Highlight.Builder()
                .fields(highlightFieldName, highlightField)
                .requireFieldMatch(false)
                .build();

        // 3.搜索请求
        int start = (pn-1)*ps;
        SearchRequest searchRequest = new SearchRequest.Builder()
                .index(index)
                .from(start)
                .size(ps)
                .query(query)
                .highlight(highlight)
                .build();
        try {
            SearchResponse<T> searchResponse = esClient.search(searchRequest, clazz);
            HitsMetadata<T> hitsMetadata = searchResponse.hits();
            long total = hitsMetadata.total().value();
            List<T> list = hitsMetadata.hits().stream().map(mapper -> {
                Map<String, List<String>> highlightMap = mapper.highlight();
                String highlightStr = "";
                if (!highlightMap.isEmpty()) {
                    highlightStr = mapper.highlight()
                            .get(highlightFieldName)
                            .get(0);
                }

                T t = mapper.source();
                try {
                    Field field = clazz.getDeclaredField(highlightFieldName);
                    field.setAccessible(true);

                    if (t == null) {
                        Object object = clazz.getDeclaredConstructors()[0].newInstance();
                        field.set(object, highlightStr);
                    } else {
                        field.set(t, highlightStr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return mapper.source();
            }).collect(Collectors.toList());

            //return list;
            PageRequest pageRequest = PageRequest.of(pn-1, ps);
            return new PageImpl<>(list, pageRequest, total);
        } catch (IOException e) {
            log.error("search By Query Highlight error", e);
        }

        return Page.empty();
    }

    public T queryById(Class<T> clazz, String index, String id) {
        Query query = TermQuery.of(t -> t
                .field("id").value(FieldValue.of(id))
        )._toQuery();

        SearchRequest searchRequest = SearchRequest.of(s -> s
                .index(index)
                .query(query)
        );

        try {
            SearchResponse<T> searchResponse = esClient.search(searchRequest, clazz);
            List<Hit<T>> hits = searchResponse.hits().hits();
            if (!hits.isEmpty()) {
                return hits.get(0).source();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
