#
# Copyright (c) Daniel Sheffield 2023
#
# All rights reserved
#
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
from typing import List, Tuple
from pandas import DataFrame

def stats(data: DataFrame, num: str, den: str, frac: str) -> List[float]:
    return list(map(float, [
        data[frac].min(),
        data[frac].median(),
        data[num].sum()/data[den].sum(), # mean
        data[frac].max()
    ]))

def get_time_range(data: DataFrame, ts_col: str) -> Tuple[str, str]:
    return f"{data[ts_col].min():%d/%m/%Y}" , f"{data[ts_col].max():%d/%m/%Y}"

def get_divider(width: int, marker:str = "─") -> str:
    if width <= 3:
        return " ─ "

    plen = width//2 - 1
    ex = marker if (width % 2) and plen > 0 else ""
    return f" {{p:>{plen}}}{ex}{{p:<{plen}}} ".format(p=marker)

def get_caption(time_range: Tuple[str, str], width: int) -> str:
    left, right = time_range
    pad = width - len(left) - len(right)
    return ''.join([
        left, get_divider(pad, marker="─"), right
    ])
