# 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,
)

@step("the response contains a {service_name} job_id")
def step_the_response_contains_a_job_id(context, service_name):
    """
    :type context: behave.runner.Context
    :type service_name: str
    """
    service_resp = context.services[service_name]["resp"]
    job_id = service_resp.json()["job_id"]
    context.check.assertIsInstance(job_id, str)
    context.services[service_name]["id"] = job_id
    # TODO is the job id valid?


@step(
    "the corresponding {service_name} job is completed with {state} state {tasks_service_name} tasks"
)
def step_the_corresponding_service_job_is_completed_and_successful(
    context, service_name, tasks_service_name, state
):
    """
    :type context: behave.runner.Context
    """
    service_client = context.services[service_name]["client"]
    service_resp = service_client.get(context.services[service_name]["id"])
    context.services[service_name]["resp"] = service_resp

    def get_time_completed(job_id):
        return service_client.get(job_id).json()["ttl_time_completed"]


# 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,
)


@step("the response contains a {service_name} job_id")
def step_the_response_contains_a_job_id(context, service_name):
    """
    :type context: behave.runner.Context
    :type service_name: str
    """
    service_resp = context.services[service_name]["resp"]
    job_id = service_resp.json()["job_id"]
    context.check.assertIsInstance(job_id, str)
    context.services[service_name]["id"] = job_id
    # TODO is the job id valid?


@step(
    "the corresponding {service_name} job is completed with {state} state {tasks_service_name} tasks"
)
def step_the_corresponding_service_job_is_completed_and_successful(
    context, service_name, tasks_service_name, state
):
    """
    :type context: behave.runner.Context
    """
    service_client = context.services[service_name]["client"]
    service_resp = service_client.get(context.services[service_name]["id"])
    context.services[service_name]["resp"] = service_resp

    def get_time_completed(job_id):
        return service_client.get(job_id).json()["ttl_time_completed"]


    completed = wait_for_not_none(