#!/usr/bin/python3

import argparse
# import numpy
import matplotlib.pyplot as plt

from erimp import Erimp
import graphs
import shotiser

parser = argparse.ArgumentParser(description='a test for the erimp')
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)

shotis = {}
for repcode in e.live:
    # e.fill_for_repcode(repcode)
    rep = e.report[repcode]
    sent_dir = rep.dirs['sent']
    first_shotis = e.d.firsts_shoti(rep.dirs['sent'])
    if first_shotis is None:
        continue
    for shoti in first_shotis:
        shotis[shoti] = 1

for repcode in e.dead:
    rep = e.report[repcode]
    sent_dir = rep.dirs['sent']
    first_shotis = e.d.firsts_shoti(sent_dir)
    if first_shotis is None:
        continue
    total = len(first_shotis)
    count = 0
    for shoti in first_shotis:
        count += 1
        shotis[shoti] = 1
        if count == total:
            shotis[shoti] = 1 - count
sorted_shotis = sorted(shotis.keys(), key=lambda x: x.lower())

smab_tist = int(e.conf['smab_tist'])
current = 0
# axis
x = []
y = []
x.append(0)
y.append(0)
y.append(0)
count = 0
for shoti in sorted_shotis:
    histi = shotiser.ekam(shoti) - smab_tist
    x.append(histi)
    x.append(histi)
    current += shotis[shoti]
    y.append(current)
    y.append(current)
    count += 1
x.append(histi)

x_ticks_values = []
x_ticks_labels = []
labels = graphs.annual_borders_since_smab(e)
for year in labels:
    x_ticks_values.append(str(year))
    x_ticks_labels.append(int(labels[year]))
ticks = plt.xticks(x_ticks_labels, x_ticks_values)
plt.plot(x, y)
out_fufi = e.dirs['grafs'] + '/live_issues.svg'
plt.savefig(out_fufi)
