from input_helpers import ask_user
from validators import ListValidator, NonEmptyStringValidator
from prompt_toolkit.completion import WordCompleter
from wrapper.ec2_wrapper import EC2Wrapper
from wrapper.pcluster_wrapper import ParallelClusterWrapper

ec2 = EC2Wrapper()
pc = ParallelClusterWrapper()


def basic_config():
    print("Creation of new cluster configuration selected!\n")
    print("Allowed values are described in round brackets where required")
    print("Default values are described in square brackets where available\n\n")

    available_regions = pc.get_supported_regions()
    current_region = ec2.get_current_region()
    region = ask_user(
        question=f'Enter AWS region, in which to create the cluster (TAB to scroll)[{current_region}]: ',
        validator=ListValidator(available_regions),
        completer=WordCompleter(available_regions, ignore_case=True),
        default=current_region
    )

    available_key_pairs = ec2.get_all_key_pairs_in_region(region)
    if available_key_pairs:
        key_pair = ask_user(
            question=f'Pick a EC2 Key Pair for this cluster (TAB to scroll)[{available_key_pairs[0]}]: ',
            validator=ListValidator(available_key_pairs),
            completer=WordCompleter(available_key_pairs, ignore_case=True),
            default=available_key_pairs[0]
        )
    else:
        key_pair = ask_user(
            question='No EC2 keys found for this account in this region!\nEnter a name for the new key pair: ',
            validator=NonEmptyStringValidator(),
        )
        print(ec2.create_key_pair(key_pair, region))

    instance_type_head = ask_user(
        question='Enter compute instance type for head node [t2.micro]: ',
        default='t2.micro'
    )

    return {
        'Region': region,
        'Image': {
            'Os': 'ubuntu2004'
        },
        'HeadNode': {
            'InstanceType': instance_type_head,
            'Ssh': {
                'KeyName': key_pair
            }
        }
    }
