package com.microservices.zone.dataset.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.httpClient.util.GitLinkRequestHelper;
import com.microservices.zone.dataset.domain.vo.ZoneDataSetProjectVo;
import com.microservices.zone.dataset.service.IZoneDataSetService;
import com.microservices.zone.project.domain.vo.ZoneProjectDataVo;
import com.microservices.zone.project.domain.vo.ZoneProjectSearchVo;
import com.microservices.zone.project.service.IZoneProjectService;
import com.microservices.zone.utils.ZoneGitLinkRequestUrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 特色专区资源聚合Service业务层处理
 *
 * @author otto
 * @date 2023-06-27
 */
@Slf4j
@Service
public class ZoneDataSetServiceImpl implements IZoneDataSetService {
    @Autowired
    IZoneProjectService projectService;
    @Autowired
    GitLinkRequestHelper gitLinkRequestHelper;

    @Override
    public List<ZoneDataSetProjectVo> getDataSetProjectList(Long zoneId, String search) {
        List<ZoneDataSetProjectVo> projectVoList = getZoneDataSetProjectVos(zoneId);
        if (projectVoList.isEmpty()) {
            return projectVoList;
        }
        // 获取存在数据集的项目id列表
        String projectIds = projectVoList.stream().map(x -> x.getRepositoryId().toString()).collect(Collectors.joining(","));
        // 根据项目id列表获取数据集列表
        JSONObject projectDataSets = gitLinkRequestHelper.doGet(
                ZoneGitLinkRequestUrl.PROJECT_DATASETS(projectIds)
        );
        List<ZoneDataSetProjectVo> projectDataSetList = new ArrayList<>();
        JSONArray projectDataSetArray = projectDataSets.getJSONArray("project_datasets");
        for (int i = 0; i < projectDataSetArray.size(); i++) {
            JSONObject projectDataSet = projectDataSetArray.getJSONObject(i);
            if (projectDataSet.containsKey("project")) {
                JSONObject project = projectDataSet.getJSONObject("project");
                if (project.containsKey("id")) {
                    ZoneDataSetProjectVo zoneDataSetProjectVo = projectVoList
                            .stream()
                            .filter(x -> x.getRepositoryId().equals(project.getLong("id")))
                            .findFirst().orElse(null);
                    if (zoneDataSetProjectVo != null) {
                        zoneDataSetProjectVo.setDataSetName(projectDataSet.getString("title"));
                        zoneDataSetProjectVo.setDataSetDescription(projectDataSet.getString("description"));
                        projectDataSetList.add(zoneDataSetProjectVo);
                    }
                }
            }
        }
        // 根据搜索条件进行筛选
        return projectDataSetList.stream().filter(x -> {
            if (StringUtils.isNotEmpty(search)) {
                return x.getDataSetName().toLowerCase().contains(search.toLowerCase());
            }
            return true;
        }).collect(Collectors.toList());
    }

    private List<ZoneDataSetProjectVo> getZoneDataSetProjectVos(Long zoneId) {
        ZoneProjectSearchVo searchVo = new ZoneProjectSearchVo();
        searchVo.setZoneId(zoneId);
        GenericsTableDataInfo<ZoneProjectDataVo> zoneProjectDataVoTable
                = projectService.selectZoneProjectList(searchVo, true);
        List<ZoneDataSetProjectVo> projectVoList = new ArrayList<>();
        if (zoneProjectDataVoTable != null) {
            List<ZoneProjectDataVo> projectDataVoList = zoneProjectDataVoTable.getRows();
            for (ZoneProjectDataVo projectDataVo : projectDataVoList) {
                // 获取有数据集的项目列表
                if (projectDataVo.getHasDataset()) {
                    ZoneDataSetProjectVo zoneDataSetProjectVo = new ZoneDataSetProjectVo(projectDataVo, gitLinkRequestHelper.gitLinkUrl);
                    projectVoList.add(zoneDataSetProjectVo);
                }
            }
        }
        return projectVoList;
    }

    @Override
    public JSONObject getDataSetDetail(Long projectId, Integer page, Integer limit) {
        ZoneProjectDataVo zoneProjectDataVo = projectService.selectZoneProjectDataById(projectId);
        ZoneDataSetProjectVo zoneDataSetProjectVo = new ZoneDataSetProjectVo(zoneProjectDataVo, gitLinkRequestHelper.gitLinkUrl);
        if (StringUtils.isNotEmpty(zoneDataSetProjectVo.getRepositoryFullName())) {
            JSONObject dataSetDetail = gitLinkRequestHelper.doGet(
                    ZoneGitLinkRequestUrl.DATASET(zoneDataSetProjectVo.getRepositoryFullName(), page, limit)
            );
            dataSetDetail.put("repository_url", zoneDataSetProjectVo.getRepositoryUrl());
            dataSetDetail.put("dataset_url", zoneDataSetProjectVo.getDataSetUrl());
            return dataSetDetail;
        }
        throw new ServiceException("获取项目聚合对应的数据集失败");
    }
}
