#! /usr/bin/env/python
from showdata import generate_html_table, load_dir
import click
import os


@click.command()
@click.option('-i', '--input-path', help='Input path. It can be a folder, a pandas pkl/csv file or json file.')
@click.option('-o', '--output-path', default='./index.html', help='Output path. It must in a parent directory of all images. Default is ./index.html.')
@click.option('-w', '--width', default='400', help='Width of all the images. Default is 400')
@click.option('-h', '--height', default='auto', help='Height of all the images. Default is auto')
@click.option('-l', '--level', default=1, type=int, help='Level of folder to be generated. Default is 1')
@click.option('-r', '--rel-path', default=True, type=bool, help='Whether to use relative path of input path and output path. Default is True')
@click.option('-f', '--float-precision', default=2, type=int, help='Float precision. Default is 2')
@click.option('-m', '--max-str-len', default=50, type=int, help='Max string length. Default is 50')
def cmd(input_path, output_path, width, height, level, rel_path, float_precision, max_str_len):
    assert os.path.exists(input_path), 'Input file not exists.'
    data_table = []
    if os.path.isdir(input_path):
        data_table = load_dir(input_path, level)

    elif input_path.endswith('pkl'):
        import pandas as pd
        data_table = pd.read_pickle(input_path).to_dict('records')

    elif input_path.endswith('csv'):
        import pandas as pd
        data_table = pd.read_csv(input_path, index_col=0).to_dict('records')

    elif input_path.endswith('json'):
        import json
        with open(input_path, 'r') as f:
            data_table = json.load(f)

    generate_html_table(data_table,
                        image_width=width,
                        image_height=height,
                        output_path=output_path,
                        rel_path=rel_path,
                        float_precision=float_precision,
                        max_str_len=max_str_len)


if __name__ == "__main__":
    cmd()
