# AUTOGENERATED! DO NOT EDIT! File to edit: notebooks/10_Download-demo-data.ipynb (unless otherwise specified).

__all__ = ['download']

# Cell

import wget
import re
import os
import sys
import ssl
import filehash

def _bar_progress(current, total, width=80):
    '''Create this bar_progress method which is invoked automatically from wget'''

    percent = (100 * current) // total
    progress_message = f'Downloading {total//1e6} MB: {percent}% '
    # Don't use print() as it will print in new line every time.
    sys.stdout.write("\r" + progress_message)
    sys.stdout.flush()

def download():
    '''Download demo files from cloud storage. '''

    # trying to fix SSL errors with wget.download()  below
    ssl._create_default_https_context = ssl._create_unverified_context

    cwd = os.getcwd()

    bucket_url = 'https://f002.backblazeb2.com/file/crono-maxrf-data-demo/'

    fname_list = ['14200215102021-blindTest2AgedDetail.HDF5',
                  'Ink-08-aged-VIS.png']

    #sha1 file hashes
    fhash_list = ['386bff97bed28d0302bf93abd00d591c4f45b44d',
                 '7d8c7f1c5f105f10c0ec06fdb9440cf3ea9c00aa']

    n_files = len(fname_list)


    print(f'Checking your current working directory: \n\'{cwd}\'\n')
    print(f'Please wait while synchronizing {n_files} files...')

    for i, [fname, fhash] in enumerate(zip(fname_list, fhash_list)):

        # do not download again if already present
        if fname in os.listdir():
            print(f'({i+1}) \'{fname}')

        # download
        else:
            url = bucket_url + fname
            fname = wget.download(url, bar=_bar_progress)
            is_ok = _check_filehash(fname, fhash)
            if is_ok:
                print(f' ({i+1}) \'{fname}')
            else:
                print('Warning! Downloaded file contains errors.')
                print('Automatically deleting downloaded file for safety reasons!')
                os.remove(fname)

    print('Ready!')


def _check_filehash(fname, fhash):
    '''Check file integrity.'''

    sha1 = filehash.FileHash('sha1')
    is_ok = sha1.hash_file(fname) == fhash

    return is_ok