#!/usr/bin/python3

import argparse
import sys

from erimp import Erimp
from grond import Grond


desc = 'feed vemlis from an issuedate into the grond'
parser = argparse.ArgumentParser(description=desc)
# # parameter settings
parser.add_argument('-T', type=int, help='target size')
parser.add_argument('-S', type=str, help='set sigfi')
# # action settings
parser.add_argument('-l', action='store_true', help='only produce vemLis')
# parser.add_argument('-s', action='store_true', help='show papids')
parser.add_argument('-u', action='store_true', help='update')
parser.add_argument('-f', action='store_true', help='fill')
parser.add_argument('-m', type=int, help='max vemli files with -f')
parser.add_argument('-t', action='store_true', help='transcribe')
parser.add_argument('-r', action='store_true', help='recalculate')
parser.add_argument('-c', action='store_true', help='produce samsu')
parser.add_argument('-p', action='store_true', help='produce spread')
# abandoned
#parser.add_argument('-I', action='store_true', help='splIt by Issuedate')
parser.add_argument('-i', type=str, help='insert issue')
parser.add_argument('-s', action='store_true', help='show')
# parser.add_argument('-r', action='store_true', help='only reload data')
# parser.add_argument('-f', action='store_true', help='start with fill')
# #  cut, just to test
#parser.add_argument('-c', type=int, help='cut to size')
parser.add_argument('-C', action="store_true", help='clear lost')
parser.add_argument('-v', action='store_true', help='raise verbosity')
parser.add_argument('bapis', metavar='bnp', type=str, nargs='*',
                    help='bits and pieces arguments')

args = parser.parse_args()
e = Erimp(args.bapis, do_verbose=args.v)
# # we only need to do this if we have grond
if 'is_seedable' not in e.conf:
    # print(f"{e.impna} is not seedable.")
    sys.exit()

if args.m is not None and args.f is None:
    print("-m only make sense with -f")
    sys.exit()

# #  we only need this for development purposes.
# #  we read a grond with a different sigfi that the normal,
# #  which would be the latest available
if args.S is not None:
    from sigsi import Sigsi
    sigsi = Sigsi(e)
    if not sigsi.is_it_valid(args.S):
        print(f"I don't seem sigfi '{args.S}'.")
        sys.exit()

grond = Grond(erimp=e, do_tarsi=args.T, do_sigfi=args.S,
              do_verbose=args.v)

if args.p:
#    grond.load()
    grond.spread()
    sys.exit()

if args.l:
#    grond.load()
    grond.write_class()
    sys.exit()

if args.s:
#    grond.load()
    grond.show()
    sys.exit()

if args.c:
    grond.get_samsu_cache()
    sys.exit()

#if args.I:
#    grond.split()
#    sys.exit()

if args.f:
    grond.load()
    # #  this will not load the grond,
    # #  has to be done before
    grond.feed_all(max_issues=args.m)
    sys.exit()

if args.C:
    # #  clear papids with vemlis but no source
    grond.clear_lost()
    sys.exit()

if args.t:
    # #  does its own load
    grond.transcribe()
    sys.exit()

if args.r:
    grond.load()
    grond.recalculate()
    sys.exit()

if args.u:
    grond.load()
    grond.feed_issue(e.max_issuedate)
    sys.exit()

# # this function just prints something it is now commented out
#if args.c:
#    print(e.max_issuedate)
#    grond.clear_outdated(e.max_issuedate)
#    quit()
#    #grond.load()
#    #grond.clear_outdate(e.max_issuedate)
#    sys.exit()

if args.i is not None:
    issuedate = args.i
    if issuedate not in e.issuedates:
        print(f"I don't know about an issuedete {issuedate}")
        sys.exit()
    grond.load()
    grond.get_dafus()
    # #  it does not really harm to feed an old
    # #  or invalid issue, but that should be checked
    done = grond.feed_issue(issuedate, do_renew=True)
    sys.exit()


print("I see nothing to do.")
