# Copyright 2016 Allan Rank
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import click


def prompt(choices, text, close=False):
    items = ["{} {}".format(idx, c) for idx, c in enumerate(choices)]
    if close:
        items.append("c <close>")
    click.echo()
    click.secho("\n".join(items), bold=True)
    try:
        return int(click.prompt(text))
    except ValueError:
        return len(choices)


def format_columns(columns):
    if columns:
        return ", ".join([c.name for c in columns])
    else:
        return "no columns"


def print_header(text, bold=False):
    click.echo()
    line = "".join(["-" for _ in xrange(len(text) + 2)])
    click.secho(line, bold=bold)
    click.secho(" {}".format(text), bold=bold)
    click.secho(line, bold=bold)


def print_dict(map, name):
    if map:
        print_header("{}: {}".format(name, len(map)))
        items = "\n".join(map.keys())
        click.echo(items)


def print_host(host):
    click.echo("{}\tv{}\t{}\t{}".format(
        host.broadcast_address,
        host.release_version,
        host.rack,
        host.datacenter))


def print_keyspace(keyspace_meta):
    print_header("{} {}".format("keyspace:", keyspace_meta.name), True)

    replication_strategy = keyspace_meta.replication_strategy
    if replication_strategy:
        click.echo("replication:\t{}".format(replication_strategy.export_for_schema()))
    click.echo("durable writes:\t{}".format(keyspace_meta.durable_writes))

    print_dict(keyspace_meta.tables, "tables")
    print_dict(keyspace_meta.views, "views")
    print_dict(keyspace_meta.indexes, "indexes")
    print_dict(keyspace_meta.user_types, "user types")
    print_dict(keyspace_meta.functions, "functions")
    print_dict(keyspace_meta.aggregates, "aggregates")


def print_table(table_meta):
    def max_column(column):
        return len(column.name)

    print_header("table: {}.{}".format(table_meta.keyspace_name, table_meta.name), True)
    click.echo("primary key:\t(({}), {})".format(format_columns(table_meta.partition_key),
                                                 format_columns(table_meta.clustering_key)))
    columns = table_meta.columns.values()
    columns_text = "\n".join(["{}\t{}".format(c.name, c.cql_type) for c in columns])
    max_len_column = max(columns, key=max_column)

    print_header("{}: {}".format("columns", len(columns)))
    click.echo(columns_text.expandtabs(len(max_len_column.name) + 2))

    print_dict(table_meta.views, "views")
    print_dict(table_meta.indexes, "indexes")
