import re
from pathlib import Path
from typing import Any
from typing import Union

import click


class TablePrinter:
    """A helper context manager that prints pretty tables."""

    WIDTH = 24

    def __init__(self, *columns: str) -> None:
        """Initialize the context manager and print the header."""
        self._num_columns = len(columns)
        column_str = [columns[0].ljust(self.WIDTH)] + [
            column.rjust(self.WIDTH) for column in columns[1:]
        ]

        column_str = [click.style(c, fg="cyan") for c in column_str]
        click.echo(" | ".join(column_str))
        click.echo("-|-".join([("-" * self.WIDTH)] * self._num_columns))

    @classmethod
    def print_row(cls, *columns: Any):
        """Print a single row."""
        column_str = [click.style(columns[0].ljust(cls.WIDTH), fg="green")] + [
            str(column).rjust(cls.WIDTH) for column in columns[1:]
        ]
        click.echo(" | ".join(column_str))

    def __enter__(self):
        """Enter the context manager and return the printing function."""
        return self.print_row

    def __exit__(self, type, value, traceback):
        """Print the footer."""
        click.echo("-|-".join([("-" * self.WIDTH)] * self._num_columns))


class RegexType(click.ParamType):
    name = "regex"

    def convert(self, value, param, ctx):
        try:
            return re.compile(value, re.IGNORECASE)
        except re.error as e:
            self.fail(
                "Regex error: " + str(e),
                param,
                ctx,
            )


def get_craft_name(filename: Union[str, Path], default: str) -> str:
    """Return the craft name (or a default) from the filename."""
    m = re.search("([^_]+)_", Path(filename).name)
    if not m:
        return default
    return m.group(1)
