from lib_python import execution, steps, variables
from lib_python.baseObjects.AppiumObject import AppiumObject
import re


def check_error_screen_crash_code(params):
    expected_hashcode = params["hashcode"]
    found_hashcode = AppiumObject(variables.driver, "crashlib_crashCode").get_text()

    if expected_hashcode == found_hashcode:
        execution.passed()
    else:
        execution.error("Expected hashcode: {0}, but found {1} instead.".format(expected_hashcode, found_hashcode))

steps.new_step("ErrorScreen - Check that current screen displays error hash code: {hashcode}", check_error_screen_crash_code)


def check_error_screen_native_pattern():
    pattern = re.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{8}-[0-9a-f]{8}")

    found_id = AppiumObject(variables.driver, "crashlib_crashCode").get_text()

    if pattern.match(found_id) is None:
        execution.error("Reported crash ID does not match the required pattern.")

    execution.passed()

steps.new_step("ErrorScreen - Check that current screen displays native crash ID", check_error_screen_native_pattern)
