# Path: src/page_object_pattern/home_page.py
# class HomePage(BasePage):
#     """
#     Home page
#     """
#     SEARCH_CONTAINER = 'searchInput'
#     SEARCH_BUTTON = 'searchButton'
#     CREATE_ACCOUNT = 'pt-createaccount'
#     LOGIN = 'pt-login'
# 
#     def set_search_query(self, query: str):
#         """
#         Search for a string
#         :param query: string you are looking for
#         """
#         self._driver.find_element_by_id(HomePage.SEARCH_CONTAINER).send_keys(query)
# 
#     def check_search(self):
#         """
#         Check search button is visible
#         """
#         return self._driver.find_element_by_id(HomePage.SEARCH_BUTTON).is_displayed()
# 
#     def search(self):
#         """
#         Click on search button
#         """
#         self._driver.find_element_by_id(HomePage.SEARCH_BUTTON).click()
# 
#     def check_login(self):
#         """
#         Check Login button is visible
#         """
#         return self._driver.find_element_by_id(HomePage.LOGIN).is_displayed()
# 
#     def login(self):
#         """
#         Press login button
#         """
#         self._driver.find_element_by_id(HomePage.LOGIN).click()
# 
#     def check_create_account(self):
#         """
#         Check create account button available
#         """
#         return self._driver.find_element_by_id(HomePage.CREATE_ACCOUNT).is_displayed()
# 
#     def create_account(self):
#         """
#         Press create account button
#         """
#         self._driver.find_element_by_id(HomePage.CREATE_ACCOUNT).click()
# 
# Path: src/utils.py
# def get_selenium_driver(browser_name: str) -> Union[Chrome, Firefox]:
#     """
#     Return the same instance to the Selenium driver.
# 
#     :param browser_name: the name of the browser: chrome or mozilla
#     :type browser_name: str
#     :return: an instance of the required driver.
#     :rtype: Union[Chrome, Mozilla]
#     """
#     if browser_name.upper() == 'CHROME':
#         return SingletonFactory.build(Chrome)
# 
#     elif browser_name.upper() == 'Mozilla':
#         return SingletonFactory.build(Firefox)
# 
#     else:
#         raise NotImplementedError
# 
# def get_process_info_spike(process):
#     """
# 
#     :param process: get system information like memory consumption, cpu usage
#     :return:
#     """
#     for process_id in psutil.pids():
#         p = psutil.Process(process_id)
#         if p.name() == 'pycharm':
#             if process == 'memory':
#                 return p.memory_percent()
#             elif process == 'cpu':
#                 return p.cpu_percent(interval=1)
#             else:
#                 return p


from src.page_object_pattern.home_page import HomePage
from src.utils import get_selenium_driver, get_process_info_spike
"""
Define a one to many relationship between objects.
If the state of an object is changed, all the others are notified
"""


class Process:
    """
     - is aware of observers
     - send a notification to the observers if its state is changed
    """

    def __init__(self):
        self._observers = set()

    def subscribe(self, who):
        self._observers.add(who)

    def un_subscribe(self, who):
        self._observers.discard(who)

    def dispatch(self,test_func, message):
        for subscriber in self._observers:
            if get_process_info_spike(message) >= 50:
                subscriber.update(test_func, 'CPU usage warning: ' + str(get_process_info_spike(message)))
            else:

# Path: src/page_object_pattern/home_page.py
# class HomePage(BasePage):
#     """
#     Home page
#     """
#     SEARCH_CONTAINER = 'searchInput'
#     SEARCH_BUTTON = 'searchButton'
#     CREATE_ACCOUNT = 'pt-createaccount'
#     LOGIN = 'pt-login'
# 
#     def set_search_query(self, query: str):
#         """
#         Search for a string
#         :param query: string you are looking for
#         """
#         self._driver.find_element_by_id(HomePage.SEARCH_CONTAINER).send_keys(query)
# 
#     def check_search(self):
#         """
#         Check search button is visible
#         """
#         return self._driver.find_element_by_id(HomePage.SEARCH_BUTTON).is_displayed()
# 
#     def search(self):
#         """
#         Click on search button
#         """
#         self._driver.find_element_by_id(HomePage.SEARCH_BUTTON).click()
# 
#     def check_login(self):
#         """
#         Check Login button is visible
#         """
#         return self._driver.find_element_by_id(HomePage.LOGIN).is_displayed()
# 
#     def login(self):
#         """
#         Press login button
#         """
#         self._driver.find_element_by_id(HomePage.LOGIN).click()
# 
#     def check_create_account(self):
#         """
#         Check create account button available
#         """
#         return self._driver.find_element_by_id(HomePage.CREATE_ACCOUNT).is_displayed()
# 
#     def create_account(self):
#         """
#         Press create account button
#         """
#         self._driver.find_element_by_id(HomePage.CREATE_ACCOUNT).click()
# 
# Path: src/utils.py
# def get_selenium_driver(browser_name: str) -> Union[Chrome, Firefox]:
#     """
#     Return the same instance to the Selenium driver.
# 
#     :param browser_name: the name of the browser: chrome or mozilla
#     :type browser_name: str
#     :return: an instance of the required driver.
#     :rtype: Union[Chrome, Mozilla]
#     """
#     if browser_name.upper() == 'CHROME':
#         return SingletonFactory.build(Chrome)
# 
#     elif browser_name.upper() == 'Mozilla':
#         return SingletonFactory.build(Firefox)
# 
#     else:
#         raise NotImplementedError
# 
# def get_process_info_spike(process):
#     """
# 
#     :param process: get system information like memory consumption, cpu usage
#     :return:
#     """
#     for process_id in psutil.pids():
#         p = psutil.Process(process_id)
#         if p.name() == 'pycharm':
#             if process == 'memory':
#                 return p.memory_percent()
#             elif process == 'cpu':
#                 return p.cpu_percent(interval=1)
#             else:
#                 return p


# Path: src/observer/testwikisearch.py
from src.page_object_pattern.home_page import HomePage
from src.utils import get_selenium_driver, get_process_info_spike

"""
Define a one to many relationship between objects.
If the state of an object is changed, all the others are notified
"""


class Process:
    """
     - is aware of observers
     - send a notification to the observers if its state is changed
    """

    def __init__(self):
        self._observers = set()

    def subscribe(self, who):
        self._observers.add(who)

    def un_subscribe(self, who):
        self._observers.discard(who)

    def dispatch(self,test_func, message):
        for subscriber in self._observers:
            if get_process_info_spike(message) >= 50:
                subscriber.update(test_func, 'CPU usage warning: ' + str(get_process_info_spike(message)))
            else:

                subscriber.update(test_func, 'CPU usage normal: ' + str(get_process_info_spike(message)))