package com.microservices.cms.utils;

import com.microservices.cms.project.domain.CmsProject;
import com.microservices.common.core.utils.html.EscapeUtil;
import com.microservices.common.httpClient.domain.GitLinkRequestUrl;

import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


public class CmsGitLinkRequestUrl extends GitLinkRequestUrl {

    /**
     * 创建项目
     */
    public static GitLinkRequestUrl CREATE_PROJECT() {
        return getAdminGitLinkRequestUrl("/api/projects.json");
    }

    /**
     * 更新项目
     */
    public static GitLinkRequestUrl UPDATE_PROJECT(CmsProject cmsProject) {
        return getGitLinkRequestUrl(String.format("/api/%s/%s.json", cmsProject.getUsername(), cmsProject.getIdentifier()));
    }

    /**
     * 创建Webhook
     */
    public static GitLinkRequestUrl CREATE_WEBHOOK(CmsProject cmsProject) {
        return getGitLinkRequestUrl(String.format("/api/%s/%s/webhooks.json", cmsProject.getUsername(), cmsProject.getIdentifier()));
    }

    public static GitLinkRequestUrl ALL_FILES(CmsProject cmsProject) {
        return getOpenGitLinkRequestUrl(
                String.format("/api/%s/%s/files", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }


    public static GitLinkRequestUrl DELETE_PROJECT(CmsProject cmsProject) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s.json", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }

    public static GitLinkRequestUrl GET_GITLINK_ORGANIZATIONS_INFO(String organizationName) {
        return getGitLinkRequestUrl(
                String.format("/api/organizations/%s.json", organizationName)
        );
    }

    public static GitLinkRequestUrl CREATE_FILE(CmsProject cmsProject) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/create_file.json", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }

    public static GitLinkRequestUrl CREATE_BRANCH(CmsProject cmsProject) {
        return getGitLinkRequestUrl(
                String.format("/api/v1/%s/%s/branches.json", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }

    public static GitLinkRequestUrl DELETE_BRANCH(CmsProject cmsProject, String branchName) {
        return getGitLinkRequestUrl(
                String.format("/api/v1/%s/%s/branches/%s.json", cmsProject.getUsername(), cmsProject.getIdentifier(), branchName)
        );
    }

    public static GitLinkRequestUrl CREATE_PULL_REQUEST(CmsProject cmsProject) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/pulls.json", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }

    public static GitLinkRequestUrl GET_PULL_REQUEST_DETAILS(CmsProject cmsProject, String giteaPrNumber) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/pulls/%s.json", cmsProject.getUsername(), cmsProject.getIdentifier(), giteaPrNumber)
        );
    }

    public static GitLinkRequestUrl MERGE_PULL_REQUEST(CmsProject cmsProject, String pullRequestId) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/pulls/%s/pr_merge.json", cmsProject.getUsername(), cmsProject.getIdentifier(), pullRequestId)
        );
    }

    public static GitLinkRequestUrl REFUSE_MERGE_PULL_REQUEST(CmsProject cmsProject, String giteaPrNumber) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/pulls/%s/refuse_merge.json", cmsProject.getUsername(), cmsProject.getIdentifier(), giteaPrNumber)
        );
    }

    public static GitLinkRequestUrl REMOVE_USER_FROM_PROJECT(CmsProject cmsProject) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/collaborators/remove.json", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }

    public static GitLinkRequestUrl ADD_USER_FROM_PROJECT(CmsProject cmsProject) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/collaborators.json", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }

    public static GitLinkRequestUrl CHANGE_ROLE(CmsProject cmsProject) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/collaborators/change_role.json", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }

    public static GitLinkRequestUrl GET_ENTRIES(CmsProject cmsProject, String branch) {
        return getOpenGitLinkRequestUrl(
                String.format("/api/%s/%s/entries.json?ref=%s", cmsProject.getUsername(), cmsProject.getIdentifier(), branch)
        );
    }

    public static GitLinkRequestUrl UPDATE_FILE(CmsProject cmsProject) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/update_file.json", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }

    public static GitLinkRequestUrl BATCH_UPDATE_FILE(CmsProject cmsProject) {
        return getGitLinkRequestUrl(
                String.format("/api/v1/%s/%s/contents/batch", cmsProject.getUsername(), cmsProject.getIdentifier())
        );
    }

    public static GitLinkRequestUrl DELETE_FILE(CmsProject cmsProject, String filepath, String sha, String branchName) {
        return getGitLinkRequestUrl(
                String.format("/api/%s/%s/delete_file.json?filepath=%s&sha=%s&branch=%s", cmsProject.getUsername(), cmsProject.getIdentifier(), URLEncoder.encode(filepath), sha, branchName)
        );
    }

    public static GitLinkRequestUrl GET_DIR_SUB_ENTRIES(CmsProject cmsProject, String filepath, String ref) {
        return getOpenGitLinkRequestUrl(
                String.format("/api/%s/%s/sub_entries.json?filepath=%s&ref=%s&type=%s", cmsProject.getUsername(), cmsProject.getIdentifier(), URLEncoder.encode(filepath), ref, CmsConstants.ENTRY_TYPE_DIR_KEY)
        );
    }

    public static GitLinkRequestUrl GET_FILE_SUB_ENTRIES(CmsProject cmsProject, String filepath, String ref) {
        return getOpenGitLinkRequestUrl(
                String.format("/api/%s/%s/sub_entries.json?filepath=%s&ref=%s&type=%s", cmsProject.getUsername(), cmsProject.getIdentifier(), URLEncoder.encode(filepath), ref, CmsConstants.ENTRY_TYPE_FILE_KEY)
        );
    }

    public static GitLinkRequestUrl CREATE_PROTECTED_BRANCHES(String username, String identifier, Map<String,String> params, List<String> pushWhitelistUsernames) throws URISyntaxException {
        String path = buildUri(String.format("/api/%s/%s/protected_branches.json", username, identifier), params);
        path = concatenateParametersAfterUrl(path, "push_whitelist_usernames[]", pushWhitelistUsernames);
        return getGitLinkRequestUrl(path);
    }

    public static GitLinkRequestUrl UPDATE_PROTECTED_BRANCHES(String username, String identifier, Map<String,String> params, List<String> pushWhitelistUsernames) throws URISyntaxException {
        String path = buildUri(String.format("/api/%s/%s/protected_branches/master.json", username, identifier), params);
        path = concatenateParametersAfterUrl(path, "push_whitelist_usernames[]", pushWhitelistUsernames);
        return getGitLinkRequestUrl(path);
    }

    public static GitLinkRequestUrl DELETE_PROTECTED_BRANCHES(String username, String identifier, Map<String,String> params, List<String> pushWhitelistUsernames) throws URISyntaxException {
        String path = buildUri(String.format("/api/%s/%s/protected_branches/master.json", username, identifier), params);
        path = concatenateParametersAfterUrl(path, "push_whitelist_usernames[]", pushWhitelistUsernames);
        return getGitLinkRequestUrl(path);
    }

    public static GitLinkRequestUrl ADD_ISSUE_JOURNAL_BY_ISSUE_INDEX(Long issueIndex, String username, String identifier) {
        return getGitLinkRequestUrl(String.format("/api/v1/%s/%s/issues/%d/journals", username, identifier, issueIndex));
    }

    public static GitLinkRequestUrl CREATE_ISSUE(String username, String identifier) {
        return getAdminGitLinkRequestUrl(String.format("/api/v1/%s/%s/issues", username, identifier));
    }

    public static GitLinkRequestUrl GET_ISSUE_BY_ISSUE_ID(String username, String identifier, Long issueId) {
        return getOpenGitLinkRequestUrl(String.format("/api/v1/%s/%s/issues/%d/show_by_id", username, identifier, issueId));
    }

    public static GitLinkRequestUrl GET_JOURNALS_BY_ISSUE_INDEX(String username, String identifier, Long issueIndex, Integer page, Integer limit) {
        return getOpenGitLinkRequestUrl(String.format("/api/v1/%s/%s/issues/%d/journals?category=comment&page=%d&limit=%d", username, identifier, issueIndex, page, limit));
    }

    public static GitLinkRequestUrl DELETE_AND_UPDATE_JOURNAL_BY_ISSUE_INDEX(String username, String identifier, Long issueIndex, Long journalId) {
        return getGitLinkRequestUrl(String.format("/api/v1/%s/%s/issues/%d/journals/%d", username, identifier, issueIndex, journalId));
    }

    public static GitLinkRequestUrl DELETE_JOURNAL_BY_ADMIN_AND_ISSUE_INDEX(String username, String identifier, Long issueIndex, Long journalId) {
        return getAdminGitLinkRequestUrl(String.format("/api/v1/%s/%s/issues/%d/journals/%d", username, identifier, issueIndex, journalId));
    }

    public static GitLinkRequestUrl DELETE_ISSUE_BY_ADMIN_AND_ISSUE_INDEX(String username, String identifier, Long issueIndex) {
        return getAdminGitLinkRequestUrl(String.format("/api/v1/%s/%s/issues/%d", username, identifier, issueIndex));
    }

    public static String ZONE_DOC_URL(String gitlinkUrl, String zoneKey, Long docId) {
        return EscapeUtil.removeExtraSlashOfUrl(String.format("%s/zone/%s/newdetail/%d", gitlinkUrl, zoneKey, docId));
    }
}
