from typing import List, Dict
import os

from six import iteritems
from flask import send_file, abort
import connexion

from jass.models.phenotype import Phenotype, get_available_phenotypes
from jass.models.project import Project, create_project
from jass.config import config

PHENOTYPES = get_available_phenotypes(
    os.path.join(config["DATA_DIR"], "initTable.hdf5")
)  # FIXME part of the config


def phenotypes_get():
    """
    phenotypes_get
    Gets the list of available phenotypes 

    :rtype: List[Phenotype]
    """
    return PHENOTYPES


def projects_post(phenotypeID):
    """
    projects_post
    Create a new project from a selection of phenotypes 
    :param phenotypeID: IDs of the phenotypes selected for the project
    :type phenotypeID: List[str]

    :rtype: str
    """
    return create_project(phenotypeID, PHENOTYPES)


def projects_project_id_summary_statistics(projectID):
    """
    projects_project_id_summary_statistics
    Retrieve project summary statistics
    """
    return Project(id=projectID).get_project_summary_statistics()


def projects_project_id_genome_get(projectID, threshold=None):
    """
    projects_project_id_genome_get
    Retrieve genome data for a given project 
    :param projectID: project ID
    :type projectID: str

    :rtype: str
    """
    return Project(id=projectID).get_project_genomedata()


def projects_project_id_global_manhattan_plot_get(projectID):
    try:
        return send_file(
            Project(id=projectID).get_global_manhattan_plot_path(), mimetype="image/png"
        )
    except FileNotFoundError:
        status = Project(id=projectID).status
        if status == Project.DOES_NOT_EXIST:
            abort(404)
        elif status["global_manhattan"] == Project.CREATING:
            return (
                "Plot is not ready yet",
                202,
                {"Content-Type": "text/plain; charset=utf-8"},
            )
        else:
            abort(500)


def projects_project_id_quadrant_plot_get(projectID):
    try:
        return send_file(
            Project(id=projectID).get_quadrant_plot_path(), mimetype="image/png"
        )
    except FileNotFoundError:
        status = Project(id=projectID).status
        if status == Project.DOES_NOT_EXIST:
            abort(404)
        elif status["quadrant_plot_status"] == Project.CREATING:
            return (
                "Plot is not ready yet",
                202,
                {"Content-Type": "text/plain; charset=utf-8"},
            )
        else:
            abort(500)


def projects_project_id_genome_full_get(projectID):
    try:
        return send_file(
            Project(id=projectID).get_csv_path(), mimetype="text/csv"
        )
    except FileNotFoundError:
        status = Project(id=projectID).status
        if status == Project.DOES_NOT_EXIST:
            abort(404)
        elif status["worktable"] == Project.CREATING:
            return (
                "CSV is not ready yet",
                202,
                {"Content-Type": "text/plain; charset=utf-8"},
            )
        else:
            abort(500)


def projects_project_id_local_manhattan_data_get(projectID, chromosome, region):
    return Project(id=projectID).get_project_local_manhattan_data(chromosome, region)


def projects_project_id_local_heatmap_data_get(projectID, chromosome, region):
    return Project(id=projectID).get_project_local_heatmap_data(chromosome, region)


def projects_project_idget(projectID):
    """
    projects_project_idget
    Retrieve a project definition 
    :param projectID: project ID
    :type projectID: str

    :rtype: Phenotype
    """
    return Project(id=projectID)
