package flashscore

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

func TestTennisParseAllGames(t *testing.T) {
	// flashscore format
	buf, err := os.ReadFile("for_parse/allGamesTennis.fsf")
	//buf, err := ioutil.ReadFile("for_parse/allGamesHandball.fsf")
	if err != nil {
		t.Fatal(err)
	}
	str := string(buf)

	/*
		list, err := ParseFlashscoreEncoding(str)
		if err != nil {
			t.Fatal(err)
		}

		for _,m := range list {
			for k, v := range m {
				fmt.Printf("%s: %s\n", k, v)
			}
			fmt.Println()
		}
	*/

	matches, err := parseFlashscoreEncodedAllGames(str, Tennis)
	//matches, err := ParseFlashscoreEncoding(str)
	if err != nil {
		t.Fatal(err)
	}

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

func TestTennisParseMatchScore(t *testing.T) {
	str := "DA÷3¬DZ÷3¬DB÷3¬DD÷1618078710¬AW÷1¬DC÷1618074000¬DS÷0¬DE÷2¬DF÷0¬DG÷2¬DH÷0¬DI÷-1¬DK÷1618078747¬DL÷3¬DM÷¬DT÷¬DX÷ST,MH,MC,OD,HH,DR¬DEI÷https://www.flashscore.com/res/image/data/p2vtlMHG-xfKxQmxa-MiVx2mEg.png¬DR÷0¬DV÷2¬DJ÷H¬AZ÷1¬SC÷417¬SB÷3¬SD÷1xBet¬A1÷¬~"

	homeScore, awayScore, err := parseHandballMatchScore(str)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("home - away: %d-%d\n\n", homeScore, awayScore)
}

func TestTennisParseScoreByPeriods(t *testing.T) {
	// 2 sets
	// str := "AC÷3¬BA÷6¬BB÷1¬RC÷0:44¬~BC÷6¬BD÷2¬RD÷0:42¬~RB÷1:26¬~A1÷¬~"

	// 3 sets
	//str := "AC÷3¬BA÷7¬DA÷7¬BB÷6¬DB÷2¬RC÷1:05¬~BC÷4¬BD÷6¬RD÷0:50¬~BE÷6¬BF÷2¬RE÷0:43¬~RB÷2:38¬~PSPH÷WC¬PSPA÷11¬A1÷¬~"

	// 5 sets
	str := "AC÷3¬BA÷3¬BB÷6¬RC÷0:36¬~BC÷7¬DC÷7¬BD÷6¬DD÷5¬RD÷1:18¬~BE÷7¬DE÷7¬BF÷6¬DF÷4¬RE÷1:08¬~BG÷0¬BH÷6¬RF÷0:29¬~BI÷6¬BJ÷3¬RG÷0:38¬~RB÷4:09¬~PSPH÷20¬PSPA÷12¬A1÷¬~"

	score, hasScoreByPeriods, err := parseTennisTeamScoreByPeriods(str)
	if err != nil {
		t.Fatal(err)
	}

	buf, _ := json.MarshalIndent(score, "", "    ")
	fmt.Printf("hasScoreByPeriods: %t, %s\n\n", hasScoreByPeriods, buf)
}

func TestTennisParseOdds(t *testing.T) {
	// Строку возвращает отдельный запрос на адрес handballOddsURL
	// flashscore format
	buf, err := os.ReadFile("for_parse/tennisOdds.fsf")
	//buf, err := os.ReadFile("for_parse/oddsHandball.fsf")
	if err != nil {
		t.Fatal(err)
	}
	str := string(buf)

	odds, err := parseFlashscoreEncodedOdds(str)
	if err != nil {
		t.Fatal(err)
	}

	//odds, err := ParseFlashscoreEncoding(str)
	//if err != nil {
	//	return
	//}

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

func TestTennisParseMatchStat(t *testing.T) {
	// Строку возвращает отдельный запрос на адрес handballOddsURL
	// flashscore format
	buf, err := os.ReadFile("for_parse/tennisMatchStat.fsf")
	if err != nil {
		t.Fatal(err)
	}
	str := string(buf)

	stat, err := parseTennisMatchStat(str)
	if err != nil {
		return
	}

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

// TestParseBreakpointsString - строка из таблицы статистики по матчу.
func TestParseBreakpointsString(t *testing.T) {
	bp := parseBreakpoints("50% (0/4)")

	fmt.Printf("breakpoints: %#v\n", bp)

}

//

func TestParseTennisSurfaceFromChampName(t *testing.T) {
	surface := ParseTennisSurfaceFromChampName("ITF MEN - SINGLES M25 Reus (Spain), clay")

	fmt.Printf("Surface: %s\n", surface)

}
