# This file is meant to sit in ~ for use from all shells.
from invoke import task
from pandas_ods_reader import read_ods
import pandas as pd

from datetime import datetime as dt
from datetime import timedelta
import os

HOME = os.path.expanduser('~')
DATE_FORMAT = '%Y-%m-%d'
TODAY = dt.now().strftime(DATE_FORMAT)

SCHOOL_DIR = f'{HOME}/School/Assignments'
ODS_FILE = f'{HOME}/School/Assignments/assignments.ods'
BIN = f'{HOME}/envs/invoke/bin'
STUDENTS = os.environ.get('STUDENTS').split('|')

@task
def open_assignments(c):
    print("LibreOffice does not like to be launched this way.")
    # c.run(f'libreoffice {ODS_FILE}', asynchronous=True)


@task
def dump_ods(c):
    df = read_ods(ODS_FILE)
    print(df)


@task
def print_week(c, person, monday):
    print(f'# {person}\'s Assignements for Week of {monday}')

    day = dt.strptime(monday, DATE_FORMAT)
    for i in range(0,5):
        print_day(c, person, day.strftime(DATE_FORMAT))
        day = day + timedelta(days=1)

@task
def md_week(c, person, monday):
    c.run(f'cd {SCHOOL_DIR};{BIN}/invoke print-week {person} {monday} > {SCHOOL_DIR}/{person}.{monday}.md')


@task
def show_all(c, monday):
    for person in STUDENTS:
        md_week(c, person, monday)
        c.run(f'codium {SCHOOL_DIR}/{person}.{monday}.md')

@task
def html_all(c, monday):
    for person in STUDENTS:
        md_week(c, person, monday)
        c.run(f'cd {SCHOOL_DIR};{BIN}/markdown_py {person}.{monday}.md > {SCHOOL_DIR}/{person}.{monday}.html')
        c.run(f'cat {SCHOOL_DIR}/style.html >> {SCHOOL_DIR}/{person}.{monday}.html')
        c.run(f'librewolf {SCHOOL_DIR}/{person}.{monday}.html', asynchronous=True)


def get_todays_assignments(df, date, person):
    mine = df[df['person'] == person]
    today = mine[mine['date'] == date]
    return today

@task
def missing_on_day(c, person, day):
    df = read_ods(ODS_FILE)
    today = get_todays_assignments(df, day, person)
    for subject in ['College Prep', 'Life Skills', 'Job Prep']:
        if not 'All Day Event' in today['subject'].values and not subject in today['subject'].values:
            print(f'Missing {subject} for {person} on {day}')

@task
def missing(c, monday, person=None):

    if not person:
        for person in STUDENTS:
            missing(c, monday, person)
    else:
        day = dt.strptime(monday, DATE_FORMAT)
        for i in range(0,5):
            missing_on_day(c, person, day.strftime(DATE_FORMAT))
            day = day + timedelta(days=1)

@task
def print_day(c, person, date):
    df = read_ods(ODS_FILE)
    # df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
    # today = df.loc(df['date'] >= '2026-01-12')
    mine = df[df['person'] == person]
    today = mine[mine['date'] == date]

    weekday = dt.strptime(date, DATE_FORMAT).strftime('%A')

    print()
    print()
    print(f"## {person}'s Assignments for {weekday}, {date}")

    for subject, row in today.groupby('subject'):
        print()
        print()
        print(f'### {row["subject"].values[0]}')
        print()
        for task in row['task'].values:
            print()
            print(f'- [ ] {task}')
        if(len(row['tip'].values) > 0):
            for tip in row['tip'].values:
                if tip:
                    print()
                    print(f'> Tip: {tip}')

        if(len(row['when_done'].values) > 0):
            for tip in row['when_done'].values:
                if tip:
                    print()
                    print(f'**When done**: {tip}')

@task
def files(c):
    c.run('dolphin .')


