from input_helpers import ask_user
from validators import BooleanValidator, ListValidator, NonEmptyStringValidator, RangeValidator
from prompt_toolkit.completion import WordCompleter


def efs_config():
    options = ['existing', 'new']
    pick = ask_user(
        question='Create new file system, or use existing? (TAB to scroll)[existing]: ',
        validator=ListValidator(options),
        completer=WordCompleter(options, ignore_case=True),
        default='existing'
    )
    if pick == 'existing':
        return existing_efs_config()
    else:
        return managed_efs_config()


def existing_efs_config():
    file_system_id = ask_user(
        question='Enter file file system ID: ',
        validator=NonEmptyStringValidator()
    )
    return {
        'EfsSettings': {
            'FileSystemId': file_system_id
        }
    }


def managed_efs_config():
    print('\n\nCreating new ParallelCluster managed EFS file system\n')

    deletion_policies = ['Delete', 'Retain', 'Snapshot']
    throughput_modes = ['bursting', 'provisioned']
    performance_modes = ['generalPurpose', 'maxIO']

    encrypted = ask_user(
        question='Should the volume be encrypted? (True/False)[False]: ',
        validator=BooleanValidator(),
        default='False'
    )

    kms_key_id = None
    if encrypted == 'True':
        kms_key_id = ask_user(
            question='Enter KMS key ID: ',
            validator=NonEmptyStringValidator()
        )

    in_transit = ask_user(
        question='Should encryption be in transit? (True/False)[False]: ',
        validator=BooleanValidator(),
        default='False'
    )
    iam_auth = ask_user(
        question='Should IAM be authorized? (True/False)[False]: ',
        validator=BooleanValidator(),
        default='False'
    )
    performance_mode = ask_user(
        question='Performance mode (TAB to scroll)[generalPurpose]: ',
        validator=ListValidator(performance_modes),
        completer=WordCompleter(performance_modes, ignore_case=True),
        default='generalPurpose'
    )
    throughput_mode = ask_user(
        question='Enter throughput mode (TAB to scroll)[bursting]: ',
        validator=ListValidator(throughput_modes),
        completer=WordCompleter(throughput_modes, ignore_case=True),
        default='bursting'
    )

    throughput = None
    if throughput_mode == 'provisioned':
        throughput = ask_user(
            question='Enter provisioned throughput (1-1024): ',
            validator=RangeValidator(1, 1024)
        )
    deletion_policy = ask_user(
        question='Enter deletion policy (TAB to scroll)([Delete]: ',
        validator=ListValidator(deletion_policies),
        completer=WordCompleter(deletion_policies, ignore_case=True),
        default='Delete'
    )

    return {
        'EfsSettings': {
            'Encrypted': encrypted,
            'KmsKeyId': kms_key_id,
            'EncryptionInTransit': in_transit,
            'IamAuthorization': iam_auth,
            'PerformanceMode': performance_mode,
            'ThroughputMode': throughput_mode,
            'ProvisionedThroughput': throughput,
            'DeletionPolicy': deletion_policy
        }
    }
