package com.microservices.zone.search.dto;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.zone.project.domain.vo.ZoneProjectDataVo;
import com.microservices.zone.search.utils.SearchUtils;
import lombok.Data;

import java.util.List;

@Data
public class GitlinkSearchVo {

    private String id;

    private Long instanceId;

    private String title;

    private String content;

    private String userName;

    private Long type;

    private Long star;

    private Long fork;

    private Long watch;

    private String createTime;

    private String updateTime;

    private List<String> topics;

    private Long praisesCount;

    private Long forkedCount;

    private Long watchersCount;

    private String url;

    private Double score;

    public GlobalSearchResult toGlobalSearchResult(ZoneProjectDataVo zoneProjectDataVo, String topic) {
        GlobalSearchResult globalSearchResult = new GlobalSearchResult();
        globalSearchResult.setTitle(replaceHighlightGitlinkSearch(this.title));
        globalSearchResult.setContent(replaceHighlightGitlinkSearch(this.content));
        globalSearchResult.setScore(this.score);
        globalSearchResult.setId(zoneProjectDataVo.getId());
        globalSearchResult.setType("zone_project");
        globalSearchResult.setDeptId(zoneProjectDataVo.getZoneId());
        globalSearchResult.setCreateTime(globalSearchResult.getCreateTime());
        globalSearchResult.setZoneId(zoneProjectDataVo.getZoneId());
        JSONObject extendData = zoneProjectDataVo.getProjectProperties();
        String topics = extendData.getString("topics");
        if (StringUtils.isNotEmpty(topics)) {
            extendData.put("topics", SearchUtils.highlightKeyword(topics, topic));
        }
        globalSearchResult.setExtendData(extendData);
        return globalSearchResult;
    }

    private String replaceHighlightGitlinkSearch(String text) {
        if (StringUtils.isNotEmpty(text)) {
            text = text.replace("<span class=\"highlightByGitlinkSearch\">", "<em>").replace("</span>", "</em>");
        }
        return text;
    }
}
