# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from marionette_driver import expected, By, Wait
from marionette_driver.marionette import Actions

from gaiatest.apps.base import Base


class System(Base):

    # status bar
    _status_bar_locator = (By.ID, 'statusbar')
    _gripper_locator = (By.ID, 'tray-invisible-gripper')
    _utility_tray_locator = (By.ID, 'utility-tray')

    _system_banner_locator = (By.CSS_SELECTOR, '#screen > gaia-toast.banner')
    _notification_toaster_locator = (By.ID, 'notification-toaster')
    _update_manager_toaster_locator = (By.ID, 'update-manager-toaster')

    _software_home_button_locator = (By.ID, 'software-home-button')

    _software_buttons_locator = (By.ID, 'software-buttons')
    _screen_locator = (By.ID, 'screen')

    _search_bar_locator = (By.CSS_SELECTOR, '[data-l10n-id="search-or-enter-address"]')

    @property
    def status_bar(self):
        self.marionette.switch_to_frame()
        from gaiatest.apps.system.regions.status_bar import StatusBar
        element = self.marionette.find_element(*self._status_bar_locator)
        return StatusBar(self.marionette, element)

    def wait_for_status_bar_displayed(self):
        Wait(self.marionette).until(expected.element_displayed(*self._status_bar_locator))

    # A lot of tests, like mail or call received, need a longer timeout here
    def wait_for_notification_toaster_displayed(self, timeout=30, message=None, for_app=None):
        self.marionette.switch_to_frame()
        el = self.marionette.find_element(*self._notification_toaster_locator)
        Wait(self.marionette, timeout).until(expected.element_displayed(el), message=message)
        if for_app is not None:
            Wait(self.marionette, timeout).until(lambda m:
                for_app in el.get_attribute('data-notification-id'), message=message)

    def wait_for_notification_toaster_not_displayed(self, timeout=10):
        self.marionette.switch_to_frame()
        Wait(self.marionette, timeout).until(
            expected.element_not_displayed(*self._notification_toaster_locator))

    def wait_for_system_banner_displayed(self):
        self.marionette.switch_to_frame()
        Wait(self.marionette).until(expected.element_displayed(*self._system_banner_locator))

    def wait_for_system_banner_not_displayed(self):
        self.marionette.switch_to_frame()
        Wait(self.marionette).until(
            expected.element_not_displayed(*self._system_banner_locator))

    def wait_for_software_home_button_displayed(self, timeout=10, message=None):
        self.marionette.switch_to_frame()
        Wait(self.marionette, timeout=timeout).until(
            expected.element_displayed(*self._software_home_button_locator), message)

    def wait_for_software_home_button_not_displayed(self, timeout=10):
        self.marionette.switch_to_frame()
        Wait(self.marionette, timeout=timeout).until(
            expected.element_not_displayed(*self._software_home_button_locator))

    def open_utility_tray(self):
        self.marionette.switch_to_frame()
        body = self.marionette.find_element(By.TAG_NAME, 'body')
        gripper = self.marionette.find_element(*self._gripper_locator)
        gripper_x = int(gripper.rect['width']/2)
        gripper_y = int(gripper.rect['y'])
        gripper_y_end = int(body.rect['height'])
        Actions(self.marionette).flick(gripper, gripper_x, gripper_y, gripper_x, gripper_y_end).perform()

        from gaiatest.apps.system.regions.utility_tray import UtilityTray
        utility_tray = UtilityTray(self.marionette)
        utility_tray.wait_for_dropped_down()
        return utility_tray

    def tap(self, x=None, y=None):
        self.marionette.switch_to_frame()
        self.marionette.find_element(*self._screen_locator).tap(x, y)

    # As we have trouble disabling the app update toaster these methods
    # may be used to wait for it when we know it may interfere
    @property
    def is_app_update_notification_displayed(self):
        self.marionette.switch_to_frame()
        update_manager_toaster = self.marionette.find_element(*self._update_manager_toaster_locator)
        return update_manager_toaster.location['y'] > (0 - update_manager_toaster.size['height'])

    def wait_for_app_update_to_clear(self):
        self.marionette.switch_to_frame()
        element = self.marionette.find_element(*self._update_manager_toaster_locator)
        Wait(self.marionette).until(lambda m: element.location['y'] == (0 - element.size['height']))

    @property
    def screen_width(self):
        self.marionette.switch_to_frame()
        return self.marionette.find_element(*self._screen_locator).rect['width']

    @property
    def screen_height_without_software_home_button(self):
        self.marionette.switch_to_frame()
        return self.marionette.find_element(*self._screen_locator).rect['height'] - self.software_buttons_height

    @property
    def software_buttons_height(self):
        """
        Gets the height of the software buttons container on the screen.
        Always returns 0 if software buttons are not displayed.
        """
        self.marionette.switch_to_frame()
        if 'software-button-enabled' in self.marionette.find_element(*self._screen_locator).get_attribute('class'):
            return self.marionette.find_element(*self._software_buttons_locator).size['height']
        else:
            return 0

    @property
    def wallpaper_properties(self):
        # The wallpaper returns a blob url and a gradient
        self.marionette.switch_to_frame()
        screen = self.marionette.find_element(*self._screen_locator)
        return screen.value_of_css_property('background-image')

    def tap_search_bar(self):
        self.marionette.switch_to_frame()
        self.marionette.find_element(*self._search_bar_locator).tap()

        from gaiatest.apps.system.regions.search_panel import SearchPanel
        return SearchPanel(self.marionette)
