from rich.console import Console
from rich.markdown import Markdown
from rich.panel import Panel
from rich.prompt import Confirm, Prompt
from rich.table import Table

from flyers import jsons

DEFAULT_STYLE = 'cyan'

console = Console()


def print_table(title: str, columns: list, data_list: list):
    table = Table(title=title)

    for col in columns:
        if isinstance(col, str):
            table.add_column(col, style=DEFAULT_STYLE)
        elif isinstance(col, dict):
            table.add_column(col.get('content'), style=col.get('style') or DEFAULT_STYLE)
        else:
            raise Exception('The colum type is unsupported')

    for data in data_list:
        table.add_row(*data)

    console.print(table)


def print_to_json(d: dict):
    console.print_json(jsons.to_json_str(d))


def print_markdown(content: str):
    md = Markdown(content)
    console.print(md)


def print_panel(content: str, title: str = None, fit: bool = False):
    if not fit:
        console.print(Panel(content, title=title))
    else:
        console.print(Panel.fit(content, title=title))


def prompt_ask(prompt: str, default: str = None, choices: list[str] = None):
    return Prompt.ask(prompt, default=default, choices=choices)


def prompt_confirm(prompt: str):
    return Confirm.ask(prompt)
