from ..engine import SearchEngine
from ..config import PROXY, TIMEOUT, FAKE_USER_AGENT
from ..utils import unquote_url


class Google(SearchEngine):
    """Searches google.com"""

    def __init__(self, proxy=PROXY, timeout=TIMEOUT):
        super(Google, self).__init__(proxy, timeout)
        self._base_url = "https://www.google.com"
        self._delay = (2, 6)
        self.set_headers({"User-Agent": FAKE_USER_AGENT})

    def _selectors(self, element):
        """Returns the appropriate CSS selector."""
        selectors = {
            "url": "a[href]",
            "title": "a",
            "text": "span.st",
            "links": "div#search div[class=g]",
            "next": "div#foot a#pnnext",
        }
        return selectors[element]

    def _first_page(self):
        """Returns the initial page and query."""
        url = u"{}/search?q={}".format(self._base_url, self._query)
        return {"url": url, "data": None}

    def _next_page(self, tags):
        """Returns the next page URL and post data (if any)"""
        selector = self._selectors("next")
        next_page = self._get_tag_item(tags.select_one(selector), "href")
        url = (self._base_url + next_page) if next_page else None
        return {"url": url, "data": None}

    def _get_url(self, tag, item="href"):
        """Returns the URL of search results item."""
        selector = self._selectors("url")
        url = self._get_tag_item(tag.select_one(selector), item)

        if url.startswith(u"/url?q="):
            url = url.replace(u"/url?q=", u"").split(u"&sa=")[0]
        return unquote_url(url)
