#!/usr/bin/env python
"""Tron Control

Part of the command line interface to the tron daemon. Provides the interface
to controlling jobs and runs.
"""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import datetime
import logging
import sys

import argcomplete
from six.moves.urllib.parse import urljoin

from tron.commands import client
from tron.commands import cmd_utils
from tron.commands.cmd_utils import ExitCode
from tron.commands.cmd_utils import suggest_possibilities
from tron.commands.cmd_utils import tron_jobs_completer

COMMAND_HELP = (
    ('start', 'Start the selected job, job run, or action'),
    ('rerun', 'Re-run a full job (all actions) with a new job id'),
    ('retry', 'Re-run a job action within an existing job run'),
    ('cancel', 'Cancel the selected job run'),
    ('backfill', 'Start many jobs for a particular date range'),
    ('disable', 'Disable selected job and cancel any outstanding runs'),
    ('enable', 'Enable the selected job and schedule the next run'),
    ('fail', 'Mark an UNKNOWN job as having failed'),
    ('success', 'Mark an UNKNOWN job as having succeeded'),
    ('skip', 'Skip a failed action, runs dependent actions.'),
    ('stop', 'Stop the action run (SIGTERM)'),
    ('kill', 'Force kill the action run (SIGKILL)'),
    ('move', 'Rename a job'),
)

log = logging.getLogger('tronctl')


def command_help_epilog():
    # We want to include some extra helpful info
    result = ["commands:", "\n"]
    for cmd_name, desc in COMMAND_HELP:
        result.append('  ')
        text = ''.join((cmd_name.ljust(15), desc.ljust(40)))
        result.append(text)
        result.append('\n')
    return ''.join(result)


def parse_date(date_string):
    return datetime.datetime.strptime(date_string, "%Y-%m-%d")


def parse_cli():
    parser = cmd_utils.build_option_parser(epilog=command_help_epilog(), )

    parser.add_argument(
        "--run-date",
        type=parse_date,
        dest="run_date",
        help="For job starts, what should run-date be set to",
    )
    parser.add_argument(
        "--start-date",
        type=parse_date,
        dest="start_date",
        help="For backfills, what should the first run-date be",
    )
    parser.add_argument(
        "--end-date",
        type=parse_date,
        dest="end_date",
        help=
        "For backfills, what should the last run-date be (note: many jobs operate on date-1). Defaults to today.",
    )
    parser.add_argument(
        'command',
        help='Tronctl command to run',
        choices=[row[0] for row in COMMAND_HELP],
    )
    parser.add_argument(
        'id',
        nargs='+',
        help='job name, job run id, or action id',
    ).completer = cmd_utils.tron_jobs_completer

    argcomplete.autocomplete(parser)
    args = parser.parse_args()

    return args


def edit(args, uri):
    data = {'command': args.command}
    if args.command == "start" and args.run_date:
        data['run_time'] = str(args.run_date)
    elif args.command == "move":
        data['old_name'] = args.id[0]
        data['new_name'] = args.id[1]

    uri = urljoin(args.server, uri)
    response = client.request(uri, data=data)

    if response.error:
        print(f'Error: {response.content}')
        return

    print(response.content['result'])
    return True


def control_objects(args):
    tron_client = client.Client(args.server)
    url_index = tron_client.index()
    for identifier in args.id:
        try:
            tron_id = client.get_object_type_from_identifier(
                url_index,
                identifier,
            )
        except ValueError as e:
            possibilities = list(
                tron_jobs_completer(prefix='', client=tron_client)
            )
            suggestions = suggest_possibilities(
                word=identifier, possibilities=possibilities
            )
            raise SystemExit(f"Error: {e}{suggestions}")
        yield edit(args, tron_id.url)


def move(args):
    try:
        old_name = args.id[0]
        new_name = args.id[1]
    except IndexError as e:
        raise SystemExit(f"Error: Move command needs two arguments.\n{e}")

    tron_client = client.Client(args.server)
    url_index = tron_client.index()
    job_index = url_index['jobs']
    if old_name not in job_index.keys():
        raise SystemExit(f"Error: {old_name} doesn't exist")
    if new_name in job_index.keys():
        raise SystemExit(f"Error: {new_name} exists already")

    return edit(args, '/api/jobs')


def backfill(args):
    if args.start_date is None:
        print("Error: For a backfill, --start-date must be set")
        return 1
    if args.end_date is None:
        args.end_date = datetime.datetime.today()
    dates = get_dates_for_backfill(
        start_date=args.start_date, end_date=args.end_date
    )
    print("tronctl backfill currently only prints jobs for a human to run.")
    print(f"Please run the following {len(dates)} commands:")
    print("")
    for date in dates:
        print(f"tronctl start {args.id[0]} --date {date}")
    print("")
    print("Note that many jobs operate on the previous day's data.")


def get_dates_for_backfill(start_date, end_date):
    dates = []
    delta = end_date - start_date
    for i in range(delta.days + 1):
        dates.append((start_date + datetime.timedelta(i)).date().isoformat())
    return dates


def main():
    """run tronctl"""
    args = parse_cli()
    cmd_utils.setup_logging(args)
    cmd_utils.load_config(args)

    if args.command == "backfill":
        sys.exit(backfill(args))
    elif args.command == "move":
        sys.exit(move(args))
    else:
        if not all(control_objects(args)):
            sys.exit(ExitCode.fail)


if __name__ == '__main__':
    main()
