from rich import print
from rich.console import Console
from rich.table import Table
import click
from click_default_group import DefaultGroup
import yaml
import os
##from terminaltables import SingleTable
import sys
from textwrap import wrap
import collections
import datetime
import configparser
import pkg_resources  # part of setuptools

VERSION = pkg_resources.require("clikan")[0].version


class Config(object):
    """The config in this example only holds aliases."""

    def __init__(self):
        self.path = os.getcwd()
        self.aliases = {}

    def read_config(self, filename):
        parser = configparser.RawConfigParser()
        parser.read([filename])
        try:
            self.aliases.update(parser.items('aliases'))
        except configparser.NoSectionError:
            pass


pass_config = click.make_pass_decorator(Config, ensure=True)


class AliasedGroup(DefaultGroup):
    """This subclass of a group supports looking up aliases in a config
    file and with a bit of magic.
    """

    def get_command(self, ctx, cmd_name):
        # Step one: bulitin commands as normal
        rv = click.Group.get_command(self, ctx, cmd_name)
        if rv is not None:
            return rv

        # Step two: find the config object and ensure it's there.  This
        # will create the config object is missing.
        cfg = ctx.ensure_object(Config)

        # Step three: lookup an explicit command aliase in the config
        if cmd_name in cfg.aliases:
            actual_cmd = cfg.aliases[cmd_name]
            return click.Group.get_command(self, ctx, actual_cmd)

        # Alternative option: if we did not find an explicit alias we
        # allow automatic abbreviation of the command.  "status" for
        # instance will match "st".  We only allow that however if
        # there is only one command.
        matches = [x for x in self.list_commands(ctx)
                   if x.lower().startswith(cmd_name.lower())]
        if not matches:
            return None
        elif len(matches) == 1:
            return click.Group.get_command(self, ctx, matches[0])
        ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))


def read_config(ctx, param, value):
    """Callback that is used whenever --config is passed.  We use this to
    always load the correct config.  This means that the config is loaded
    even if the group itself never executes so our aliases stay always
    available.
    """
    cfg = ctx.ensure_object(Config)
    if value is None:
        value = os.path.join(os.path.dirname(__file__), 'aliases.ini')
    cfg.read_config(value)
    return value


@click.version_option(VERSION)
@click.command(cls=AliasedGroup, default='show', default_if_no_args=True)
def clikan():
    """clikan: CLI personal kanban """


@clikan.command()
def configure():
    """Place default config file in CLIKAN_HOME or HOME"""
    home = get_clikan_home()
    data_path = os.path.join(home, ".clikan.dat")
    config_path = os.path.join(home, ".clikan.yaml")
    if (os.path.exists(config_path) and not
            click.confirm('Config file exists. Do you want to overwrite?')):
        return
    with open(config_path, 'w') as outfile:
        conf = {'clikan_data': data_path}
        yaml.dump(conf, outfile, default_flow_style=False)
    click.echo("Creating %s" % config_path)


@clikan.command()
@click.argument('task')
def add(task):
    """Add a task in todo"""
    if len(task) > 40:
        click.echo('Task must be shorter than 40 chars. Brevity counts.')
    else:
        config = read_config_yaml()
        dd = read_data(config)

        todos, inprogs, dones = split_items(config, dd)
        if ('limits' in config and 'todo' in config['limits'] and
                int(config['limits']['todo']) <= len(todos)):
            click.echo('No new todos, limit reached already.')
        else:
            od = collections.OrderedDict(sorted(dd['data'].items()))
            new_id = 1
            if bool(od):
                new_id = next(reversed(od)) + 1
            entry = ['todo', task, timestamp(), timestamp()]
            dd['data'].update({new_id: entry})

            click.echo("Creating new task w/ id: %d -> %s" % (new_id, task))
            write_data(config, dd)


@clikan.command()
@click.argument('id')
def delete(id):
    """Delete task"""
    config = read_config_yaml()
    dd = read_data(config)
    item = dd['data'].get(int(id))
    if item is None:
        click.echo('No existing task with that id.')
    else:
        item[0] = 'deleted'
        item[2] = timestamp()
        dd['deleted'].update({int(id): item})
        dd['data'].pop(int(id))
        write_data(config, dd)
        click.echo('Removed task %d.' % int(id))


@clikan.command()
@click.argument('id')
def promote(id):
    """Promote task"""
    config = read_config_yaml()
    dd = read_data(config)
    todos, inprogs, dones = split_items(config, dd)

    item = dd['data'].get(int(id))
    if item[0] == 'todo':
        if ('limits' in config and 'wip' in config['limits'] and
                int(config['limits']['wip']) <= len(inprogs)):
            click.echo('No new tasks, limit reached already.')
        else:
            click.echo('Promoting task %s to in-progress.' % id)
            dd['data'][int(id)] = ['inprogress', item[1], timestamp(), item[3]]
            write_data(config, dd)
    elif item[0] == 'inprogress':
        click.echo('Promoting task %s to done.' % id)
        dd['data'][int(id)] = ['done', item[1], timestamp(), item[3]]
        write_data(config, dd)
    else:
        click.echo('Already done, can not promote %s' % id)


@clikan.command()
@click.argument('id')
def regress(id):
    """Regress task"""
    config = read_config_yaml()
    dd = read_data(config)
    item = dd['data'].get(int(id))
    if item[0] == 'done':
        click.echo('Regressing task %s to in-progress.' % id)
        dd['data'][int(id)] = ['inprogress', item[1], timestamp(), item[3]]
        write_data(config, dd)
    elif item[0] == 'inprogress':
        click.echo('Regressing task %s to todo.' % id)
        dd['data'][int(id)] = ['todo', item[1], timestamp(), item[3]]
        write_data(config, dd)
    else:
        click.echo('Already in todo, can not regress %s' % id)


@clikan.command()
def show():
    console = Console()
    """Show tasks in clikan"""
    config = read_config_yaml()
    dd = read_data(config)
    todos, inprogs, dones = split_items(config, dd)
    if 'limits' in config and 'done' in config['limits']:
        dones = dones[0:int(config['limits']['done'])]
    else:
        dones = dones[0:10]

    todos = '\n'.join([str(x) for x in todos])
    inprogs = '\n'.join([str(x) for x in inprogs])
    dones = '\n'.join([str(x) for x in dones])

    # td = [
    #     ['todo', 'in-progress', '[bold magenta]done[/bold magenta]'],
    #     ['', '', ''],
    # ]

    #table = SingleTable(td, 'clikan v.{}'.format(VERSION))
    # table.inner_heading_row_border = False
    # table.inner_row_border = True
    # table.justify_columns = {0: 'center', 1: 'center', 2: 'center'}

    table = Table(show_header=True, show_footer=True)
    table.add_column("[bold yellow]todo[/bold yellow]", no_wrap=True, footer="clikan")
    table.add_column('[bold green]in-progress[/bold green]', no_wrap=True)
    table.add_column('[bold magenta]done[/bold magenta]', no_wrap=True, footer="v.{}".format(VERSION))

    # def wrap_lines(lines, column_index):
    #     max_width = table.column_max_width(column_index)
    #     packed = [line for line in lines if line.strip() != '']
    #     wrapped = [wrap(line, max_width, break_long_words=False,
    #                     replace_whitespace=False) for line in packed]
    #     return '\n'.join(['\n'.join(w) for w in wrapped])

    # for index, section in enumerate((todos, inprogs, dones)):
    #     table.table_data[1][index] = wrap_lines(section.splitlines(), index)
    table.add_row(todos, inprogs, dones)
    console.print(table)
    #print(table.table)


def read_data(config):
    """Read the existing data from the config datasource"""
    try:
        with open(config["clikan_data"], 'r') as stream:
            try:
                return yaml.load(stream, Loader=yaml.FullLoader)
            except yaml.YAMLError as exc:
                print("Ensure %s exists, as you specified it "
                      "as the clikan data file." % config['clikan_data'])
                print(exc)
    except IOError:
        click.echo("No data, initializing data file.")
        write_data(config, {"data": {}, "deleted": {}})
        with open(config["clikan_data"], 'r') as stream:
            return yaml.load(stream, Loader=yaml.FullLoader)


def write_data(config, data):
    """Write the data to the config datasource"""
    with open(config["clikan_data"], 'w') as outfile:
        yaml.dump(data, outfile, default_flow_style=False)


def get_clikan_home():
    home = os.environ.get('CLIKAN_HOME')
    if not home:
        home = os.path.expanduser('~')
    return home


def read_config_yaml():
    """Read the app config from ~/.clikan.yaml"""
    try:
        home = get_clikan_home()
        with open(home + "/.clikan.yaml", 'r') as stream:
            try:
                return yaml.load(stream, Loader=yaml.FullLoader)
            except yaml.YAMLError:
                print("Ensure %s/.clikan.yaml is valid, expected YAML." % home)
                sys.exit()
    except IOError:
        print("Ensure %s/.clikan.yaml exists and is valid." % home)
        sys.exit()


def split_items(config, dd):
    todos = []
    inprogs = []
    dones = []

    for key, value in dd['data'].items():
        if value[0] == 'todo':
            todos.append("[%d] %s" % (key, value[1]))
        elif value[0] == 'inprogress':
            inprogs.append("[%d] %s" % (key, value[1]))
        else:
            dones.insert(0, "[%d] %s" % (key, value[1]))

    return todos, inprogs, dones


def timestamp():
    return '{:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now())

[(35, 'click.make_pass_decorator', 'click.make_pass_decorator', False, 'import click\n'), (84, 'click.version_option', 'click.version_option', False, 'import click\n'), (85, 'click.command', 'click.command', False, 'import click\n'), (106, 'click.argument', 'click.argument', False, 'import click\n'), (132, 'click.argument', 'click.argument', False, 'import click\n'), (150, 'click.argument', 'click.argument', False, 'import click\n'), (175, 'click.argument', 'click.argument', False, 'import click\n'), (94, 'os.path.join', 'os.path.join', False, 'import os\n'), (95, 'os.path.join', 'os.path.join', False, 'import os\n'), (102, 'click.echo', 'click.echo', False, 'import click\n'), (195, 'rich.console.Console', 'Console', False, 'from rich.console import Console\n'), (219, 'rich.table.Table', 'Table', False, 'from rich.table import Table\n'), (262, 'os.environ.get', 'os.environ.get', False, 'import os\n'), (16, 'pkg_resources.require', 'pkg_resources.require', False, 'import pkg_resources\n'), (23, 'os.getcwd', 'os.getcwd', False, 'import os\n'), (27, 'configparser.RawConfigParser', 'configparser.RawConfigParser', False, 'import configparser\n'), (45, 'click.Group.get_command', 'click.Group.get_command', False, 'import click\n'), (96, 'os.path.exists', 'os.path.exists', False, 'import os\n'), (101, 'yaml.dump', 'yaml.dump', False, 'import yaml\n'), (110, 'click.echo', 'click.echo', False, 'import click\n'), (139, 'click.echo', 'click.echo', False, 'import click\n'), (182, 'click.echo', 'click.echo', False, 'import click\n'), (258, 'yaml.dump', 'yaml.dump', False, 'import yaml\n'), (264, 'os.path.expanduser', 'os.path.expanduser', False, 'import os\n'), (300, 'datetime.datetime.now', 'datetime.datetime.now', False, 'import datetime\n'), (56, 'click.Group.get_command', 'click.Group.get_command', False, 'import click\n'), (79, 'os.path.dirname', 'os.path.dirname', False, 'import os\n'), (97, 'click.confirm', 'click.confirm', False, 'import click\n'), (118, 'click.echo', 'click.echo', False, 'import click\n'), (127, 'click.echo', 'click.echo', False, 'import click\n'), (161, 'click.echo', 'click.echo', False, 'import click\n'), (163, 'click.echo', 'click.echo', False, 'import click\n'), (167, 'click.echo', 'click.echo', False, 'import click\n'), (171, 'click.echo', 'click.echo', False, 'import click\n'), (186, 'click.echo', 'click.echo', False, 'import click\n'), (190, 'click.echo', 'click.echo', False, 'import click\n'), (249, 'click.echo', 'click.echo', False, 'import click\n'), (279, 'rich.print', 'print', False, 'from rich import print\n'), (280, 'sys.exit', 'sys.exit', False, 'import sys\n'), (67, 'click.Group.get_command', 'click.Group.get_command', False, 'import click\n'), (243, 'yaml.load', 'yaml.load', False, 'import yaml\n'), (252, 'yaml.load', 'yaml.load', False, 'import yaml\n'), (274, 'yaml.load', 'yaml.load', False, 'import yaml\n'), (245, 'rich.print', 'print', False, 'from rich import print\n'), (247, 'rich.print', 'print', False, 'from rich import print\n'), (276, 'rich.print', 'print', False, 'from rich import print\n'), (277, 'sys.exit', 'sys.exit', False, 'import sys\n')]