#!/usr/bin/env python3

import click
from pyrule_compendium import compendium as c
from json import dumps
from pygments import highlight, lexers, formatters
from json import dumps
from cli_helpers import tabular_output

def format_dict(object: dict) -> str:
    return(
        highlight(
            dumps(
                object, 
                indent=4
            ), 
            lexers.JsonLexer(), 
            formatters.TerminalFormatter()
        )
    )

comp = c(default_timeout=5)

@click.group()
def compendium():
    "Get the Hyrule Compendium onto your command line"
    
@compendium.command()
@click.argument("target_entry")
def entry(target_entry):
    "Get data on an entry"
    try:
        target_entry = int(target_entry)
    except ValueError: pass
    click.echo(format_dict(comp.get_entry(target_entry)))


@compendium.command()
@click.argument("target_category")
def category(target_category):
    "Get items in a category"
    click.echo(format_dict(comp.get_category(target_category)))

@compendium.command()
def all():
    "Get all items from the compendium"
    click.echo(format_dict(comp.get_all()))
    
@compendium.command()
@click.argument("entry")
@click.option("--output", "-o", type=click.Path(), default=None, help="The output file of the image")
def image(entry, output):
    "Download the image of an entry"
    comp.get_image(entry).download(output_file=output)

compendium()