#!/usr/bin/env python

from sensei import Api
import argparse
import os
import json

CONF_PATH = os.path.expanduser("~/.sensei")


def get_conf():
    if os.path.isfile(CONF_PATH):
        return json.load(open(CONF_PATH))
    return {}

def write_conf(conf):
    json.dump(conf, open(CONF_PATH, "w"), indent=4)


if  __name__=='__main__':
    parser = argparse.ArgumentParser("Sensei Robotics CLI")
    parser.add_argument("--key", help="Set global API Key")
    parser.add_argument("--ls", metavar="PATH", nargs="?", const="/", help="List contents of given path")
    parser.add_argument("-i", "--interactive", action="store_true", help="Interactive mode")
    parser.add_argument("-o", "--out", metavar="DIR", default="sensei-data", help="Directory to write files to")
    parser.add_argument("-d", "--download", metavar="PATH", nargs="?", const="/", help="Download resource at given path."
                        " If no argument given, download everything. To download directory, use --recursive")
    parser.add_argument("-r", "--recursive", action="store_true", help="Recursively download files in given directory."
                        " Use in conjunction with --download")
    parser.add_argument("-w", "--overwrite", action="store_true", help="Overwrite existing files in destination dir")
    

    args = parser.parse_args()

    conf = get_conf()

    if args.key:
        print("Writing API key to", CONF_PATH)
        conf["key"] = args.key
        write_conf(conf)
        exit()
    
    destination_path = os.path.abspath(args.out)

    if not conf.get("key"):
        print("No API Key found")
        conf["key"] = input("Enter your API Key: ").strip()
        if not conf["key"]:
            exit()
        write_conf(conf)
        exit()
    
    api = Api(conf["key"], destination=destination_path)

    if args.interactive:
        print("Entering interactive mode. Type 'help' for help, 'exit' to exit")
        while True:
            try:
                cmd = input("sensei> ").strip()
            except EOFError:
                print()
                break
            if cmd in ("exit", "quit"):
                break
            elif cmd == "help":
                print("Commands:")
                print("  ls [PATH]          List contents of given path (default /)")
                print("  download PATH      Download file at given path")
                print("  rdownload PATH    Recursively download all files in given directory")
                print("  exit, quit        Exit interactive mode")
                print("  help              Show this help message")
            elif cmd.startswith("ls"):
                parts = cmd.split(maxsplit=1)
                path = parts[1] if len(parts) > 1 else "/"
                result_count = 0
                for e in api.iter_dirs(path):
                    print("(DIR)", e["path"])
                    result_count += 1
                for e in api.iter_files(path):
                    print(e["filename"])
                    result_count += 1
                
                print()
                print(result_count, "results")
            elif cmd.startswith("download"):
                parts = cmd.split(maxsplit=1)
                if len(parts) < 2:
                    print("Error: download command requires a PATH argument")
                    continue
                path = parts[1]
                print(f"Downloading file {path} into {destination_path}...")
                try:
                    api.download_file_from_path(path, overwrite=args.overwrite)
                except FileNotFoundError:
                    print("Warning: file not found")
            elif cmd.startswith("rdownload"):
                parts = cmd.split(maxsplit=1)
                if len(parts) < 2:
                    print("Error: rdownload command requires a PATH argument")
                    continue
                path = parts[1]
                print(f"Recursively downloading from {path} into {destination_path}...")
                file_found = api.recursive_download(path=path, overwrite=args.overwrite)
                if not file_found:
                    print("Warning: folder is empty or doesn't exist")
            elif cmd == "":
                continue
            else:
                print("Unknown command:", cmd)
        exit()

    if args.download:
        if args.recursive:
            print(f"Recursively downloading from {args.download} into {destination_path}...")
            file_found = api.recursive_download(path=args.download, overwrite=args.overwrite)
            if not file_found:
                print("Warning: folder is empty or doesn't exist")
        else:
            print(f"Downloading file {args.download} into {destination_path}...")
            try:
                api.download_file_from_path(args.download, overwrite=args.overwrite)
            except FileNotFoundError:
                print("Warning: file not found")
        exit()

    if args.ls:
        result_count = 0
        for e in api.iter_dirs(args.ls):
            print("(DIR)", e["path"])
            result_count += 1
        for e in api.iter_files(args.ls):
            print(e["filename"])
            result_count += 1
        
        print()
        print(result_count, "results")

        exit()

    parser.print_help()





