from easy_ko.load_genomes import SharedData

from easy_ko.CRISPR_components import component
from easy_ko.primer_interactions import *

import pandas as pd

# for calculating entropy
from collections import Counter
from skbio.diversity.alpha import shannon


def get_5prime_upstream_info(data: SharedData, L, seqname, orf_start, orf_end, strand):
    # L is the length upstream from the 5' end of the gene
    seq = data.genome_assembly_dict.get(seqname)
    if seq is None:
        return None
    output_start, output_end = orf_start - L, orf_start # look at start and end
    output_seq = seq[output_start:output_end]
    output = {
        'seq': output_seq,
        'start': output_start,
        'end': output_end
    }

    return output
    
def get_3prime_downstream_info(data: SharedData, L, seqname, orf_start, orf_end, strand):
    # L is the length upstream from the 5' end of the gene
    seq = data.genome_assembly_dict.get(seqname)
    if seq is None:
        return None
    output_start, output_end = orf_end, orf_end + L
    output_seq = seq[output_start:output_end].reverse_complement()
    output = {
        'seq': output_seq,
        'start': output_start,
        'end': output_end
    }

    return output


def make_homology_primers(data: SharedData, gene_id, gene_name, A_allele):
    L =70
    up5prime = get_5prime_upstream_info(data, L, A_allele['seqname'], A_allele['start'], A_allele['end'], A_allele['strand'])
    down3prime = get_3prime_downstream_info(data, L, A_allele['seqname'], A_allele['start'], A_allele['end'], A_allele['strand'])

    fwd_SATFLP = str(up5prime['seq']) + component(data,'F_SATFLP')
    rev_SATFLP = str(down3prime['seq']) + component(data, 'R_SATFLP')

    if len(fwd_SATFLP) > 90:
        fwd_SATFLP = fwd_SATFLP[-90:]
    
    if len(rev_SATFLP) > 90:
        rev_SATFLP = rev_SATFLP[-90:]

    notes_F_SATFLP, notes_R_SATFLP = '',''

    # issue: need to be less than 60bp
    """
    if self_interaction(fwd_SATFLP):
        notes_F_SATFLP += 'primer not viable due to self interaction. '

    if self_interaction(rev_SATFLP):
        notes_R_SATFLP += 'primer not viable due to self interaction. '
    
    SATFLP_heterodimer_dg = heterodimer_dg(fwd_SATFLP, rev_SATFLP)

    notes_R_SATFLP += f'dg with F_SATFLP primer: {str(SATFLP_heterodimer_dg)}'

    notes_F_SATFLP += f'dg with R_SATFLP primer: {str(SATFLP_heterodimer_dg)}'
    """

    """
    df_SATFLP_primers = pd.DataFrame({
        'name': [f'F_SATFLP_{gene_name}',f'R_SAT_{gene_name}'],
        'seq': [fwd_SATFLP,rev_SATFLP],
        'notes': ['',''],
        'pair with': ['',''],
    })

    """

    output_F_SATFLP, output_R_SATFLP = ({
        'systematic name': gene_id,
        'gene name': gene_name,
        'part name': f'F_SATFLP_{gene_name}',
        'seq': fwd_SATFLP,
        'notes': notes_F_SATFLP,
        'pair with': '',
        'start': up5prime['start'],
        'end': up5prime['end']
    },
    {
        'systematic name': gene_id,
        'gene name': gene_name,
        'part name': f'R_SATFLP_{gene_name}',
        'seq': rev_SATFLP,
        'notes': notes_R_SATFLP,
        'pair with': '',
        'start': down3prime['start'],
        'end': down3prime['end']
    })

    
    return output_F_SATFLP, output_R_SATFLP