#!/usr/bin/env python3

import sys
import subprocess
from datetime import datetime
from dataclasses import dataclass


@dataclass
class CompiledPackage:
    name: str
    date: datetime
    compile_time: int

    def __repr__(self) -> str:
        name = self.name
        date = self.date.strftime("%a %b %d %H:%M:%S %Y")
        compile_time = get_readable_duration(self.compile_time)
        return f"CompiledPackage({name=}, {date=}, {compile_time=})"


def parse_time(time_line: str) -> int:
    """Parse a line that contains time info, return seconds"""
    time = 0
    words = time_line.split()

    if "hour" in words[1]:
        time += int(words[0]) * 60 * 60
    elif "minute" in words[1]:
        time += int(words[0]) * 60
    elif "second" in words[1]:
        time += int(words[0])

    try:
        if "second" in words[3]:
            time += int(words[2])
        elif "minute" in words[3]:
            time += int(words[2]) * 60
    except IndexError:
        pass

    return time


def get_readable_duration(total_seconds: int) -> str:
    """Get readable time duration string from total seconds"""
    hours, rem = divmod(total_seconds, 3600)
    minutes, rem = divmod(rem, 60)
    seconds = rem

    output = []
    if hours > 0:
        output.append(f"{hours} hour{'s' if hours > 1 else ''}")
    if minutes > 0:
        output.append(f"{minutes} minute{'s' if minutes > 1 else ''}")
    if seconds > 0:
        output.append(f"{seconds} second{'s' if seconds > 1 else ''}")

    if len(output) > 1:
        output.insert(-1, "and")

    return " ".join(output)


def get_packages() -> list[CompiledPackage]:
    """Obtain compilation times for every compiled package"""
    x = subprocess.run(
        "sudo genlop -nlt",
        stdout=subprocess.PIPE,
        shell=True
    )
    txt = x.stdout.decode("utf-8")

    # Cleanup the output
    txt = txt.replace("* packages merged:\n\n", "")
    txt = txt.replace("merge time: ", "")
    txt = txt.replace("and ", "")
    txt = txt.replace(".", "")

    # Remove indents
    clean_lines = [line.lstrip() for line in txt.split("\n")]
    txt = "\n".join(clean_lines)

    # Store (package name, date, compile time) for each package
    packages = []
    for pkg_txt in txt.split("\n\n"):
        if len(pkg_txt) == 0:
            continue

        pkg_lines = pkg_txt.split("\n")

        date, name = pkg_lines[0].split(" >>> ")
        time = parse_time(pkg_lines[1])
        date = datetime.strptime(date, "%a %b %d %H:%M:%S %Y")

        pkg = CompiledPackage(name, date, time)
        packages.append(pkg)

    return packages


def get_compile_time(package_amount: int) -> int:
    """Get compilation time of last n specified packages (seconds)"""
    packages = get_packages()
    last_packages = packages[-package_amount:]
    return sum(package.compile_time for package in last_packages)


if __name__ == "__main__":
    try:
        package_amt = int(sys.argv[1])
    except IndexError:
        print("Missing required argument: package amount")
    except TypeError:
        print("Argument must be a number (package amount)")
    else:
        time = get_compile_time(package_amt)
        print(get_readable_duration(time))