package flashscore

import (
	"bytes"
	"fmt"

	"github.com/PuerkitoBio/goquery"
	"gordenko.dev/dima/flashscore/model"
	webspider "gordenko.dev/dima/spider"
)

func loadHomeAndAwayTeams(spider *webspider.Spider, matchID string, sportID int) (home, away model.Team, err error) {
	// Загружаем Landing
	landingURL := fmt.Sprintf(h2hLandingURLPattern, matchID)

	var (
		buf []byte
		doc *goquery.Document
	)

	buf, err = spider.Get(landingURL, nil)
	if err != nil {
		err = fmt.Errorf("load h2hLanding: %s", err)
		return
	}

	//ioutil.WriteFile("h2h.html", buf, 0644)

	doc, err = goquery.NewDocumentFromReader(bytes.NewBuffer(buf))
	if err != nil {
		err = fmt.Errorf("goquery.NewDocumentFromReader: %s", err)
		return
	}

	//var home, away model.Team

	switch sportID {

	// БОЛЬШЕ НЕ АКТУАЛЬНО!
	// case Football:
	// 	// Данные футбольных команд закодированы в HTML на странице
	// 	// Home team
	// 	home, err = parseTeam(doc.Find(".tname-home"))
	// 	if err != nil {
	// 		err = fmt.Errorf("Parse home team: %s", err)
	// 		return
	// 	}

	// 	// Away team
	// 	away, err = parseTeam(doc.Find(".tname-away"))
	// 	if err != nil {
	// 		err = fmt.Errorf("Parse away team: %s", err)
	// 		return
	// 	}

	case Football, Handball, Tennis:
		// Данные гандбольных команд закодированы в JSON переменной 'environment'
		// в теге <script> на странице
		home, away, err = parseTeamsOnMatchPageInJsVar(doc)
		if err != nil {
			err = fmt.Errorf("parseTeamsOnMatchPageInJsVar: %s", err)
			return
		}

		//case Tennis:

	}
	return
}

// storedHome, storedAway *model.Team

func listOfScheduledMatches(sportID int) (result []ScheduledMatch, err error) {
	spider, err := webspider.NewSpider(nil)
	if err != nil {
		panic(err)
	}

	var (
		referer string
		urls    []string
	)

	switch sportID {
	case Football:
		urls = []string{
			footballAllGamesTodayURL,
			footballAllGamesTomorrowURL,
		}
		referer = "https://www.flashscore.com/football/"

	case Handball:
		urls = []string{
			handballAllGamesTodayURL,
			handballAllGamesTomorrowURL,
		}
		referer = "https://www.flashscore.com/handball/"

	case Tennis:
		urls = []string{
			tennisAllGamesTodayURL,
			tennisAllGamesTomorrowURL,
		}
		referer = "https://www.flashscore.com/tennis/"

	}

	// Загружаем AJAX страницы со списками матчей

	for _, allGamesURL := range urls {
		var (
			buf  []byte
			list []ScheduledMatch
		)

		buf, err = spider.Get(allGamesURL, map[string]string{
			"referer":          "https://d.flashscore.com/x/feed/proxy-local",
			"x-requested-with": "XMLHttpRequest",
			// Важное поле! Код можно найти в исходниках либо подсмотреть. Без кода
			// сервер отдает 401 ошибку
			"x-fsign":   feedSign,
			"x-referer": referer,
			"x-geoip":   "1",
		})
		if err != nil {
			err = fmt.Errorf("Load all Games feed: %s", err)
			return
		}

		//fmt.Printf("%s\n\n", buf)

		list, err = parseFlashscoreEncodedAllGames(string(buf), sportID)
		if err != nil {
			err = fmt.Errorf("parseListOfScheduledMatches: %s", err)
			return
		}

		result = append(result, list...)
	}
	return
}

func loadH2H(spider *webspider.Spider, matchID string, sportID int, homeCanonicalName, awayCanonicalName string) (homeTeamMatches, awayTeamMatches, h2hMatches []model.TeamMatch, err error) {
	switch sportID {
	case Football:
		//return loadFootballH2H(spider, matchID)
		return loadHandballH2H(spider, matchID, homeCanonicalName, awayCanonicalName)

	case Handball:
		return loadHandballH2H(spider, matchID, homeCanonicalName, awayCanonicalName)

	case Tennis:
		// pass

	default:
		panic(fmt.Sprintf("Bug: h2h loader for the sportID %d is undefined", sportID))
	}
	return
}
