#!/bin/python3
import os
import sys
import subprocess
from datetime import datetime
import requests
import hashlib
from urllib.parse import urlparse
import signal
import argparse

# Default Constants
DEFAULT_WORKDIR = "/mnt/Apps/Syncthing/UserData/Aroy/Pictures/Reference/gallery-dl"
CONFIG = "gallery-dl.conf"
DATETIME = datetime.now().strftime("%F_%H-%M-%S")


# Handle Ctrl-C
def handle_sigint(signal_received, frame):
    print("\nProcess interrupted. Cleaning up...")
    sys.exit(1)


signal.signal(signal.SIGINT, handle_sigint)


# Function to check if lock file exists
def check_lock(site, workdir):
    lock_file = os.path.join(workdir, f".gallery-dl-{site}.lock")
    if os.path.isfile(lock_file):
        print(f"Lock file exists for {site}. Skipping...")
        return False
    else:
        open(lock_file, "w").close()
        return True


# Function to remove lock file
def remove_lock(site, workdir):
    lock_file = os.path.join(workdir, f".gallery-dl-{site}.lock")
    if os.path.isfile(lock_file):
        os.remove(lock_file)
        print(f"Removed lock file: {lock_file}")


# Function to send a push notification
def push_notice(site):
    title = f"Run done for {site}"
    data = f"Run done for {site}."
    headers = {"Title": title, "Priority": "normal", "Tags": "info"}
    requests.post(
        "https://ntfy.aroy-art.com/g11M8PwuAYBTDYCi", headers=headers, data=data
    )


# Generate descriptive sitename from URL
def generate_site_name_from_url(url):
    parsed_url = urlparse(url)
    hostname = parsed_url.hostname or "unknown-site"
    path_hash = hashlib.md5(parsed_url.path.encode("utf-8")).hexdigest()[:8]
    return f"{hostname}-{path_hash}"


# Function to execute gallery-dl command
def execute_gallery_dl(input_source, log_file, sitename, workdir, extra_args=None):
    if check_lock(sitename, workdir):
        log_path = os.path.join(workdir, "logs", sitename, f"{log_file}-{DATETIME}.log")
        command = [
            "/home/aroy/.local/bin/gallery-dl",
            "-c",
            os.path.join(workdir, CONFIG),
            "-d",
            workdir,
            "--mtime-from-date",
            "--write-metadata",
            "--write-log",
            log_path,
        ]
        if os.path.isfile(input_source):
            command.extend(["--input-file", input_source])
        else:
            command.append(input_source)

        if extra_args:
            for arg in extra_args:
                command.extend(["-o", arg])

        subprocess.run(command)
        remove_lock(sitename, workdir)
        push_notice(sitename)


# Main execution block
if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Run gallery-dl with specified input and options."
    )
    parser.add_argument(
        "-i", "--input_source", help="Sitename, URL, or input file", required=False
    )
    parser.add_argument(
        "--workdir", default=DEFAULT_WORKDIR, help="Override the working directory"
    )
    parser.add_argument(
        "-o",
        "--option",
        action="append",
        help="Additional arguments for gallery-dl, can be used multiple times",
    )

    args, remaining = parser.parse_known_args()

    # If input_source is not given with -i, treat the last argument as input_source
    input_source = (
        args.input_source
        if args.input_source
        else (remaining[-1] if remaining else None)
    )
    extra_args = args.option if args.option else []
    workdir = args.workdir

    if not input_source:
        print("Error: No input source provided.")
        sys.exit(1)

    if input_source.startswith("http://") or input_source.startswith("https://"):
        sitename = generate_site_name_from_url(input_source)
    elif os.path.isfile(input_source):
        sitename = os.path.splitext(os.path.basename(input_source))[0]
    else:
        potential_file = os.path.join(workdir, f"user-urls-{input_source.lower()}.txt")
        if os.path.isfile(potential_file):
            input_source = potential_file
            sitename = input_source.lower().split("-")[-1].split(".")[0]
        else:
            print(
                f"Error: Input '{input_source}' is not a valid URL, file, or predefined source."
            )
            sys.exit(1)

    execute_gallery_dl(input_source, f"log-{sitename}", sitename, workdir, extra_args)

    print(f"Done at {datetime.now().strftime('%F_%H-%M-%S')}")
    print("Purr Done!")