"""Common arguments and options for the CLI"""
from typing import List, Union

import click

CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}
ROLES = ["ADMIN", "TEAM_MANAGER", "REVIEWER", "LABELER"]


class BoolOrStringParamType(click.ParamType):
    """Click parameter type for boolean or string. This is useful for the --ssl-verify option,
    that can take a boolean or a string as value (like the `requests` library)"""

    name = "bool_or_string"

    def convert(self, value, param, ctx) -> Union[bool, str]:
        if value.lower() == "true":
            return True
        if value.lower() == "false":
            return False
        return value


BOOL_OR_STRING = BoolOrStringParamType()


class Options:  # pylint: disable=too-few-public-methods
    """Common options for the CLI"""

    api_key = click.option(
        "--api-key",
        type=str,
        default=None,
        help="Your Kili API key. ",
        show_default='"KILI_API_KEY" environment variable',
    )

    endpoint = click.option(
        "--endpoint",
        type=str,
        default=None,
        help="Kili API Endpoint. ",
        show_default=(
            '"KILI_API_ENDPOINT" environment variable,'
            ' Kili SAAS: "https://cloud.kili-technology.com/api/label/v2/graphql"'
        ),
    )

    project_id = click.option("--project-id", type=str, required=True, help="Id of the project")

    tablefmt = click.option(
        "--stdout-format",
        "tablefmt",
        type=str,
        default="plain",
        help=(
            "Defines how the output table is formatted "
            "(see https://pypi.org/project/tabulate/, default: plain)."
        ),
    )

    from_project = click.option(
        "--from-project",
        "project_id_src",
        type=str,
        help="project_id of another Kili project",
    )

    role = click.option(
        "--role",
        type=click.Choice(ROLES),
        default=None,
        show_default="LABELER",
        help="Project role of the added user(s).",
    )

    script_name = click.option(
        "--script-name",
        type=str,
        default=None,
        help=(
            "Script's name in Kili database."
            "If not provided, it will be the file name on your machine"
        ),
    )

    verbose = click.option(
        "--verbose", type=bool, is_flag=True, default=False, help="Show more logs"
    )

    ssl_verify = click.option(
        "--ssl-verify",
        type=BOOL_OR_STRING,
        default="True",
        help=(
            "Verify SSL certificate (see Python's requests' verify argument documentation for more"
            " details:"
            " https://requests.readthedocs.io/en/latest/user/advanced/#ssl-cert-verification)"
        ),
    )


def from_csv(required_columns: List[str], optionnal_columns: List[str]):
    """--from-csv shared click option"""
    help_ = (
        "path to a csv file with required columns:"
        + ", ".join(required_columns)
        + " required columns: "
        + ", ".join(optionnal_columns)
    )

    return click.option(
        "--from-csv",
        "csv_path",
        type=click.Path(),
        help=help_,
    )


class Arguments:  # pylint: disable=too-few-public-methods
    """Common arguments for the CLI"""

    files = click.argument("files", type=click.Path(), required=False, nargs=-1)

    emails = click.argument("emails", type=str, required=False, nargs=-1)

    project_id = click.argument("project_id", type=str, required=True)
