from wrapper.pcluster_wrapper import ParallelClusterWrapper
from validators import RangeValidator
from input_helpers import ask_user, wait_for_enter
from menu.config_menu import config_menu
import os

main_menu_options = ['List clusters', 'Create new cluster', 'Find cluster', 'Delete cluster',
                     'Create new cluster config', 'Quit']

pc_wrapper = ParallelClusterWrapper()


def print_main_menu_options():
    # Clear the console
    os.system('cls' if os.name == 'nt' else 'clear')

    # Introduction
    print("Welcome to the cluster management CLI!\n")
    print("Choose one of the following options by selecting the associated number:\n")

    # Print options
    for i, option in enumerate(main_menu_options, 1):
        print(f"[{i}] {option}")
    print("\n")


def main_menu():
    while True:
        print_main_menu_options()

        operation = ask_user(
            question='Choose an option: ',
            validator=RangeValidator(1, len(main_menu_options)),
        )

        if operation == '1':
            pc_wrapper.list_cluster()
        elif operation == '2':
            cluster_name = ask_user('Enter the name of the cluster: ')
            cluster_file = ask_user(
                'Enter the name of the cluster config file: ')
            pc_wrapper.create_cluster(cluster_name, cluster_file)
        elif operation == '3':
            cluster_name = ask_user('Enter the name of the cluster: ')
            pc_wrapper.find_cluster(cluster_name)
        elif operation == '4':
            cluster_name = ask_user('Enter the name of the cluster: ')
            pc_wrapper.delete_cluster(cluster_name)
        elif operation == '5':
            config_menu()
        elif operation == '6':
            break
        else:
            print('ERROR: Invalid option selected!')
        wait_for_enter()
