from input_helpers import ask_user
from validators import IntegerValidator


def slurm_config():
    print("\n\nQueue configuration:\n")
    num_queues = ask_user(
        question='Number of queues [1]: ',
        validator=IntegerValidator(),
        default=1
    )

    queues = []
    for i in range(int(num_queues)):
        queues.append(queue_config(i))

    return {
        'Scheduling': {
            'Scheduler': 'slurm',
            'SlurmQueues': queues
        }
    }


def queue_config(i):
    name = ask_user(
        question=f'Name of queue no.{i+1} [queue{i+1}]: ',
        default=f'queue{i+1}'
    )
    comp_resource = compute_resource_config(name)
    return {
        'Name': name,
        'ComputeResources': [comp_resource],
    }


def compute_resource_config(queue_name):
    instance_type = ask_user(
        question=f'Instance type for queue {queue_name} [t2.micro]: ',
        default='t2.micro'
    )
    min_instances = ask_user(
        question='Minimum instance count [0]: ',
        validator=IntegerValidator(),
        default=0
    )
    max_instances = ask_user(
        question='Maximum instance count [10]: ',
        validator=IntegerValidator(),
        default=10
    )
    return {
        'Name': instance_type.replace('.', ''),
        'Instances': [
            {
                'InstanceType': instance_type
            }
        ],
        'Megacount': None,
        'MinCount': int(min_instances),
        'MaxCount': int(max_instances)
    }
