r"""
Download datasets
"""

import time
import requests
import os
from keras.models import load_model
import scanpy as sc

def data_downloader(url,path,title):
    r"""datasets downloader
    
    Arguments
    ---------
    url
        the download url of datasets
    path
        the save path of datasets
    title
        the name of datasets
    
    """
    if os.path.isfile(path):
        print("......Loading dataset from {}".format(path))
        return path
    else:
        print("......Downloading dataset save to {}".format(path))
        
    dirname, _ = os.path.split(path)
    try:
        if not os.path.isdir(dirname):
            print("......Creating directory {}".format(dirname))
            os.makedirs(dirname, exist_ok=True)
    except OSError as e:
        print("......Unable to create directory {}. Reason {}".format(dirname,e))
    
    
    start = time.time()
    size = 0
    res = requests.get(url, stream=True)

    chunk_size = 102400
    content_size = int(res.headers["content-length"]) 
    if res.status_code == 200:
        print('......[%s Size of file]: %0.2f MB' % (title, content_size/chunk_size/10.24))
        with open(path, 'wb') as f:
            for data in res.iter_content(chunk_size=chunk_size):
                f.write(data)
                size += len(data) 
                print('\r'+ '......[Downloader]: %s%.2f%%' % ('>'*int(size*50/content_size), float(size/content_size*100)), end='')
        end = time.time()
        print('\n' + ".......Finish！%s.2f s" % (end - start))
    
    return path


def BoneMarrow():
    r""" Download the dataset 'ZhangZemin_CD8+':
        BCL of CD8+ T cells,GEO:GSE156728

    Returns
    ---------
    adata
        the anndata of ZhangZemin_CD8+
    """
    datasets_name='BoneMarrow'
    _datasets={
        'BoneMarrow':'https://figshare.com/ndownloader/files/42158367',
    }
    model_path = data_downloader(url=_datasets[datasets_name],path='datasets/{}.h5ad'.format(datasets_name),title=datasets_name)
    adata = sc.read_h5ad(model_path)
    return adata


def Zebrafish():
    r""" Download the dataset 'Zebrafish':
        the axial mesoderm lineage of the zebrafish embryo data generated by [Farrell et al., 2018]

    Returns
    ---------
    adata
        the anndata of Zebrafish
    """
    datasets_name='Zebrafish'
    _datasets={
        'Zebrafish':'https://figshare.com/ndownloader/files/42158376',
    }
    model_path = data_downloader(url=_datasets[datasets_name],path='datasets/{}.h5ad'.format(datasets_name),title=datasets_name)
    adata = sc.read_h5ad(model_path)
    return adata

def Pancreas():
    r""" Download the dataset 'Pancreas':
        Pancreatic endocrinogenesis.
        Data from Bastidas-Ponce et al. (2019).
        Pancreatic epithelial and Ngn3-Venus fusion (NVF) cells during secondary transition with transcriptome profiles sampled from embryonic day 15.5.
        Endocrine cells are derived from endocrine progenitors located in the pancreatic epithelium. Endocrine commitment terminates in four major fates: glucagon- producing α-cells, insulin-producing β-cells, somatostatin-producing δ-cells and ghrelin-producing ε-cells.

    Returns
    ---------
    adata
        the anndata of Pancreas
    """
    datasets_name='Pancreas'
    _datasets={
        'Pancreas':'https://figshare.com/ndownloader/files/42158373',
    }
    model_path = data_downloader(url=_datasets[datasets_name],path='datasets/{}.h5ad'.format(datasets_name),title=datasets_name)
    adata = sc.read_h5ad(model_path)
    return adata    