# Copyright 2014 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 telemetry.page import page as page_module
from telemetry import story


class KeyDesktopSitesPage(page_module.Page):

  def __init__(self, url, page_set):
    super(KeyDesktopSitesPage, self).__init__(
        url=url, page_set=page_set, credentials_path = 'data/credentials.json')
    self.archive_data_file = 'data/key_desktop_sites.json'

  def RunPageInteractions(self, action_runner):
    with action_runner.CreateGestureInteraction('ScrollAction'):
      action_runner.ScrollPage()


class FacebookPage(KeyDesktopSitesPage):

  def __init__(self, page_set):
    super(FacebookPage, self).__init__(
      url='http://facebook.com',
      page_set=page_set)

    self.credentials = 'facebook'


class GmailPage(KeyDesktopSitesPage):

  def __init__(self, page_set):
    super(GmailPage, self).__init__(
      url='https://mail.google.com/mail/',
      page_set=page_set)

    self.scrollable_element_function = '''
      function(callback) {
        gmonkey.load('2.0', function(api) {
          callback(api.getScrollableElement());
        });
      }'''
    self.credentials = 'google'

  def RunPageInteractions(self, action_runner):
    with action_runner.CreateGestureInteraction('ScrollAction'):
      action_runner.ScrollPage()
    action_runner.WaitForJavaScriptCondition(
        'window.gmonkey !== undefined && '
        'document.getElementById("gb") !== null')


class GoogleCalendarPage(KeyDesktopSitesPage):

  def __init__(self, page_set):
    super(GoogleCalendarPage, self).__init__(
      url='https://www.google.com/calendar/',
      page_set=page_set)

    self.scrollable_element_function = '''
      function(callback) {
        callback(document.getElementById('scrolltimedeventswk'));
      }'''
    self.credentials = 'google'


class GoogleDrivePage(KeyDesktopSitesPage):

  def __init__(self, page_set):
    super(GoogleDrivePage, self).__init__(
      url='https://drive.google.com',
      page_set=page_set)

    self.scrollable_element_function = '''
      function(callback) {
        callback(document.getElementsByClassName('doclistview-list')[0]);
      }'''
    self.credentials = 'google'

  def RunPageInteractions(self, action_runner):
    with action_runner.CreateGestureInteraction('ScrollAction'):
      action_runner.ScrollPage()
    action_runner.WaitForJavaScriptCondition(
        'document.getElementsByClassName("doclistview-list").length')


class GoogleDocPage(KeyDesktopSitesPage):

  def __init__(self, page_set):
    super(GoogleDocPage, self).__init__(
      # pylint: disable=C0301
      url='https://docs.google.com/a/google.com/document/d/1XMAtPiVFZfItsMUOYl39v5YA8bcSPe4LDrVO25OdsCU/edit',
      page_set=page_set)

    self.scrollable_element_function = '''
      function(callback) {
        callback(document.getElementsByClassName('kix-appview-editor')[0]);
      }'''
    self.credentials = 'google'

  def RunPageInteractions(self, action_runner):
    with action_runner.CreateGestureInteraction('ScrollAction'):
      action_runner.ScrollPage()
    action_runner.WaitForJavaScriptCondition(
        'document.getElementsByClassName("kix-appview-editor").length')


class KeyDesktopSitesPageSet(story.StorySet):

  """ Sites of Interest """

  def __init__(self):
    super(KeyDesktopSitesPageSet, self).__init__(
      archive_data_file='data/key_desktop_sites.json',
      cloud_storage_bucket=story.PARTNER_BUCKET)

    self.AddUserStory(FacebookPage(self))
    self.AddUserStory(GmailPage(self))
    self.AddUserStory(GoogleCalendarPage(self))
    self.AddUserStory(GoogleDrivePage(self))
    self.AddUserStory(GoogleDocPage(self))

    urls_list = [
      'http://www.google.com/nexus/5/#/',
      'http://youtube.com',
      'http://twitter.com/nbc',
      'http://bbc.co.uk',
      'http://imdb.com',
      'http://espn.go.com',
      'http://cnn.com',
      'http://bbc.co.uk/news/',
      'http://weather.com',
      'http://livejournal.com',
      'http://deviantart.com',
      'http://foxnews.com',
      'http://nbcnews.com',
      'http://scribd.com',
      'http://movies.yahoo.com',
      'http://tv.yahoo.com',
      'http://pandora.com',
      'http://tmz.com',
      'http://hulu.com',
      'http://abcnews.go.com',
      'http://youtube.com/videos',
      'http://ndtv.com',
      'http://money.cnn.com',
      'http://msn.foxsports.com',
      'http://cbsnews.com',
      'http://wired.com',
      'http://cnbc.com',
      'http://sportsillustrated.cnn.com',
      'http://home.disney.go.com',
      'http://urbandictionary.com',
      'http://rottentomatoes.com',
      'http://foodnetwork.com',
      'http://npr.org',
      'http://gawker.com',
      'http://last.fm',
      'http://sky.com',
      'http://eonline.com',
      'http://egotastic.com',
      'http://copyscape.com',
      'http://mtv.com',
      'http://ultimate-guitar.com',
      'http://comcast.com',
      'http://cbc.ca',
      'http://fanfiction.net',
      'http://discovery.com',
      'http://deezer.com',
      'http://metrolyrics.com',
      'http://foxnews.com/entertainment/',
      'http://cartoonnetwork.com',
      'http://paypal.com',
      'http://finance.yahoo.com',
      'http://alibaba.com',
      'http://bankofamerica.com',
      'http://www.chase.com/',
      'http://wellsfargo.com',
      'http://skype.com',
      'http://online.wsj.com',
      'http://indeed.com',
      'http://samsung.com',
      'http://reuters.com',
      'http://ups.com',
      'http://forbes.com',
      'http://clickbank.com',
      'http://target.com',
      'http://att.com',
      'http://cj.com',
      'http://constantcontact.com',
      'http://ezinearticles.com',
      'http://shutterstock.com',
      'http://americanexpress.com',
      'http://freelancer.com',
      'http://istockphoto.com',
      'http://fedex.com',
      'http://verizonwireless.com',
      'http://capitalone.com',
      'http://bloomberg.com',
      'http://monster.com',
      'http://hdfcbank.com',
      'http://fotolia.com',
      'http://thesun.co.uk',
      'http://zillow.com',
      'http://nokia.com',
      'http://tradedoubler.com',
      'http://icicibank.com',
      'http://123rf.com',
      'http://elance.com',
      'http://icbc.com.cn',
      'http://news.cnet.com',
      'http://verizon.com',
      'http://careerbuilder.com',
      'http://sears.com',
      'http://getresponse.com',
      'http://sitesell.com',
      'http://manta.com',
      'http://www.blogger.com/',
      'http://avg.com',
      'http://google.com/analytics/',
      'http://go.com',
      'http://flickr.com',
      'http://aol.com',
      'http://thepiratebay.se',
      'http://zedo.com',
      'http://about.com',
      'http://stackoverflow.com',
      'http://godaddy.com',
      'http://mediafire.com',
      'http://wordpress.org',
      'http://adwords.google.com',
      'http://imgur.com',
      'http://4shared.com',
      'http://vimeo.com',
      'http://play.google.com/',
      'http://badoo.com',
      'http://aweber.com',
      'http://mozilla.org',
      'http://www.stumbleupon.com/stumbler/chimacintosh',
      'http://www.google.com/adsense/',
      'http://my.yahoo.com',
      'http://sourceforge.net',
      'http://answers.com',
      'http://wordpress.org/extend/plugins/',
      'http://photobucket.com',
      'http://clicksor.com',
      'http://google.com/reader/',
      'http://store.apple.com',
      'http://wikia.com',
      'http://statcounter.com',
      'http://fiverr.com',
      'http://slideshare.net',
      'http://salesforce.com',
      'http://myspace.com',
      'http://hootsuite.com',
      'http://domaintools.com',
      'http://rediff.com',
      'http://soundcloud.com',
      'http://download.cnet.com',
      'http://archive.org',
      'http://filestube.com',
      'http://developers.facebook.com',
      'http://hostgator.com',
      'http://battle.net',
      'http://pch.com',
      'http://ign.com',
      'http://pogo.com',
      'http://miniclip.com',
      'http://888.com',
      'http://gamespot.com',
      'http://steampowered.com',
      'http://gamefaqs.com',
      'http://xbox.com',
      'http://games.yahoo.com',
      'http://betfair.com',
      'http://kongregate.com',
      'http://ea.com',
      'http://leagueoflegends.com',
      'http://roblox.com',
      'http://williamhill.com',
      'http://playstation.com',
      'http://wowhead.com',
      'http://king.com',
      'http://minecraft.net',
      'http://chess.com',
      'http://minecraftwiki.net',
      'http://addictinggames.com',
      'http://mmo-champion.com',
      'http://runescape.com',
      'http://travian.com',
      'http://zone.msn.com',
      'http://ubi.com',
      'http://calottery.com',
      'http://freeonlinegames.com',
      'http://games.com',
      'http://n4g.com',
      'http://nvidia.com',
      'http://callofduty.com',
      'http://us.playstation.com',
      'http://bet-at-home.com',
      'http://gametrailers.com',
      'http://teamliquid.net',
      'http://nick.com/games/',
      'http://planetminecraft.com',
      'http://nintendo.com',
      'http://popcap.com',
      'http://gamehouse.com',
      'http://curse.com',
      'http://bulbagarden.net',
      'http://rockstargames.com',
      'http://partycasino.com',
      'http://square-enix.com',
      'http://perfectworld.com',
      'http://nih.gov',
      'http://webmd.com',
      'http://ncbi.nlm.nih.gov/pubmed/',
      'http://focusoncrohnsdisease.com',
      'http://mayoclinic.com',
      'http://mercola.com',
      'http://drugs.com',
      'http://menshealth.com',
      'http://nlm.nih.gov/medlineplus/',
      'http://weightwatchers.com',
      'http://cdc.gov',
      'http://caloriecount.about.com',
      'http://patents.uspto.gov',
      'http://psychologytoday.com',
      'http://nhs.uk',
      'http://medscape.com',
      'http://foxnews.com/health/',
      'http://who.int',
      'http://healthboards.com',
      'http://self.com',
      'http://health.com',
      'http://kidshealth.org',
      'http://fda.gov',
      'http://netdoctor.co.uk',
      'http://prevention.com',
      'http://makeupalley.com',
      'http://stevepavlina.com',
      'http://realage.com',
      'http://fitnessmagazine.com',
      'http://healthcentral.com',
      'http://rxlist.com',
      'http://vitals.com',
      'http://totalbeauty.com',
      'http://nuance.com',
      'http://telegraph.co.uk/health/',