#!/usr/bin/python3

import argparse

import dater
import filer

from erimp import Erimp
from mocla import Mocla

desc = 'class an issue'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('-v', action='store_true', help='raise verbosity')
parser.add_argument('-t', action='store_true', help='END tonight')
parser.add_argument('-f', type=str, help='use this issue fufi')
parser.add_argument('-c', action='store_true', help='cache selection screen')
parser.add_argument('-b', type=str, help='box as zahl:leiz')
parser.add_argument('-n', action='store_true', help='no assemble')
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)
mocla= Mocla(erimp=e, do_verbose=args.v)

box = args.b
if box is not None:
    from boxer import Boxer
    b = Boxer(e, box, 'reports')
    b.compile_reports()

do_asemb = True
if args.n:
    do_asemb = False
    do_cache = False

# # the caller should give it an issuedate but if it's not there
if e.given_issuedate is None:
    if args.f is None:
        print("no issuedate thus I need an issue file (can be brown)")
        quit()
    doc_fufi = args.f
    issuedate = filer.get_issuedate(doc_fufi)
    print(f"class uses {issuedate}.")
else:
    issuedate = e.given_issuedate

if e.given_repcode is not None:
    print("I run for " + e.given_repcode)
    # # doc_fufi for brown issue fufi
    mocla.run(e.given_repcode, issuedate, do_verbose=args.v,
              doc_fufi=args.f, do_asemb=do_asemb, do_cache=args.c)
    quit()

# # case without a repcode ...
e.given_issuedate = issuedate
for repcode in e.o.by_timely():
    if not e.report[repcode].is_active():
        if args.v:
            print("I box skip the inactive" + repcode)
        continue
    # print(repcode)
    if box is not None:
        if not b.check(repcode):
            if args.v:
                print("I box skip " + repcode)
            continue
    # # if tonight, stop when day is reached
    if args.t:
        if dater.tonight() == dater.today():
            break
    # # fixme: bremse needed!
    if args.v:
        print("I do " + repcode)
    mocla.run(repcode, issuedate, doc_fufi=args.f,
              do_verbose=args.v, do_asemb=do_asemb,
              do_cache=args.c)
