#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2024 Jacob Babor <jacob@babor.tech>
#
# Distributed under terms of the MIT license.

from csv import DictReader
import datetime
import glob
import json
import os
import re
import subprocess
import textwrap
import time

# Config globals
rate = float(41.00)
timesheet = {}

# First, acquire a CSV to look at
csv = glob.glob('*.csv')
if not csv:
    print('No csv found!')
    quit()

# Then, open that CSV and collect its data, storing it in timesheet
with open(csv[0], 'r') as csv:
    items = DictReader(csv)
    for row in items:
        issue = row["Issue Key"]
        date = datetime.datetime.strptime(row["Work date"], '%Y-%m-%d %H:%M').strftime('%Y-%m-%d')
        hours = float(row["Hours"])

        if not date in timesheet.keys():
            timesheet[date] = {}

        if not issue in timesheet[date].keys():
            timesheet[date][issue] = 0

        timesheet[date][issue] += hours

for k,v in timesheet.items():
    print(k)
    subprocess.run(["ydotool", "type", f"{k} - Team Meetings\t0.25\t{rate}\t"])
    subprocess.run(["ydotool", "type", "\n"])
    for i in range(1,4):
        subprocess.run(["ydotool", "type", f"{k} - RESERVED {i}\t0.00\t{rate}\t"])
        subprocess.run(["ydotool", "type", "\n"])
    for i,h in v.items():
        print(f'  { i } - { h }')
        # Now actually do the work
        subprocess.run(["ydotool", "type", f"{k} - {i}\t{round(h,2)}\t{rate}\t"])
        time.sleep(0.10)
        subprocess.run(["ydotool", "type", "\n"])
        time.sleep(0.10)