#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# 2012 Nico Schottelius (nico-ctt at schottelius.org)
#
# This file is part of ctt.
#
# ctt is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ctt is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ctt. If not, see <http://www.gnu.org/licenses/>.
#
#

import argparse
import logging
import os.path
import sys

log = logging.getLogger(__name__)

# Setup locale for calendar printing
# Setup locale to get Timezone information?
#print(locale.getlocale())

# Record tags

def parse_argv(argv, version):
    parser = {}
    parser['loglevel'] = argparse.ArgumentParser(add_help=False)
    parser['loglevel'].add_argument('-d', '--debug',
        help='Set log level to debug', action='store_true',
        default=False)
    parser['loglevel'].add_argument('-v', '--verbose',
        help='Set log level to info, be more verbose',
        action='store_true', default=False)

    parser['main'] = argparse.ArgumentParser(description='ctt ' + version,
        parents=[parser['loglevel']])
    parser['sub'] = parser['main'].add_subparsers(title="Commands")


    parser['track'] = parser['sub'].add_parser('track',
        parents=[parser['loglevel']])
    parser['track'].set_defaults(func=Tracker.commandline)
    parser['track'].add_argument("-s", "--start", help="Start datetime (format: %s)" % ctt.DATETIMEFORMAT_PLAIN,
        nargs=1)
    parser['track'].add_argument("-e", "--end", help="End datetime (format: %s)" % ctt.DATETIMEFORMAT_PLAIN,
        nargs=1)
    parser['track'].add_argument("-c", "--comment", help="Add comment after tracking", action='store_true')
    parser['track'].add_argument("project", help="Project to track time for", nargs=1)

    parser['report'] = parser['sub'].add_parser('report',
        parents=[parser['loglevel']])
    parser['report'].set_defaults(func=Report.commandline)
    parser['report'].add_argument("project", help="Project to report time for", nargs=1)
    parser['report'].add_argument("-s", "--start", help="Start date (default: first of last month, format: %s)" % ctt.DATEFORMAT_PLAIN,
        nargs=1)
    parser['report'].add_argument("-e", "--end", help="End date (default: last of last month, format: %s)" % ctt.DATEFORMAT_PLAIN, nargs=1,
        default=None)

    #parser['track'].add_argument("-t", "--tag", help="Add tags",
    #    action="store_true")

    args = parser['main'].parse_args()

    if args.verbose:
        logging.root.setLevel(logging.INFO)
    if args.debug:
        logging.root.setLevel(logging.DEBUG)

    log.debug(args)

    try:
        args.func(args)

    except ctt.Error as e:
        log.error(e)
        sys.exit(1)

# Setup signal handler
# Start tracking
# Save stuff to our home directory

if __name__ == "__main__":
    # Ensure our /lib/ is included into PYTHON_PATH
    sys.path.insert(0, os.path.abspath(
        os.path.join(os.path.dirname(os.path.realpath(__file__)), '../lib')))

    #logging.basicConfig(format='%(levelname)s: %(message)s')
    logging.basicConfig(format='%(message)s')

    import ctt
    from ctt.tracker import Tracker
    from ctt.report import Report

    parse_argv(sys.argv[1:], ctt.VERSION)
    sys.exit(0)


# Create datetime from userinput
# Wed Aug  1 23:35:53 2012
