import requests
import json


def _get_analysis(
    entrez_dict, type_list, p_value, min_entities, max_entities, maxres, correct
):
    """ Call TOPPGENE API to detect functional enrichments of a gene list (https://toppgene.cchmc.org/API/enrich.)
    
    Parameters
    ----------
    entrez_dict: dict
        Dictionary { "Genes" : list of genes in Entrez IDs}.

    Returns
    -------
    response: requests.models.Response
        Response from "https://toppgene.cchmc.org/API/enrich" API call.
    
    """
    if type_list is None:
        type_list = [
            "GeneOntologyMolecularFunction",
            "GeneOntologyBiologicalProcess",
            "GeneOntologyCellularComponent",
            "HumanPheno",
            "MousePheno",
            "Domain",
            "Pathway",
            "Pubmed",
            "Interaction",
            "Cytoband",
            "TFBS",
            "GeneFamily",
            "Coexpression",
            "CoexpressionAtlas",
            "GeneFamily",
            "Computational",
            "MicroRNA",
            "Drug",
            "Disease",
        ]

    url = "https://toppgene.cchmc.org/API/enrich"
    headers = {"Content-Type": "text/json"}
    parameters = {}
    parameters["Categories"] = []
    for type_id in type_list:
        parameters["Categories"].append(
            {
                "Type": type_id,
                "Pvalue": p_value,
                "MinGenes": min_entities,
                "MaxGenes": max_entities,
                "MaxResults": maxres,
                "Correction": correct,
            }
        )
    data_all = {}
    data_all["Genes"] = entrez_dict["Genes"]
    data_all["Categories"] = parameters["Categories"]
    response = requests.post(url, headers=headers, data=json.dumps(data_all))
    if response.status_code == 200:
        print("Enrichment analysis success!")
    else:
        print(
            "Something went wrong during enrichment... Status code:",
            response.status_code,
        )
    return response