package flashscore

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

	"gordenko.dev/dima/flashscore/model"
	"gordenko.dev/dima/qx"
)

/*
func TestGetUpcomingMatchReport(t *testing.T) {
	var err error
	//'req, err := http.NewRequest("GET", url, nil)

	logger := log.New(os.Stdout, "", log.LstdFlags)

	db, err := qx.Open("mysql", "root:zkx75fcy@/tipper")
	//db, err := qx.Open("mysql", "root:lklnmf6@/tipper")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	fs := New(db, logger)

	report, err := fs.GetUpcomingMatchReport(ScheduledMatch{
		MatchID:   "vqgqjCUl",
		SportID:   Handball,
		StartTime: time.Now(),
		ChampID:   "/handball/denmark/1-division",
		Zone: model.TeamZone{
			ZoneID: "/handball/denmark",
			Name:   "Denmark",
		},
	})
	//report, err := fs.GetUpcomingMatchReport("8vF2Fr1o", Football)
	if err != nil {
		fmt.Printf("GetUpcomingMatchReport: %s\n", err)
		return
	}

	buf, _ := json.MarshalIndent(report, "", "    ")
	fmt.Printf("%s\n", buf)
}
*/

func TestListOfScheduledMatches(t *testing.T) {
	var err error
	//'req, err := http.NewRequest("GET", url, nil)

	logger := log.New(os.Stdout, "", log.LstdFlags)

	db, err := qx.Open("mysql", "root:zkx75fcy@/tipper")
	//db, err := qx.Open("mysql", "root:lklnmf6@/tipper")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	fs := New(db, logger)

	reports, err := fs.ListOfScheduledMatches(Handball)
	if err != nil {
		fmt.Printf("ListUpcomingMatchReports: %s\n", err)
		return
	}

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

func TestCheckUpcomingMatchReports(t *testing.T) {
	var err error
	//'req, err := http.NewRequest("GET", url, nil)

	logger := log.New(os.Stdout, "", log.LstdFlags)

	db, err := qx.Open("mysql", "root:zkx75fcy@/tipper")
	//db, err := qx.Open("mysql", "root:lklnmf6@/tipper")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	fs := New(db, logger)

	scheduledMatches, err := fs.ListOfScheduledMatches(Football)
	if err != nil {
		fmt.Printf("ListOfScheduledMatches: %s\n", err)
		return
	}

	for _, match := range scheduledMatches[29:30] {
		// Здесь лок не нужен, ибо нет race conditions, пишем в мапу в этом же потоке
		report := UpcomingMatchReport{
			MatchID:   match.MatchID,
			SportID:   match.SportID,
			StartTime: match.StartTime.Unix(),
			Zone: model.TeamZone{
				ZoneID:  match.Zone.ZoneID,
				Name:    match.Zone.Name,
				SportID: match.SportID,
			},
			Champ: model.TeamChamp{
				ChampID: match.ChampID,
			},
		}

		// Даже если отчет полный - синкаем, чтобы обновить odds

		err = fs.SyncUpcomingMatchReport(&report)
		if err != nil {
			fmt.Printf("SyncUpcomingMatchReport: %s; matchID=%s", err, report.MatchID)
		}

		buf, _ := json.MarshalIndent(report, "", "    ")
		fmt.Printf("REPORT:\n\n%s\n\n", buf)
	}
	//buf, _ := json.MarshalIndent(reports, "", "    ")
	//fmt.Printf("%s\n", buf)
}
