#!/usr/bin/env python3
# This file is placed in the Public Domain.
# pylint: disable=R0903,C0103,C0114,C0115,C0116


import os
import sys
import time


sys.path.insert(0, os.getcwd())


from genocide.obj import Class, Db, Object, Wd, keys, printable, update
from genocide.obj import find, fntime, cdir, fns
from genocide.utl import elapsed


def fnd():
    if len(sys.argv) == 1:
        print("gcd workdir [type] [selector]")
        return
    Wd.workdir = sys.argv[1]
    cdir(Wd.storedir())
    if len(sys.argv) < 3:
        res = ",".join({x.split(".")[-1].lower() for x in Wd.types()})
        if res:
            print(res)
        else:
            print("no types yet.")
        return
    gets = Object()
    if len(sys.argv) > 3:
         update(gets, [x.split("==") for x in sys.argv[1:]])
    otype = sys.argv[2]
    dbs = Db()
    nrs = 0
    for otp in Wd.types():
        if otype not in otp.split(".")[-1].lower():
            continue
        for obj in dbs.find(otp):
            txt = "%s %s %s" % (
                                str(nrs),
                                printable(obj, keys(obj)),
                                elapsed(time.time()-fntime(obj.__fnm__))
                               )
            nrs += 1
            print(txt)
    if not nrs:
        print("no result")


fnd()
