#! /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: 400')
@click.option('-h', '--height', default='auto', help='Height of all the images. Default: auto')
@click.option('-l', '--level', default=1, type=int, help='Level of folder to be generated')
def cmd(input_path, output_path, width, height, level):
    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)
        import pandas as pd
        pd.DataFrame(data_table).to_pickle('test_datashow.pkl')
        pd.DataFrame(data_table).to_csv('test_datashow.csv')


    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, width, height, output_path)


if __name__ == "__main__":
    cmd()
