#!/usr/bin/env python

"""
rezrxt-ls
    list components of a resolve (rxt) database.
"""
import argparse
import json
from os.path import isdir, realpath
from os import environ
from pprint import pprint
import sys
import time

from rezrxt import constants
from rezrxt.filebacked import reader
from rez.resolved_context import ResolvedContext
from rezrxt.timeutils import epoc_to_gm_asctime, epoc_to_loc_asctime
from rez.utils.colorize import  critical, heading, local, implicit, Printer

def pretty(jfile):
    """
    Selectively print information from resolve.
    """
    pr = Printer(sys.stdout)

    resolved = ResolvedContext.load(jfile)
    pr()
    pr("Resolve Information", heading)
    resolved.print_info(verbosity=True)

    pr()
    pr("timestamp", heading)
    t_stamp = resolved.created
    print "\t{0} | GMT {1} | LOCAL {2} ".format(t_stamp,
                                                epoc_to_gm_asctime(t_stamp),
                                                epoc_to_loc_asctime(t_stamp))
    print ""
    pr("tools", heading)
    for key, val in resolved.get_tools().iteritems():
        pr("\t{0}".format(key), implicit)
        for tool in val[1]:
            print "\t\t{0}".format(tool)


def main():
    """
    Main entry point
    """
    parser = argparse.ArgumentParser(usage=("Usage: rezrxt-ls [options] "
                                            "[context [name [timestamp]]]"),
                                     description='list components in the rez rxt database.')
    parser.add_argument('cmdargs', nargs='*',
                        help='list contexts')
    parser.add_argument('-d', '--db', dest='database',
                        help=('Optionally provide a path to the root database directory.'
                              ' Otherwise, use Env Var "{0}"').format(constants.REZRXT_DB_ROOT))
    parser.add_argument('-g', '--gmt', dest="gmt", action="store_true",
                        help="Report timestamps in gmt time.")
    parser.add_argument('-l', '--loc', dest="loc", action="store_true",
                        help="Report timestamps in local time.")
    parser.add_argument('-a', '--approximate', dest="approx", action="store_true",
                        help="Find the rxt with the approximate timestmap.")
    parser.add_argument('-f', '--file', dest="file", action="store_true",
                        help=("print file instead of contents when supplying "
                              "context, name, and timestamp."))
    parser.add_argument('-r', '--raw', dest='raw', action='store_true',
                        help=("Print the raw rxt contents when"
                              " supplying context, name, and timestamp."))
    args = parser.parse_args()

    db_root = args.database or environ.get(constants.REZRXT_DB_ROOT)

    if db_root is None:
        print parser.usage
        exit(0)
    else:
        db_root = realpath(db_root)

    if not isdir(db_root):
        print parser.usage
        print "\nfile \"{0}\" is not an directory\n".format(db_root)
        exit(0)

    try:
        db_reader = reader.RezRxtDbReader(db_root)
    except RuntimeError, err:
        print err.message
        exit(0)

    if len(args.cmdargs) == 0:
        for ctx in db_reader.contexts():
            print ctx
        exit(0)

    if len(args.cmdargs) == 1:
        try:
            for name in db_reader.names(args.cmdargs[0]):
                print name
        except KeyError, err:
            print err.message
        finally:
            exit(0)

    if len(args.cmdargs) == 2:
        try:
            for t_stamp in db_reader.timestamps(args.cmdargs[0], args.cmdargs[1]):
                print epoc_to_gm_asctime(t_stamp) if args.gmt else (\
                    epoc_to_loc_asctime(t_stamp) if args.loc else t_stamp)
        except KeyError, err:
            print err.message
        finally:
            exit(0)

    try:
        if len(args.cmdargs) == 3:
            if args.file:
                timestamp = args.cmdargs[2]
                timestamp = time.time() if timestamp == "now" else timestamp 
                print db_reader.resolve(args.cmdargs[0], args.cmdargs[1], args.cmdargs[2],
                                        approximate=True if args.approx else False)
            else:
                rxt_f = db_reader.resolve(args.cmdargs[0], args.cmdargs[1], args.cmdargs[2],
                                          approximate=True if args.approx else False)

                if args.raw:
                    with open(rxt_f) as data_file:
                        data = json.load(data_file)
                    pprint(data)
                else:
                    pretty(rxt_f)

    except KeyError, err:
        print err.message
        print "Have you tried using the \"--approximate\" flag?"
    exit(0)

    print parser.usage

if __name__ == "__main__":
    main()
