#!/usr/bin/env python3

import click
import sys
import dateutil.tz as tz

from datetime import datetime
from wttime.parser import Parser
from wttime.formatter import format_parse
from typing import Optional


@click.command()
@click.option('--confidence',
              '-c',
              is_flag=True,
              show_default=True,
              help='Show the confidence level of the chosen interpretation.')
@click.option('--utc/--no-utc',
              '-s/-ns',
              default=True,
              show_default=True,
              help='Show the result in UTC.')
@click.option('--local/--no-local',
              '-l/-nl',
              default=False,
              show_default=True,
              help='Show the result the local time zone.')
@click.option('--remote/--no-remote',
              '-r/-nr',
              default=False,
              show_default=True,
              help='Show the result in the time zone specified by '
              '--remote-timezone.')
@click.option('--remote-timezone',
              default='America/Los_Angeles',
              show_default=True,
              help='Supplementary timezone to use for --remote, given in the "tz database" format.')
@click.option('--timezone',
              '-t',
              default=None,
              show_default=True,
              help='Timezone to interpret timestrings that do not have an explicit timezone, '
                   'given in the "tz database" format. Defaults to the local timezone.')
@click.option('--format',
              '-f',
              default='%Y-%m-%d %H:%M:%S (%z)',
              show_default=True,
              help='Datetime format specification in the Python strftime '
              'format (see http://strftime.org/ for reference).')
@click.option('--usec/--no-usec',
              '-u',
              default=False,
              show_default=True,
              help='Show the result as a Unix timestamp in seconds.')
@click.option('--umilli/--no-umilli',
              '-m',
              default=False,
              show_default=True,
              help='Show the result as a Unix timestamp in milliseconds.')
@click.option('--umicro/--no-umicro',
              '-y',
              default=False,
              show_default=True,
              help='Show the result as a Unix timestamp in microseconds.')
@click.option('--label/--no-label',
              '-x/-nx',
              is_flag=True,
              default=True,
              show_default=True,
              help='Print a label in front of each output.')
@click.argument('timestring', required=False)
def parse(confidence: bool, utc: bool, local: bool, remote: bool,
          remote_timezone: str, timezone: Optional[str], format: str,
          usec: bool, umilli: bool, umicro: bool,
          label: bool,
          timestring: Optional[str]) -> None:
    """Fuzzily parse TIMESTRING and print the result."""

    # TODO: Consider handling 0+ arguments uniformly
    stdin_timestring = sys.stdin.readline() if not sys.stdin.isatty() else None
    if timestring and stdin_timestring:
        click.echo(
            'WARNING: TIMESTRING both on the STDIN and passed as an '
            'argument.',
            err=True)
    timestring = timestring or stdin_timestring

    if not timestring:
        return

    base_timezone = Parser.parse_tz(timezone) if timezone else tz.tzlocal()
    parse = Parser().parse(datetime.now(tz=tz.tzlocal()), timestring, base_timezone)
    if not parse:
        print("Couldn't parse the input. Please file a bug at "
              "https://github.com/PJK/wttime/issues if I should have.")
        sys.exit(1)

    print(format_parse(parse[0], parse[1], tz.tzlocal(), confidence, utc, local, remote,
                       Parser.parse_tz(remote_timezone), format, usec, umilli, umicro, label))



if __name__ == '__main__':
    parse()
