# Path: src/tests/behave/api/features/common/utils.py
# def get_entity_list_container_field(name):
#     """Returns the container field used in list responses
# 
#     GET /active_computers       -> {"items": [...]}
#     GET /jobs                   -> {"jobs": [...]}
#     """
#     if name == "active_computers":
#         return "items"
#     elif name == "inventory_computers":
#         return "items"
#     elif name == "rpc_tasks":
#         return "tasks"
#     elif name == "rpc_jobs":
#         return "jobs"
#     return name
# 
# def get_entity_id_field(name):
#     """Returns the id field used for entities in list responses
# 
#     GET /active_computers       -> {"items": [{"mercury_id": "...", ...}}]}
#     GET /jobs                   -> {"jobs": [{"job_id": "...", ...}}]}
#     """
#     if name == "active_computers":
#         return "mercury_id"
#     elif name == "inventory_computers":
#         return "mercury_id"
#     elif name == "rpc_tasks":
#         return "task_id"
#     elif name == "rpc_jobs":
#         return "job_id"
#     return name
# 
# def read_json_from_file(filename, location):
#     filename = "{0}/{1}".format(location, filename)
#     with open(filename) as file:
#         data = json.loads(file.read())
#     return data
# 
# def wait_for_not_none(func, *args, timeout=50):
#     value = func(*args)
#     while value == None:
#         if timeout <= 0:
#             break
#         time.sleep(5)
#         timeout -= 1
#         value = func(*args)
#     return value


import json
import time
import operator
from functools import reduce
from behave import given, when, step
from src.tests.behave.api.features.common.utils import (
    get_entity_list_container_field,
    get_entity_id_field,
    read_json_from_file,
    wait_for_not_none,
)



@given(
    "I have job injection details in {filename} for creating jobs using the {service_name} api"
)
def step_i_have_job_injection_details_in_filename_for_creating_jobs_using_the_service_api(
    context, filename, service_name
):
    """
    :type context: behave.runner.Context
    :type service_name: str
    """
    location = context.json_location
    data = read_json_from_file(filename, location)

    context.services[service_name]["details"]["job_data"] = data


@given(
    "I have job injection details for a specific {device_service} device in {filename} for creating jobs using the {service_name} api"
)
def step_i_have_job_injection_details_for_specific_device_in_filename_for_creating_jobs_using_the_service_api(
    context, filename, service_name, device_service
):
    """
    :type context: behave.runner.Context
    :type service_name: str
    """
    location = context.json_location

# Path: src/tests/behave/api/features/common/utils.py
# def get_entity_list_container_field(name):
#     """Returns the container field used in list responses
# 
#     GET /active_computers       -> {"items": [...]}
#     GET /jobs                   -> {"jobs": [...]}
#     """
#     if name == "active_computers":
#         return "items"
#     elif name == "inventory_computers":
#         return "items"
#     elif name == "rpc_tasks":
#         return "tasks"
#     elif name == "rpc_jobs":
#         return "jobs"
#     return name
# 
# def get_entity_id_field(name):
#     """Returns the id field used for entities in list responses
# 
#     GET /active_computers       -> {"items": [{"mercury_id": "...", ...}}]}
#     GET /jobs                   -> {"jobs": [{"job_id": "...", ...}}]}
#     """
#     if name == "active_computers":
#         return "mercury_id"
#     elif name == "inventory_computers":
#         return "mercury_id"
#     elif name == "rpc_tasks":
#         return "task_id"
#     elif name == "rpc_jobs":
#         return "job_id"
#     return name
# 
# def read_json_from_file(filename, location):
#     filename = "{0}/{1}".format(location, filename)
#     with open(filename) as file:
#         data = json.loads(file.read())
#     return data
# 
# def wait_for_not_none(func, *args, timeout=50):
#     value = func(*args)
#     while value == None:
#         if timeout <= 0:
#             break
#         time.sleep(5)
#         timeout -= 1
#         value = func(*args)
#     return value


# Path: src/tests/behave/api/features/steps/inject.py
import json
import time
import operator
from functools import reduce
from behave import given, when, step
from src.tests.behave.api.features.common.utils import (
    get_entity_list_container_field,
    get_entity_id_field,
    read_json_from_file,
    wait_for_not_none,
)




@given(
    "I have job injection details in {filename} for creating jobs using the {service_name} api"
)
def step_i_have_job_injection_details_in_filename_for_creating_jobs_using_the_service_api(
    context, filename, service_name
):
    """
    :type context: behave.runner.Context
    :type service_name: str
    """
    location = context.json_location
    data = read_json_from_file(filename, location)

    context.services[service_name]["details"]["job_data"] = data


@given(
    "I have job injection details for a specific {device_service} device in {filename} for creating jobs using the {service_name} api"
)
def step_i_have_job_injection_details_for_specific_device_in_filename_for_creating_jobs_using_the_service_api(
    context, filename, service_name, device_service
):
    """
    :type context: behave.runner.Context
    :type service_name: str
    """
    location = context.json_location

    data = read_json_from_file(filename, location)