import time
from pathogenprofiler import get_summary
from pathogenprofiler import errlog, debug, unlist, dict_list2text
from .utils import get_drug_list

def lineagejson2text(x):
    textlines = []
    for l in x:
        textlines.append("%(lin)s\t%(family)s\t%(spoligotype)s\t%(rd)s\t%(frac)s" % l)
    return "\n".join(textlines)

def load_spoligotype_report(text_strings):
    return r"""
TBProfiler spoligotype report
=================

Summary
-------
ID%(sep)s%(id)s
Binary-spoligotype%(sep)s%(binary)s
Octal-spologotype%(sep)s%(octal)s

Spacers
-------
%(spacer_report)s
""" % text_strings

def load_text(text_strings):
    txt = r"""
TBProfiler report
=================

The following report has been generated by TBProfiler.

Summary
-------
ID%(sep)s%(id)s
Date%(sep)s%(date)s
Strain%(sep)s%(strain)s
Drug-resistance%(sep)s%(drtype)s
Median Depth%(sep)s%(med_dp)s

Lineage report
--------------
%(lineage_report)s
""" % text_strings

    if "spacers" in text_strings:
        txt += """
Spoligotype report
------------------
Binary%(sep)s%(binary)s
Octal%(sep)s%(octal)s
""" % text_strings

    txt += """
Resistance report
-----------------
%(dr_report)s

Resistance variants report
-----------------
%(dr_var_report)s

Other variants report
---------------------
%(other_var_report)s

Coverage report
---------------------
%(coverage_report)s

Missing positions report
---------------------
%(missing_report)s
""" % text_strings
    if "spacers" in text_strings:
        txt += """Spoligotype spacers
-------------------
%(spacers)s
""" % text_strings

    txt += """
Analysis pipeline specifications
--------------------------------
Pipeline version%(sep)s%(version)s
Database version%(sep)s%(db_version)s
%(pipeline)s

Citation
--------
Coll, F. et al. Rapid determination of anti-tuberculosis drug resistance from
whole-genome sequences. Genome Medicine 7, 51. 2015

Phelan, JE. et al. Integrating informatics tools and portable sequencing 
technology for rapid detection of resistance to anti-tuberculous drugs. 
Genome Medicine 11, 41. 2019
""" % text_strings
    return txt
def write_spoligotype_report(json_results,conf,outfile,sep="\t"):
    json_results["spacer_report"] = dict_list2text(json_results["spacers"],["name","count"],{"name":"Spacer","count":"Count"},sep=sep)
    if sep=="\t":
        json_results["sep"] = ": "
    else:
        json_results["sep"] = ","
    with open(outfile,"w") as O:
        O.write(load_spoligotype_report(json_results))


def stringify_annotations(annotation):
    annotations = []
    for ann in annotation:
        annotations.append("|".join([f'{key}={val}' for key,val in ann.items()]))
    return ";".join(annotations)

def write_text(json_results,conf,outfile,columns = None,reporting_af = 0.0,sep="\t",add_annotations=True):
    json_results = get_summary(json_results,conf,columns = columns,reporting_af=reporting_af)
    drug_list = get_drug_list(conf["bed"])

    json_results["drug_table"] = [[y for y in json_results["drug_table"] if y["Drug"].upper()==d.upper()][0] for d in conf["drugs"] if d in drug_list]
    for var in json_results["dr_variants"]:
        var["drug"] = "; ".join([d["drug"] for d in var["drugs"]])

    text_strings = {}
    text_strings["id"] = json_results["id"]
    text_strings["date"] = time.ctime()
    text_strings["strain"] = json_results["sublin"]
    text_strings["drtype"] = json_results["drtype"]
    text_strings["med_dp"] = json_results["qc"]["median_coverage"] if json_results['input_data_source'] in ('bam','fastq') else "NA"
    if "spoligotype" in json_results:
        text_strings["binary"] = json_results["spoligotype"]["binary"]
        text_strings["octal"] = json_results["spoligotype"]["octal"]
        text_strings["spacers"] = dict_list2text(json_results["spoligotype"]["spacers"],["name","count"])
    if add_annotations:
        for var in json_results["dr_variants"] + json_results["other_variants"]:
            if "annotation" in var:
                var["annotation_str"] = stringify_annotations(var["annotation"])
            else:
                var["annotation_str"] = ""

    text_strings["dr_report"] = dict_list2text(json_results["drug_table"],["Drug","Genotypic Resistance","Mutations"]+columns if columns else [],sep=sep)
    text_strings["lineage_report"] = dict_list2text(json_results["lineage"],["lin","frac","family","spoligotype","rd"],{"lin":"Lineage","frac":"Estimated fraction"},sep=sep)
    text_strings["dr_var_report"] = dict_list2text(json_results["dr_variants"],mappings={"genome_pos":"Genome Position","locus_tag":"Locus Tag","gene":"Gene","type":"Variant type","change": "Change","freq":"Estimated fraction","drugs.drug":"Drug"},sep=sep)
    text_strings["other_var_report"] = dict_list2text(json_results["other_variants"],mappings={"genome_pos":"Genome Position","locus_tag":"Locus Tag","gene":"Gene","type":"Variant type","change": "Change","freq":"Estimated fraction","annotation_str":"Annotation"},sep=sep)
    text_strings["coverage_report"] = dict_list2text(json_results["qc"]["gene_coverage"], ["gene","locus_tag","cutoff","fraction"],sep=sep) if "gene_coverage" in json_results["qc"] else "Not available"
    text_strings["missing_report"] = dict_list2text(json_results["qc"]["missing_positions"],["gene","locus_tag","position","variants","drugs"],sep=sep) if "missing_positions" in json_results["qc"] else "Not available"
    text_strings["pipeline"] = dict_list2text(json_results["pipeline"],["Analysis","Program"],sep=sep)
    text_strings["version"] = json_results["tbprofiler_version"]
    tmp = json_results["db_version"]
    text_strings["db_version"] = "%s_%s_%s_%s" % (tmp["name"],tmp["commit"],tmp["Author"],tmp["Date"])
    if sep=="\t":
        text_strings["sep"] = ": "
    else:
        text_strings["sep"] = ","
    with open(outfile,"w") as O:
        O.write(load_text(text_strings))