# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from page_sets.system_health import platforms
from page_sets.system_health import story_tags
from page_sets.system_health import system_health_story

from page_sets.login_helpers import dropbox_login
from page_sets.login_helpers import google_login

from page_sets.helpers import override_online

from telemetry.util import js_template


class _LoadingStory(system_health_story.SystemHealthStory):
  """Abstract base class for single-page System Health user stories."""
  ABSTRACT_STORY = True

  def __init__(self,
               story_set,
               take_memory_measurement,
               extra_browser_args=None):
    super(_LoadingStory, self).__init__(story_set, take_memory_measurement,
                                        extra_browser_args)
    self.script_to_evaluate_on_commit = override_online.ALWAYS_ONLINE

  @classmethod
  def GenerateStoryDescription(cls):
    return 'Load %s' % cls.URL


################################################################################
# Search and e-commerce.
################################################################################
# TODO(petrcermak): Split these into 'portal' and 'shopping' stories.


class LoadGoogleStory2018(_LoadingStory):
  NAME = 'load:search:google:2018'
  URL = 'https://www.google.co.uk/search?q=pepper'
  TAGS = [story_tags.YEAR_2018]


class LoadBaiduStory2018(_LoadingStory):
  NAME = 'load:search:baidu:2018'
  URL = 'https://www.baidu.com/s?word=google'
  TAGS = [story_tags.INTERNATIONAL, story_tags.YEAR_2018]


class LoadYahooStory2018(_LoadingStory):
  NAME = 'load:search:yahoo:2018'
  # Use additional parameter to bypass consent screen.
  URL = 'https://search.yahoo.com/search;_ylt=?p=google&_guc_consent_skip=1541794498'
  TAGS = [story_tags.YEAR_2018]


class LoadAmazonDesktopStory2018(_LoadingStory):
  NAME = 'load:search:amazon:2018'
  URL = 'https://www.amazon.com/s/?field-keywords=pixel'
  TAGS = [story_tags.YEAR_2018]
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY


class LoadAmazonMobileStory2019(_LoadingStory):
  NAME = 'load:search:amazon:2019'
  URL = 'https://www.amazon.com/s/?field-keywords=pixel'
  TAGS = [story_tags.YEAR_2019]
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY


class LoadTaobaoDesktopStory2018(_LoadingStory):
  NAME = 'load:search:taobao:2018'
  URL = 'https://world.taobao.com/'
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY
  TAGS = [story_tags.INTERNATIONAL, story_tags.YEAR_2018]


class LoadFlipkartDesktop2018(_LoadingStory):
  NAME = 'load:search:flipkart:2018'
  URL = 'https://www.flipkart.com/search?q=sneakers'
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY
  TAGS = [story_tags.INTERNATIONAL, story_tags.YEAR_2018]


class LoadTaobaoMobileStory2019(_LoadingStory):
  NAME = 'load:search:taobao:2019'
  URL = 'http://m.intl.taobao.com/'
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY
  TAGS = [story_tags.INTERNATIONAL, story_tags.YEAR_2019]


class LoadYandexStory2018(_LoadingStory):
  NAME = 'load:search:yandex:2018'
  URL = 'https://yandex.ru/touchsearch?text=science'
  TAGS = [story_tags.INTERNATIONAL, story_tags.YEAR_2018]


class LoadEbayStory2018(_LoadingStory):
  NAME = 'load:search:ebay:2018'
  URL = 'https://www.ebay.com/sch/i.html?_nkw=headphones'
  TAGS = [story_tags.YEAR_2018]


################################################################################
# Social networks.
################################################################################


class LoadTwitterMoibleStory2019(_LoadingStory):
  NAME = 'load:social:twitter:2019'
  URL = 'https://www.twitter.com/nasa'
  TAGS = [story_tags.YEAR_2019]

  # Desktop version is already covered by
  # 'browse:social:twitter_infinite_scroll'
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY


class LoadVkDesktopStory2018(_LoadingStory):
  NAME = 'load:social:vk:2018'
  URL = 'https://vk.com/sbeatles'
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY
  TAGS = [
      story_tags.HEALTH_CHECK, story_tags.INTERNATIONAL, story_tags.YEAR_2018
  ]


class LoadInstagramDesktopStory2018(_LoadingStory):
  NAME = 'load:social:instagram:2018'
  URL = 'https://www.instagram.com/selenagomez/'
  TAGS = [story_tags.HEALTH_CHECK, story_tags.YEAR_2018]
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY


class LoadInstagramMobileStory2019(_LoadingStory):
  NAME = 'load:social:instagram:2019'
  URL = 'https://www.instagram.com/selenagomez/'
  TAGS = [story_tags.YEAR_2019]
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY


class LoadPinterestStory2019(_LoadingStory):
  NAME = 'load:social:pinterest:2019'
  URL = 'https://uk.pinterest.com/categories/popular/'
  TAGS = [story_tags.JAVASCRIPT_HEAVY, story_tags.YEAR_2019]


################################################################################
# News, discussion and knowledge portals and blogs.
################################################################################


class LoadBbcDesktopStory2018(_LoadingStory):
  NAME = 'load:news:bbc:2018'
  URL = 'https://www.bbc.co.uk/news'
  TAGS = [story_tags.YEAR_2018]
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY


class LoadBbcMobileStory2019(_LoadingStory):
  NAME = 'load:news:bbc:2019'
  URL = 'https://www.bbc.co.uk/news'
  TAGS = [story_tags.YEAR_2019]
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY


class LoadCnnStory2018(_LoadingStory):
  NAME = 'load:news:cnn:2018'
  URL = 'https://edition.cnn.com'
  TAGS = [
      story_tags.HEALTH_CHECK, story_tags.JAVASCRIPT_HEAVY, story_tags.YEAR_2018
  ]


class LoadFlipboardDesktopStory(_LoadingStory):
  NAME = 'load:news:flipboard'
  URL = 'https://flipboard.com/explore'
  TAGS = [story_tags.YEAR_2016]
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY


class LoadHackerNewsDesktopStory2018(_LoadingStory):
  NAME = 'load:news:hackernews:2018'
  URL = 'https://news.ycombinator.com'
  TAGS = [story_tags.YEAR_2018]
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY


class LoadNytimesDesktopStory2018(_LoadingStory):
  NAME = 'load:news:nytimes:2018'
  URL = 'http://www.nytimes.com'
  TAGS = [story_tags.YEAR_2018]
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY


class LoadNytimesMobileStory2019(_LoadingStory):
  NAME = 'load:news:nytimes:2019'
  URL = 'http://mobile.nytimes.com'
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY
  TAGS = [story_tags.YEAR_2019]


class LoadQqMobileStory2019(_LoadingStory):
  NAME = 'load:news:qq:2019'
  URL = 'https://xw.qq.com/?f=c_news'
  TAGS = [story_tags.INTERNATIONAL, story_tags.YEAR_2019]
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY


class LoadQqDesktopStory2018(_LoadingStory):
  NAME = 'load:news:qq:2018'
  URL = 'https://news.qq.com'
  TAGS = [story_tags.INTERNATIONAL, story_tags.YEAR_2018]
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY


class LoadRedditDesktopStory2018(_LoadingStory):
  NAME = 'load:news:reddit:2018'
  URL = 'https://www.reddit.com/r/news/top/?sort=top&t=week'
  TAGS = [story_tags.YEAR_2018]
  SUPPORTED_PLATFORMS = platforms.DESKTOP_ONLY


class LoadRedditMobileStory2019(_LoadingStory):
  NAME = 'load:news:reddit:2019'
  URL = 'https://www.reddit.com/r/news/top/?sort=top&t=week'
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY
  TAGS = [story_tags.YEAR_2019]


class LoadWashingtonPostMobileStory2019(_LoadingStory):
  NAME = 'load:news:washingtonpost:2019'
  URL = 'https://www.washingtonpost.com/pwa'
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY
  TAGS = [story_tags.YEAR_2019]
  _CONTINUE_FREE_BUTTON_SELECTOR = '.continue-btn.button.free'
  _ACCEPT_GDPR_SELECTOR = '.agree-ckb'
  _CONTINUE_TO_SITE_SELECTOR = '.continue-btn.button.accept-consent'

  def _DidLoadDocument(self, action_runner):
    # Close the popup window. On Nexus 9 (and probably other tables) the popup
    # window does not have a "Close" button, instead it has only a "Send link
    # to phone" button. So on tablets we run with the popup window open. The
    # popup is transparent, so this is mostly an aesthetical issue.
    has_button = action_runner.EvaluateJavaScript(
        '!!document.querySelector({{ selector }})',
        selector=self._CONTINUE_FREE_BUTTON_SELECTOR)
    if has_button:
      action_runner.ClickElement(selector=self._CONTINUE_FREE_BUTTON_SELECTOR)
      action_runner.ScrollPageToElement(selector=self._ACCEPT_GDPR_SELECTOR)
      action_runner.ClickElement(selector=self._ACCEPT_GDPR_SELECTOR)
      element_function = js_template.Render(
        'document.querySelectorAll({{ selector }})[{{ index }}]',
        selector=self._CONTINUE_TO_SITE_SELECTOR, index=0)
      action_runner.ClickElement(element_function=element_function)


class LoadWikipediaStory2018(_LoadingStory):
  NAME = 'load:news:wikipedia:2018'
  URL = 'https://en.wikipedia.org/wiki/Science'
  TAGS = [story_tags.EMERGING_MARKET, story_tags.YEAR_2018]


class LoadIrctcStory2019(_LoadingStory):
  NAME = 'load:news:irctc:2019'
  URL = 'https://www.irctc.co.in'
  SUPPORTED_PLATFORMS = platforms.MOBILE_ONLY
  TAGS = [story_tags.EMERGING_MARKET, story_tags.YEAR_2019]

  def _Login(self, action_runner):
    # There is an error on replay that pops up the first time. If we
    # navigate again, the error disappears.
    action_runner.tab.WaitForDocumentReadyStateToBeComplete()
    action_runner.Navigate(self.URL)
    action_runner.tab.WaitForDocumentReadyStateToBeComplete()

################################################################################
# Audio, images, and video.
################################################################################


class LoadYouTubeStory2018(_LoadingStory):
  # No way to disable autoplay on desktop.
  NAME = 'load:media:youtube:2018'
  URL = 'https://www.youtube.com/watch?v=QGfhS1hfTWw&autoplay=false'
  TAGS = [
      story_tags.HEALTH_CHECK, story_tags.EMERGING_MARKET, story_tags.YEAR_2018
  ]


class LoadDailymotionStory2019(_LoadingStory):
  NAME = 'load:media:dailymotion:2019'
  URL = ('https://www.dailymotion.com/video/x7paozv')
  TAGS = [story_tags.YEAR_2019]


class LoadGoogleImagesStory2018(_LoadingStory):