import copy


def set_fio_dicts(job_name, fio_job_dict, fio_configmap_dict, mode='fs'):
    """
    Set correct names for jobs, targets, configs and volumes in fio
    dictionaries.

    Args:
        job_name (str): fio job name
        fio_job_dict (dict): instance of fio_job_dict fixture
        fio_configmap_dict (dict): instance of fio_configmap_dict fixture
        mode (str): block or fs

    Returns:
        tupple: Edited fio_job_dict and fio_configmap_dict

    """
    config_name = f"{job_name}-config"
    volume_name = f"{config_name}-vol"
    target_name = f"{job_name}-target"

    fio_configmap_dict['metadata']['name'] = config_name
    fio_job_dict['metadata']['name'] = job_name
    fio_job_dict['spec']['template']['metadata']['name'] = job_name

    job_spec = fio_job_dict['spec']['template']['spec']
    job_spec['volumes'][0]['name'] = target_name
    job_spec['volumes'][0]['persistentVolumeClaim']['claimName'] = target_name
    job_spec['volumes'][1]['name'] = volume_name
    job_spec['volumes'][1]['configMap']['name'] = config_name

    job_spec['containers'][0]['volumeMounts'][0]['name'] = target_name
    job_spec['containers'][0]['volumeMounts'][1]['name'] = volume_name

    if mode == 'block':
        # set correct path for fio volumes
        fio_job_dict_block = copy.deepcopy(fio_job_dict)
        job_spec = fio_job_dict_block['spec']['template']['spec']
        job_spec['containers'][0]['volumeDevices'] = []
        job_spec['containers'][0]['volumeDevices'].append(
            job_spec['containers'][0]['volumeMounts'].pop(0)
        )
        block_path = '/dev/rbdblock'
        # set correct path for fio volumes
        job_spec['containers'][0]['volumeDevices'][0]['devicePath'] = block_path
        try:
            job_spec['containers'][0]['volumeDevices'][0].pop('mountPath')
        except KeyError:
            # mountPath key might be missing from previous update of fio_job_dict
            pass

        return fio_job_dict_block, fio_configmap_dict

    return fio_job_dict, fio_configmap_dict