package flashscore

import (
	"encoding/json"
	"fmt"
	"testing"

	webspider "gordenko.dev/dima/spider"
)

func TestTennisLoadHomeAndAwayTeams(t *testing.T) {
	spider, _ := webspider.NewSpider(nil)

	home, away, err := loadHomeAndAwayTeams(spider, "dUf6AMy8", Tennis)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("home: %#v\n", home)
	fmt.Printf("away: %#v\n", away)
}

func TestFootballLoadHomeAndAwayTeams(t *testing.T) {
	spider, _ := webspider.NewSpider(nil)

	home, away, err := loadHomeAndAwayTeams(spider, "rLXrnfEM", Football)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("home: %#v\n", home)
	fmt.Printf("away: %#v\n", away)
}

func TestFootballLoadH2H(t *testing.T) {
	spider, _ := webspider.NewSpider(nil)

	homeTeamMatches, awayTeamMatches, h2hMatches, err := loadH2H(spider, "rLXrnfEM", Football, "Gent B", "Saint Eloois")
	if err != nil {
		t.Fatal(err)
	}

	var buf []byte

	buf, _ = json.MarshalIndent(homeTeamMatches, "", "    ")
	fmt.Printf("home: %s\n", buf)

	buf, _ = json.MarshalIndent(awayTeamMatches, "", "   ")
	fmt.Printf("away: %s\n", buf)

	buf, _ = json.MarshalIndent(h2hMatches, "", "    ")
	fmt.Printf("h2h: %s\n", buf)
}

func TestLoadOdds(t *testing.T) {
	spider, _ := webspider.NewSpider(nil)

	matchID := "OtpuFKWi"

	url := fmt.Sprintf(handballOddsURLPattern, matchID)

	buf, err := spider.Get(url, map[string]string{
		"x-requested-with": "XMLHttpRequest",
		"x-fsign":          feedSign,
		"x-geoip":          "1",
		"referer":          fmt.Sprintf(h2hLandingURLPattern, matchID),
	})
	if err != nil {
		err = fmt.Errorf("Load handball odds: %s; url=%s", err, url)
		return
	}

	odds, err := parseFlashscoreEncodedOdds(string(buf))
	if err != nil {
		return
	}

	buf, _ = json.MarshalIndent(odds, "", "    ")
	fmt.Printf("TestLoadOdds: %s\n", buf)
}
