package com.purui.prims.headless.chat.mapper;

import com.google.common.collect.Lists;
import com.purui.prims.common.pojo.Constants;
import com.purui.prims.headless.api.pojo.SchemaElement;
import com.purui.prims.headless.api.pojo.SchemaElementMatch;
import com.purui.prims.headless.api.pojo.SchemaElementType;
import com.purui.prims.headless.api.pojo.SchemaMapInfo;
import com.purui.prims.headless.api.pojo.request.QueryFilter;
import com.purui.prims.headless.api.pojo.request.QueryFilters;
import com.purui.prims.headless.chat.knowledge.builder.BaseWordBuilder;
import com.purui.prims.headless.chat.ChatQueryContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class QueryFilterMapper extends BaseMapper {

    private double similarity = 1.0;

    @Override
    public void doMap(ChatQueryContext queryContext) {
        Set<Long> dataSetIds = queryContext.getDataSetIds();
        if (CollectionUtils.isEmpty(dataSetIds)) {
            return;
        }
        SchemaMapInfo schemaMapInfo = queryContext.getMapInfo();
        clearOtherSchemaElementMatch(dataSetIds, schemaMapInfo);
        for (Long dataSetId : dataSetIds) {
            List<SchemaElementMatch> schemaElementMatches = schemaMapInfo.getMatchedElements(dataSetId);
            if (schemaElementMatches == null) {
                schemaElementMatches = Lists.newArrayList();
                schemaMapInfo.setMatchedElements(dataSetId, schemaElementMatches);
            }
            addValueSchemaElementMatch(dataSetId, queryContext, schemaElementMatches);
        }
    }

    private void clearOtherSchemaElementMatch(Set<Long> viewIds, SchemaMapInfo schemaMapInfo) {
        for (Map.Entry<Long, List<SchemaElementMatch>> entry : schemaMapInfo.getDataSetElementMatches().entrySet()) {
            if (!viewIds.contains(entry.getKey())) {
                entry.getValue().clear();
            }
        }
    }

    private void addValueSchemaElementMatch(Long dataSetId, ChatQueryContext queryContext,
            List<SchemaElementMatch> candidateElementMatches) {
        QueryFilters queryFilters = queryContext.getQueryFilters();
        if (queryFilters == null || CollectionUtils.isEmpty(queryFilters.getFilters())) {
            return;
        }
        for (QueryFilter filter : queryFilters.getFilters()) {
            if (checkExistSameValueSchemaElementMatch(filter, candidateElementMatches)) {
                continue;
            }
            SchemaElement element = SchemaElement.builder()
                    .id(filter.getElementID())
                    .name(String.valueOf(filter.getValue()))
                    .type(SchemaElementType.VALUE)
                    .bizName(filter.getBizName())
                    .dataSet(dataSetId)
                    .build();
            SchemaElementMatch schemaElementMatch = SchemaElementMatch.builder()
                    .element(element)
                    .frequency(BaseWordBuilder.DEFAULT_FREQUENCY)
                    .word(String.valueOf(filter.getValue()))
                    .similarity(similarity)
                    .detectWord(Constants.EMPTY)
                    .build();
            candidateElementMatches.add(schemaElementMatch);
        }
        queryContext.getMapInfo().setMatchedElements(dataSetId, candidateElementMatches);
    }

    private boolean checkExistSameValueSchemaElementMatch(QueryFilter queryFilter,
            List<SchemaElementMatch> schemaElementMatches) {
        List<SchemaElementMatch> valueSchemaElements = schemaElementMatches.stream().filter(schemaElementMatch ->
                        SchemaElementType.VALUE.equals(schemaElementMatch.getElement().getType()))
                .collect(Collectors.toList());
        for (SchemaElementMatch schemaElementMatch : valueSchemaElements) {
            if (schemaElementMatch.getElement().getId().equals(queryFilter.getElementID())
                    && schemaElementMatch.getWord().equals(String.valueOf(queryFilter.getValue()))) {
                return true;
            }
        }
        return false;
    }
}
