# -*- coding: UTF-8 -*-
# Python
"""
Contains functions to facilitate code workflow, directory creation, Notebook readability, logfile creation, etc.

23-08-2023
@author: jeremy auclair
"""

from typing import Tuple  # to declare argument types


def format_duration(timedelta: float) -> None:
    """
    Print formatted timedelta in human readable format
    (days, hours, minutes, seconds, microseconds, milliseconds, nanoseconds).

    Arguments
    =========
    
    timedelta: ``float``
        time value in seconds to format

    Returns
    =======
    
    ``None``
    """

    if timedelta < 0.9e-6:
        print(round(timedelta * 1e9, 1), 'ns')
    elif timedelta < 0.9e-3:
        print(round(timedelta * 1e6, 1), 'µs')
    elif timedelta < 0.9:
        print(round(timedelta * 1e3, 1), 'ms')
    elif timedelta < 60:
        print(round(timedelta, 1), 's')
    elif timedelta < 3.6e3:
        print(round(timedelta // 60), 'm', round(timedelta % 60, 1),  's')
    elif timedelta < 24*3.6e3:
        print(round((timedelta / 3.6e3) // 1), 'h', round((timedelta / 3.6e3) % 1 * 60 // 1), 'm', round((timedelta / 3.6e3) % 1 * 60 % 1 * 60, 1), 's')
    elif timedelta < 48*3.6e3:
        print(round((timedelta / (24 * 3.6e3)) // 1), 'day,', round(((timedelta / (24 * 3.6e3)) % 1 * 24) // 1), 'h,',
              round((timedelta / (24*3.6e3)) % 1 * 24 % 1 * 60 // 1), 'm,',  round((timedelta / (24 * 3.6e3)) % 1 * 24 % 1 * 60 % 1 * 60, 1), 's')
    else:
        print(round((timedelta / (24 * 3.6e3)) // 1), 'days,', round(((timedelta / (24 * 3.6e3)) % 1 * 24) // 1), 'h,',
              round((timedelta / (24 * 3.6e3)) % 1 * 24 % 1 * 60 // 1), 'm,',  round((timedelta / (24 * 3.6e3)) % 1 * 24 % 1 * 60 % 1 * 60, 1), 's')

    return None


def format_Byte_size(size: float, decimals: int = 3) -> Tuple[float, str]:
    """
    Format total memory size of calculation for print.

    Arguments
    =========
    
    1. size: ``float``
        memory size of calculation in GiB
    2. decimals: ``int`` ``default = 3``
        number of decimal digits to round to

    Returns
    =======
    
    1. size, unit: ``Tuple[float, str]``
        formatted size for print
    """
    
    if size < 1e-3:
        return round(size * (1024**2), decimals), 'kiB'
    elif size < 1:
        return round(size * 1024, decimals), 'MiB'
    else:
        return round(size, decimals), 'GiB'