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


def ebs_config():
    options = ['existing', 'new']
    pick = ask_user(
        question='Create new volume, or use existing? (TAB to scroll)[existing]: ',
        validator=ListValidator(options),
        completer=WordCompleter(options, ignore_case=True),
        default='existing'
    )
    if pick == 'existing':
        return existing_ebs_config()
    else:
        return managed_ebs_config()


def existing_ebs_config():
    volume_id = ask_user(
        question='Enter volume ID: ',
        validator=NonEmptyStringValidator()
    )
    return {
        'EbsSettings': {
            'VolumeId': volume_id
        }
    }


def managed_ebs_config():
    print('\n\nCreating new ParallelCluster managed EBS volume\n')

    volume_types = ['gp2', 'gp3', 'io1', 'io2', 'sc1', 'st1', 'standard']
    deletion_policies = ['Delete', 'Retain', 'Snapshot']

    volume_type = ask_user(
        question='Enter volume type (TAB to scroll)[gp3]: ',
        validator=ListValidator(volume_types),
        completer=WordCompleter(volume_types, ignore_case=True),
        default='gp3'
    )

    iops = None
    throughput = None
    if volume_type == 'gp3':
        iops = ask_user(
            question='Enter Iops (3000-16000)[3000]: ',
            validator=RangeValidator(3000, 16000),
            default=3000
        )
        throughput = ask_user(
            question='Enter throughput (125-1000)[125]: ',
            validator=RangeValidator(125, 1000),
            default=125
        )
    elif volume_type in ['io1', 'io2']:
        iops = ask_user(
            question='Enter Iops (100-64000)[100]: ',
            validator=RangeValidator(100, 64000),
            default=100
        )

    size = ask_user(
        question='Enter Size in GB [35]: ',
        validator=IntegerValidator(),
        default=35
    )
    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()
        )

    snapshot_id = ask_user(
        question='Enter snapshot ID [None]: ',
    )
    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'
    )
    raid = raid_config()

    return {
        'EbsSettings': {
            'VolumeType': volume_type,
            'Iops': iops,
            'Size': size,
            'Encrypted': encrypted,
            'KmsKeyId': kms_key_id,
            'SnapshotId': snapshot_id,
            'Throughput': throughput,
            'DeletionPolicy': deletion_policy,
            'Raid': raid
        }
    }


def raid_config():
    raid_type = ask_user(
        question='Enter raid type (0,1)[None]: ',
        validator=RangeValidator(0, 1)
    )
    if raid_type is None:
        return None

    raid_volumes = ask_user(
        question='Enter number of volumes (2-5)[2]: ',
        validator=RangeValidator(2, 5),
        default=2
    )
    return {
        'type': raid_type,
        'NumberOfVolumes': raid_volumes
    }
