import sys
import os
import glob
import re
import gzip
import click
import array
import loompy
import numpy as np
import random
import string
import csv
from collections import defaultdict
import logging
from typing import *
import velocyto as vcy
from ._run import _run

logging.basicConfig(stream=sys.stdout, format='%(asctime)s - %(levelname)s - %(message)s', level=logging.DEBUG)


def id_generator(size: int=6, chars: str=string.ascii_uppercase + string.digits) -> str:
    return ''.join(random.choice(chars) for _ in range(size))


@click.command(short_help="Runs the velocity analysis outputing a loom file")
@click.argument("bamfile",
                type=click.Path(exists=True,
                                file_okay=True,
                                dir_okay=False,
                                readable=True,
                                resolve_path=True))
@click.argument("ivlfile",
                type=click.Path(exists=True,
                                file_okay=True,
                                dir_okay=False,
                                readable=True,
                                resolve_path=True))
@click.option("--bcfile", "-b",
              help="""Valid barcodes file, to filter the bam. If --bcfile is not specified all the cell barcodes will be incuded.
              Cell barcodes should be specified in the bcfile as the `CB` tag of each read""",
              default=None,
              show_default=True,
              type=click.Path(resolve_path=True,
                              file_okay=True,
                              dir_okay=False,
                              readable=True))
@click.option("--outputfolder", "-o",
              help="Output folder, does not need to exist",
              default=None,
              type=click.Path(exists=False))
@click.option("--sampleid", "-d",
              help="The sample name that will be used to retrieve informations from metadatatable",
              default=None,
              type=click.Path(exists=False))
@click.option("--metadatatable", "-s",
              help="Table containing metadata of the various samples (csv fortmated, [row:samples, col:entry])",
              default=None,
              type=click.Path(resolve_path=True,
                              file_okay=True,
                              dir_okay=False,
                              readable=True))
@click.option("--repmask", "-m",
              help=".gtf file containing intrvals sorted by chromosome, strand, position\n(e.g. generated by running `velocyto extract_repeats mm10_rmsk.gtf`)",
              default=None,
              type=click.Path(resolve_path=True,
                              file_okay=True,
                              dir_okay=False,
                              readable=True))
@click.option("--debug", "-d",
              help="debug mode. It will generate .sam files of individual reads (not molecules) that are identified as exons, introns, ambiguous and chimeras",
              default=False,
              is_flag=True)
def run(bamfile: str, ivlfile: str,
        bcfile: str, outputfolder: str,
        sampleid: str, metadatatable: str,
        repmask: str, debug: bool,
        additional_ca: dict={}) -> None:
    """Runs the velocity analysis outputing a loom file

    BAMFILE bam file with sorted reads

    IVLFILE text file generated by velocyto extract_intervals
    """
    return _run(bamfile, ivlfile, bcfile, outputfolder,
                sampleid, metadatatable, repmask, debug,
                additional_ca)
