package flashscore

import (
	"log"
	"time"

	xhtml "golang.org/x/net/html"
	"gordenko.dev/dima/flashscore/model"
	webspider "gordenko.dev/dima/spider"
)

const (
//WSMessageFootballChamps       = "footballChamps"
//WSMessageFootballMatchReports = "footballMatchReports"
//WSMessageFootballMatchUpdate  = "footballMatchUpdate"
//WSMTeamMatchReport = "teamMatchReport"

// Channel = "main"
)

const (
	// В файле https://www.flashscore.com/x/js/core_2_1825000000.js переменной feed_sign жестко задается значение
	feedSign = "SW9D1eZo"

	//url = "https://www.flashscore.com/football/england/premier-league/"
	// Слэш в конце, чтобы избежать лишнего редиректа
	teamURLPattern = "https://www.flashscore.com/team%s/"
	// champID = /england/premier-league
	//fixturesURLPattern = "https://www.flashscore.com/football%s/fixtures/"
	//h2hURL      = "https://www.flashscore.com/match/lUKXoAEo/#h2h;overall"
	//h2hLandingURLPattern        = "https://www.flashscore.com/match/%s/#h2h;overall"
	h2hLandingURLPattern        = "https://www.flashscore.com/match/%s/#/h2h/overall"
	footballH2HTablesURLPattern = "https://d.flashscore.com/x/feed/d_hh_%s_en_1"
	// Лента событий: голы, карточки, пенальти и т.д.
	footballMatchSummaryURLPattern = "https://d.flashscore.com/x/feed/d_su_%s_en_1"
	footballOddsURLPattern         = "https://d.flashscore.com/x/feed/d_od_%s_en_1_eu" // en - lang, eu - price format

	handballOddsURLPattern = "https://d.flashscore.com/x/feed/df_od_1_%s"

	//basketballAllGamesURL = "https://d.flashscore.com/x/feed/f_3_0_2_en_1"
	// basketball next day https://d.flashscore.com/x/feed/f_3_1_2_en_1

	footballAllGamesTodayURL    = "https://d.flashscore.com/x/feed/f_1_0_2_en_1"
	footballAllGamesTomorrowURL = "https://d.flashscore.com/x/feed/f_1_1_2_en_1"

	//footballAllGamesFeedURL = "https://d.flashscore.com/x/feed/f_1_0_3_en_1" ????

	handballAllGamesTodayURL    = "https://d.flashscore.com/x/feed/f_7_0_2_en_1"
	handballAllGamesTomorrowURL = "https://d.flashscore.com/x/feed/f_7_1_2_en_1"

	tennisAllGamesTodayURL    = "https://d.flashscore.com/x/feed/f_2_0_3_en_1"
	tennisAllGamesTomorrowURL = "https://d.flashscore.com/x/feed/f_2_1_3_en_1"

	handballH2HTablesURLPattern = "https://d.flashscore.com/x/feed/df_hh_1_%s"

	tennisH2HTablesURLPattern = "https://d.flashscore.com/x/feed/df_hh_1_%s"

	handballScoreByPeriodsURLPattern = "https://d.flashscore.com/x/feed/df_sur_1_%s"
	handballMatchScoreURLPattern     = "https://d.flashscore.com/x/feed/dc_1_%s"

	tennisMatchStatURLPattern = "https://d.flashscore.com/x/feed/df_st_1_%s"

	teamChampURLPattern = "https://www.flashscore.com%s"

	SymbolNot    = "¬"
	SymbolTilda  = "~"
	SymbolDivide = "÷"
)

const (
	Football   = 1
	Handball   = 2
	Tennis     = 3
	Basketball = 4
)

const (
	BookmakerBwin      = 2
	BookmakerBetAtHome = 3
	BookmakerUnibet    = 5
	BookmakerWillHill  = 15
	BookmakerBet365    = 16
	BookmakerBetfair   = 429
	Bookmaker1xBet     = 417
)

var (
	nodeTypes = map[xhtml.NodeType]string{
		xhtml.ErrorNode:    "ErrorNode",
		xhtml.TextNode:     "TextNode",
		xhtml.DocumentNode: "DocumentNode",
		xhtml.ElementNode:  "ElementNode",
		xhtml.CommentNode:  "CommentNode",
		xhtml.DoctypeNode:  "DoctypeNode",
	}
)

var (
	sportPrefixes = map[int]string{
		Football:   "/football",
		Handball:   "/handball",
		Basketball: "/basketball",
	}
)

//type FlashscoreSnapshot struct {
//	Handball []UpcomingMatchReport
//}

type ScheduledMatch struct {
	MatchID   string
	StartTime time.Time
	// Название (правда в верхнем регистре) и ID достаем со страницы AllGames
	Zone model.TeamZone
	// Если нет в БД - загружаем страницу с чемпионатом и достаем чистое название
	// без приписок Play Offs, State 2  и т.д.
	ChampID       string
	SportID       int
	FullChampName string
}

type UpcomingMatchReport struct {
	MatchID         string            `json:"matchID"`
	SportID         int               `json:"sportID"`
	StartTime       int64             `json:"startTime"`
	ParseTime       time.Time         `json:"parseTime"`
	Home            model.Team        `json:"home"`
	Away            model.Team        `json:"away"`
	Champ           model.TeamChamp   `json:"champ"`
	Zone            model.TeamZone    `json:"zone"`
	FullChampName   string            `json:"fullChampName"`
	HomeTeamMatches []model.TeamMatch `json:"homeTeamMatches"`
	AwayTeamMatches []model.TeamMatch `json:"awayTeamMatches"`
	H2H             []model.TeamMatch `json:"h2h"`
	Odds            Odds              `json:"odds"`
	IsTeamsLoaded   bool              `json:"isTeamsLoaded"`
	IsHistoryLoaded bool              `json:"isHistoryLoaded"`
}

type Flashscore struct {
	model  *model.Model
	spider *webspider.Spider
	logger *log.Logger
}

type teamScoreByPeriods struct {
	HasStats    bool
	P1HomeGoals int
	P1AwayGoals int
	P2HomeGoals int
	P2AwayGoals int
}

type Winner3Way struct {
	Bookmaker int     `json:"bookmaker"`
	Home      float64 `json:"home"`
	Draw      float64 `json:"draw"`
	Away      float64 `json:"away"`
}

type Winner2Way struct {
	Bookmaker int     `json:"bookmaker"`
	Home      float64 `json:"home"`
	Away      float64 `json:"away"`
}

type TotalOffer struct {
	Bookmaker int     `json:"bookmaker"`
	Over      float64 `json:"over"`
	Under     float64 `json:"under"`
}

type Total struct {
	Total  string       `json:"total"`
	Offers []TotalOffer `json:"offers"`
}

type Odds struct {
	FullTimeTotal []Total      `json:"fullTimeTotal"`
	H1Total       []Total      `json:"h1Total"`
	Winner        []Winner3Way `json:"winner3way"` // footabll, handball
	Winner2Way    []Winner2Way `json:"winner2way"` // tennis
}
