package com.microservices.dms.utils;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.httpClient.domain.GitLinkRequestUrl;
import com.microservices.common.httpClient.util.GitLinkRequestHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DmsRequestHelper extends GitLinkRequestHelper {

    private static final Logger logger = LoggerFactory.getLogger(DmsRequestHelper.class);

    /**
     * 循环分页获取Gitlink列表接口所有数据
     *
     * @return 所有数据列表
     */
    public JSONArray getAllDataByPage(String url, String listKey) {
        JSONArray jsonArray = new JSONArray();
        boolean isAllData = false;
        int pageNum = 1;
        // 循环分页获取gitlink组织下所有成员
        while (!isAllData) {
            JSONObject result = doGet(GET_LIST_BY_PAGES(url, pageNum, 50)
            );
            jsonArray.addAll(result.getJSONArray(listKey));
            long total = result.getLong("search_count");
            if (total <= jsonArray.size()) {
                isAllData = true;
            } else {
                pageNum++;
            }
        }
        return jsonArray;
    }

    public <T> List<T> getAllDataByPage(String url, String listKey, Class<T> tClass) {
        List<T> allList = new ArrayList<>();
        boolean isAllData = false;
        int pageNum = 1;
        // 循环分页获取gitlink组织下所有成员
        while (!isAllData) {
            JSONObject result = doGet(GET_LIST_BY_PAGES(url, pageNum, 50)
            );
            allList.addAll(result.getList(listKey, tClass));
            long total = result.getLong("search_count");
            if (total <= allList.size()) {
                isAllData = true;
            } else {
                pageNum++;
            }
        }
        return allList;
    }

    /**
     * 循环分页获取Gitlink列表接口所有数据
     *
     * @return 所有数据列表
     */
    public JSONArray getAllDataByPage(String url, String listKey,String countKey) {
        JSONArray jsonArray = new JSONArray();
        boolean isAllData = false;
        int pageNum = 1;
        while (!isAllData) {
            JSONObject result = doGet(GET_LIST_BY_PAGES(url, pageNum, 50)
            );
            jsonArray.addAll(result.getJSONArray(listKey));
            long total = result.getLong(countKey);
            if (total <= jsonArray.size()) {
                isAllData = true;
            } else {
                pageNum++;
            }
        }
        return jsonArray;
    }


    public static GitLinkRequestUrl GET_LIST_BY_PAGES(String url, Integer page, Integer limit) {
        String p = url.contains("?")? "%s&page=%s&limit=%s": "%s?page=%s&limit=%s";
        return getAdminGitLinkRequestUrl(String.format(p, url, page, limit));
    }


    protected static GitLinkRequestUrl getAdminGitLinkRequestUrl(String path) {
        GitLinkRequestUrl gitLinkRequestUrl = new GitLinkRequestUrl();
        gitLinkRequestUrl.setIsOpen(false);
        gitLinkRequestUrl.setIsAdmin(true);
        gitLinkRequestUrl.setPath(path);
        return gitLinkRequestUrl;
    }
}
