import math
import json


def process_role_options(options):
    """
    Parse Lemur options and convert them to Vault parameter in json format.
    :param options: Lemur option dictionary
    :return: All needed parameters for Vault roles endpoint in json formatted string.
    """
    vault_params = {'allow_subdomains': 'true', 'allow_any_name': 'true'}

    if 'key_type' in options:
        vault_params['key_type'] = options['key_type'][:3].lower()
        vault_params['key_bits'] = options['key_type'][-4:]

    vault_params['key_usage'] = 'KeyAgreement'
    if 'extensions' in options and 'key_usage' in options['extensions']:
        if options['extensions']['key_usage'].digital_signature:
            vault_params['key_usage'] += ', DigitalSignature'
        if options['extensions']['key_usage'].key_encipherment:
            vault_params['key_usage'] += ', KeyEncipherment'

    ttl = -1

    if 'validity_end' in options and 'validity_start' in options:
        ttl = math.floor(abs(options['validity_end'] - options['validity_start']).total_seconds() / 3600)
    elif 'validity_years' in options:
        ttl = options['validity_years'] * 365 * 24

    if ttl > 0:
        vault_params['ttl'] = str(ttl) + 'h'
        vault_params['max_ttl'] = str(ttl) + 'h'

    return json.dumps(vault_params)