import csv
import logging

from dipper.models.Reference import Reference
from dipper.models.assoc.G2PAssoc import G2PAssoc
from dipper.sources.Source import Source
from dipper.sources.ZFIN import ZFIN
from dipper.models.Model import Model


LOG = logging.getLogger(__name__)
# note: currently no log issued
ZPCURATION = 'http://purl.obolibrary.org/obo/zp/'

class ZFINSlim(Source):
    """
    zfin mgi model only containing Gene to phenotype associations
    Using the file here: https://zfin.org/downloads/phenoGeneCleanData_fish.txt
    """
    files = {
        'g2p_clean': {
            'file': 'phenoGeneCleanData_fish.txt',
            'url': 'https://zfin.org/downloads/phenoGeneCleanData_fish.txt',
            # https://zfin.org/downloads#  header Documentation is burried in UI crap
        },
        'zpmap': {  # this is a zp mapping file generated by Nico (matches ZIN.py)
            'file': 'id_map_zfin.tsv',
            'url': ZPCURATION + '/id_map_zfin.tsv',
            'columns': [
                'iri',
                'id',
            ]},
    }

    def __init__(self,
                 graph_type,
                 are_bnodes_skolemized,
                 data_release_version=None):
        super().__init__(
            graph_type=graph_type,
            are_bnodes_skized=are_bnodes_skolemized,
            data_release_version=data_release_version,
            name='zfinslim',
            ingest_title='Simplified ZFIN',
            ingest_url='https://zfin.org/',
            ingest_logo="source-zfin.png",
            license_url=None,
            data_rights='http://zfin.org/warranty.html',
            # file_handle=None
        )
        self.dataset.set_citation(
            'https://wiki.zfin.org/display/general/ZFIN+db+information')

    def fetch(self, is_dl_forced=False):
        self.get_files(is_dl_forced)

    def parse(self, limit=None):
        zfin_parser = ZFIN(self.graph_type, self.are_bnodes_skized)
        model = Model(self.graph)
        zp_file = '/'.join((self.rawdir, self.files['zpmap']['file']))
        g2p_file = '/'.join((self.rawdir, self.files['g2p_clean']['file']))
        zfin_parser.zp_map = zfin_parser._load_zp_mappings('zpmap')  # zfin.files key

        with open(g2p_file, 'r', encoding="utf8") as csvfile:
            filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
            for row in filereader:
                (internal_id,
                 symbol,
                 gene_id,
                 subterm1_id,
                 subterm1_label,
                 pc_rel_id,
                 pc_rel_label,
                 superterm1_id,
                 superterm1_label,
                 quality_id,
                 quality_name,
                 modifier,
                 subterm2_id,
                 subterm2_label,
                 pc_rel2_id,
                 pc_rel2_label,
                 superterm2_id,
                 superterm2_label,
                 fish_id,
                 fish_label,
                 start_stage,
                 end_stage,
                 environment,
                 pub_id,
                 figure_id
                ) = row

                if modifier != "abnormal":
                    LOG.warning("skipping phenotype with modifier != abnormal: " + modifier)
                    continue

                zp_id = zfin_parser._map_octuple_to_phenotype(subterm1_id,
                                                              pc_rel_id,
                                                              superterm1_id,
                                                              quality_id,
                                                              subterm2_id,
                                                              pc_rel2_id,
                                                              superterm2_id,
                                                              modifier)

                gene_curie = "ZFIN:{0}".format(gene_id)
                model.makeLeader(gene_curie)
                pub_curie = "ZFIN:{0}".format(pub_id)
                if zp_id:
                    assoc = G2PAssoc(self.graph, self.name, gene_curie, zp_id)
                    if pub_id:
                        reference = Reference(self.graph, pub_curie,
                                              self.globaltt['document'])
                        reference.addRefToGraph()
                        assoc.add_source(pub_curie)

                    assoc.add_evidence(
                        self.globaltt['experimental phenotypic evidence'])
                    assoc.add_association_to_graph()

@@ -718,81 +718,1186 @@
     
-# https://zfin.org/downloads#  header Documentation is burried in UI crap
+'columns': %5B%0A                'ID',%0A                'Gene Symbol',%0A                'Gene ID',%0A                'Affected Structure or Process 1 subterm ID',%0A                'Affected Structure or Process 1 subterm Name',%0A                'Post-composed Relationship ID',%0A                'Post-composed Relationship Name',%0A                'Affected Structure or Process 1 superterm ID',%0A                'Affected Structure or Process 1 superterm Name',%0A                'Phenotype Keyword ID',%0A                'Phenotype Keyword Name',%0A                'Phenotype Tag',%0A                'Affected Structure or Process 2 subterm ID',%0A                'Affected Structure or Process 2 subterm name',%0A                'Post-composed Relationship (rel) ID',%0A                'Post-composed Relationship (rel) Name',%0A                'Affected Structure or Process 2 superterm ID',%0A                'Affected Structure or Process 2 superterm name',%0A                'Fish ID',%0A                'Fish Display Name',%0A                'Start Stage ID',%0A                'End Stage ID',%0A                'Fish Environment ID',%0A                'Publication ID',%0A                'Figure ID',%0A            %5D,
 %0A   
@@ -3088,24 +3088,25 @@
 .graph)%0A
+%0A
         
 zp_file 
@@ -3101,232 +3101,285 @@
     
-zp_file = '/'.join((self.rawdir, self.files%5B'
+src_key = 'zpmap'%0A        zfin_parser.zp_map = zfin_parser._load_
 zp
+_
 map
-'%5D%5B'file'%5D))%0A        g2p_file = '/'.join((self.rawdir, self.files%5B'g2p_clean'%5D%5B'file'%5D))%0A        zfin_parser.zp_map = zfin_parser._load_zp_mappings('zpmap')  # zfin.files key
+pings(src_key)  # zfin.files%5Bkey%5D%0A%0A        src_key = 'g2p_clean'%0A        raw = '/'.join((self.rawdir, self.files%5B'g2p_clean'%5D%5B'file'%5D))%0A        LOG.info(%22Processing clean Geno to Pheno from file: %25s%22, raw)%0A
 %0A%0A  
@@ -3398,16 +3398,11 @@
 pen(
-g2p_file
+raw
 , 'r

add columns