#!/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 import Class, Db, Object, Wd, keys, printable, update
from genocide import find, fntime, cdir, elapsed, fns


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 otype:
            continue
        for fnm, obj in dbs.all(otp):
            txt = "%s %s %s" % (
                                str(nrs),
                                printable(obj, keys(obj)),
                                elapsed(time.time()-fntime(fnm))
                               )
            nrs += 1
            print(txt)
    if not nrs:
        print("no result")


fnd()
