import typer
import RegularExpressionParser.Parser as Parser
import Crawler.Main as Main
import API.Functions as API
import os
import json
from dotenv import load_dotenv

app = typer.Typer()


@app.command()
def main_page():
    print("Welcome to use goodReads scraper!\n"
          "This is main page\n"
          "Usage:\n"
          "scrape: scrape goodRead and load data to database\n"
          "get: find a document for a book or an author from database\n"
          "put: create or update a document in database\n"
          "post: add one or multiple documents of book or author in database \n"
          "delete: delete certain document from database\n"
          "export: export a certain collection from database\n"
          "exit: end the program")
    order = input()
    if order == "scrape":
        scrape()
    elif order == "get":
        get()
    elif order == "put":
        put()
    elif order == "post":
        post()
    elif order == "delete":
        delete()
    elif order == "export":
        export()
    elif order == "exit":
        exit()
    else:
        print("Unknown command")
    main_page()


def scrape():
    """
    Function used to create scrape request to Server
    Only for assignment 2.0, for GET scrape, find it in GET in SimplerServer.py
    """
    print('please enter URL of the book')
    url = input()
    print('please enter the number of books you want to crawl')
    book_num = input()
    if not book_num.isdigit():
        print("max book num must be an integer")
        main_page()
    book_num = int(book_num)
    print('please enter the number of authors you want to crawl')
    author_num = input()
    if not author_num.isdigit():
        print("max author num must be an integer")
        main_page()
    author_num = int(author_num)
    print('Request received, start scraping')
    if not Parser.check_if_address_valid(url):
        print("Error: invalid URL")
        main_page()
    elif book_num >= 2000 or author_num >= 2000:
        print("Error, the number of book and author should be lower than 2000")
        main_page()
    else:
        if book_num > 200 or author_num > 50:
            print("Warning, maximum of the number of books or authors is large")
        Main.scrape_api(url, book_num, author_num)
        main_page()


def get():
    print("Please enter your query:")
    query = input()
    result = API.get(query)
    typer.echo(result)
    main_page()


def put():
    print("Please enter your query:")
    query = input()
    print("Please select one way to load update info: input or read:")
    load = input()
    if load == "input":
        print("Please enter json data:")
        string = input()
        try:
            data = json.loads(string)
        except ValueError as err:
            print(err)
            main_page()
    elif load == "read":
        load_dotenv()
        file_root = os.getenv('FILE_ROOT')
        print("Please enter the name of file which is in the file root directory:")
        file_path = r"" + file_root + input()
        if os.path.isfile(file_path):
            with open(file_path, "r") as file:
                try:
                    data = json.load(file)
                except ValueError as err:
                    print(err)
                    main_page()
        else:
            print("Error: file not exist")
            main_page()
    else:
        print("Unknown command")
        main_page()
    result = API.put(query, data)
    typer.echo(result)
    main_page()


def post():
    print("Please enter your query:")
    query = input()
    load_dotenv()
    file_root = os.getenv('FILE_ROOT')
    print("Please enter the name of file which is in the file root directory:")
    file_path = r"" + file_root + input()
    if os.path.isfile(file_path):
        with open(file_path, "r") as file:
            try:
                data = json.load(file)
            except ValueError as err:
                print(err)
                main_page()
    else:
        print("Error: file not exist")
        main_page()
    result = API.post(query, data)
    typer.echo(result)
    main_page()


def delete():
    print("Please enter your query:")
    query = input()
    result = API.delete(query)
    typer.echo(result)
    main_page()


def export():
    print("Which collection do you want to export? books or authors?")
    collection = input()
    if collection == "books":
        json = API.get("book")
        load_dotenv()
        file_root = os.getenv('FILE_ROOT')
        with open(file_root + "books" + ".json", "w") as output:
            output.write(json)
        print("books.json is stored at " + file_root)
        main_page()
    elif collection == "authors":
        json = API.get("author")
        load_dotenv()
        file_root = os.getenv('FILE_ROOT')
        with open(file_root + "authors" + ".json", "w") as output:
            output.write(json)
        print("authors.json is stored at " + file_root)
        main_page()
    elif collection == "back":
        main_page()
    else:
        print("Unknown collection")
        main_page()


# def goodbye(name: str, formal: bool = False):
#     """ function2 """
#     if formal:
#         typer.echo(f"Goodbye Ms. {name}. Have a good day.")
#     else:
#         typer.echo(f"Bye {name}!")


if __name__ == "__main__":
    app()