package flashscore

import (
	"fmt"
	//"io/ioutil"
	//webspider "spider"
	//"strconv"
	//"strings"
	//"time"
	"bytes"

	"github.com/PuerkitoBio/goquery"

	//"github.com/kr/pretty"
	//"html"
	"flashscore/model"
)

//var (
//	spider *webspider.Spider
//)

/*
	err = ioutil.WriteFile("fixtures-unescaped", []byte(content), 0666)
	if err != nil {
		return
	}
*/

var trsrc = `<html>
<body>
<table>
<tr class="odd highlight" onclick="cjs.Api.loader.get('cjs').call(function(_cjs){_cjs.fromGlobalScope.detail_open('g_0_z7bKeorA', null, false); });">
<td>
	<span class="date">1583784000</span>
</td>
<td class="flag_td "					title="Premier League (England)">
	<span class="flag fl_198"></span>PL]
</td>
<td class="name">
	<span><strong>Leicester</strong></span>
</td>
<td class="name highTeam">
	<span>Aston Villa</span>
</td>
<td class="">
	<span class="score"><strong>4 : 0</strong></span>
</td>
<td class="winLose">
	<a title="Loss" class="wld wld--l">L</a>
</td>
</tr>
</table>
</body>
</html>`

func testParseTr(src string) (err error) {
	doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer([]byte(src)))
	if err != nil {
		return
	}
	/*
		txt, err := doc.Html()
		if err != nil {
			return
		}

		fmt.Printf("%s\n", txt)


		trs := doc.Find("tr")

		fmt.Printf("trs: %d\n", trs.Length())
	*/
	trs := doc.Find(".highlight")
	for trIdx := range trs.Nodes {
		var m model.TeamMatch
		//fmt.Printf("trIdx: %d\n", trIdx)

		m, err = parseMatchResultTr(trs.Eq(trIdx))
		if err != nil {
			err = fmt.Errorf("parseTr: %s", err)
			return
		}

		fmt.Printf("%v\n", m)
		fmt.Println()
	}
	return
}

/*

 */
// cut
/*
	txt, err := first.Html()
	if err != nil {
		return
	}

	err = ioutil.WriteFile("temp", []byte(html.UnescapeString(txt)), 0666)
	if err != nil {
		return
	}
*/
// end cut
//n := first.Get(0)

//fmt.Printf("node: %#v\n\n", txt)

/*

Всё. Я написал. Парсит следующие страницы:

https://www.flashscore.com/football/england/premier-league/fixtures/ - расписание матчей

https://www.flashscore.com/match/lUKXoAEo/#h2h;overall - H2H (все 3 вкладки: overall, home, away)

https://www.flashscore.com/match/lUKXoAEo/#odds-comparison;1x2-odds;full-time - коэфф. на FullTime 1 x 2 (плюс определяет букмекеров)

https://www.flashscore.com/match/Sv4qwtWD/#match-summary - из отчета по матчу достает только счет 1го и 2го тайма

*/
