# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import CrawlSpider,Rule as rule
from worldfootball.items import WorldfootballItem
from scrapy.linkextractors import LinkExtractor
import re
from bs4 import BeautifulSoup
import requests

class StatscrawlerSpider(CrawlSpider):
    name = 'statscrawler'
    allowed_domains = ['worldfootball.net']
    base_url = "https://www.worldfootball.net/"

    league_url_names = ["eng-premier-league",
                        "bel-eerste-klasse-a",
                        "fra-ligue-1",
                        "bundesliga",
                        "ita-serie-a",
                        "ned-eredivisie",
                        "pol-ekstraklasa",
                        "por-primeira-liga",
                        "sco-premiership",
                        "esp-primera-division",
                        "sui-super-league"]

    links = []
    for league in league_url_names:
        links.append(base_url + "schedule/" + league)

    start_urls = links
    # rules = (
    #     rule(LinkExtractor(restrict_css=(".luecke+ td a")), follow=True, callback="parse"), # for weeks
    #     rule(LinkExtractor(restrict_css=(".with-border td:nth-child(1) a")), follow=True, callback="parse"),
    # )

    def parse(self, response):
        table_list = []
        res = response.css("div.data > table.standard_tabelle")[1].css("tr")
        for i, row_selector in enumerate(res):
            if i == 0:
                continue
            row_list = []
            for i, col_selector in enumerate(row_selector.css("td")):
                if i == 1:
                    continue
                elif i == 2:
                    col_selector = col_selector.css("a::text")
                else:
                    col_selector = col_selector.css("td::text")
                row_list.append(col_selector.get())
            table_list.append(row_list)

        time_text = response.css("div#navi > div.breadcrumb > h1::text").get()

        re1='.*?'	# Non-greedy match on filler
        re2='((?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])'	# Year 1
        rg = re.compile(re1+re2,re.IGNORECASE|re.DOTALL)
        m = rg.search(time_text)
        season = 2000
        if m:
            season=int(m.group(1))

        if season < 2005:
            return

        week = time_text[time_text.index("»", time_text.index(str(season))) + 2 : -7]

        league = time_text[: time_text.index(str(season)) - 1].replace("» ", "")

        #league = time_text[:time_text.index("League") + 6]
        ranking = 0
        date = response.css("div.data > table.standard_tabelle")[0].css("tr > td::text").get()
        for temp_list in table_list:
            print(temp_list)
            item = WorldfootballItem()
            item["date"] = date
            item["week"] = week
            item["season"] = season
            item["league"] = league
            item["team"] = temp_list[1]
            if temp_list[0] != "\u00a0":
                ranking = int(temp_list[0])
            item["ranking"] = ranking
            item["win_count"] = int(temp_list[3])
            item["draw_count"] = int(temp_list[4])
            item["lose_count"] = int(temp_list[5])
            item["goals_scored"] = int(temp_list[6].split(":")[0])
            item["goals_against"] = int(temp_list[6].split(":")[1])
            item["dif"] = int(temp_list[7])
            item["points"] = int(temp_list[8])
            yield item

        seasonPageButton = response.css(".with-border td:nth-child(1) a")
        weekPageButton = response.css(".luecke+ td a")
        base_url = "https://www.worldfootball.net/"

        if weekPageButton:
            yield scrapy.Request(base_url + weekPageButton.css("a::attr(href)").get(),callback=self.parse)
        else:
            yield scrapy.Request(base_url + seasonPageButton.css("a::attr(href)").get(),callback=self.parse)