package main

/*
Отписка от матча:
- метод Unwatch
- когда матч исчезнет из лайва
*/

import (
	"os/signal"
	"syscall"

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

	"gordenko.dev/dima/flashscore/daemon"

	"log"
	"os"

	"gordenko.dev/dima/web"

	"gordenko.dev/dima/web/api"
	"gordenko.dev/dima/web/router"

	_ "github.com/go-sql-driver/mysql"
)

const (
	port    = 7767
	dsn     = "user:password@/tipper"
	baseDir = "/home/ubuntu/hfs"
)

func main() {
	var err error

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

	db, err := qx.Open("mysql", dsn)
	if err != nil {
		log.Fatalf("qx.Open: %s\n", err)
	}
	defer db.Close()

	//fs := flashscore.New(db)

	d, err := daemon.New(daemon.Options{
		BaseDir:       baseDir,
		Logger:        logger,
		CheckInterval: 60 * 60 * 6,
		Db:            db,
		SportIDs: []int{
			flashscore.Tennis,
			flashscore.Handball,
			flashscore.Football,
		},
	})
	if err != nil {
		log.Fatalf("daemon.New: %s\n", err)
	}
	defer d.Close()

	publicAPI, err := api.NewAPI(api.Options{
		Logger: logger,
	})
	if err != nil {
		log.Fatalf("api.NewAPI: %s\n", err)
	}

	publicAPI.PublicFunc("listUpcomingMatchReports", d.ListUpcomingMatchReports)

	r := router.New()
	r.HandleFunc("/reports", d.ShowReports)
	r.PrefixHandle("/api", publicAPI)

	app, err := web.NewApp(web.AppOptions{
		Port:   port,
		Router: r,
	})
	if err != nil {
		log.Fatalf("web.NewApp: %s\n", err)
	}

	go app.Run()
	go d.Run()

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	<-sigs

	logger.Println("daemon stopped.")

	/*
		//err = flashscore.ListFootballChampFixtures(fixturesURL)
		_, err = flashscore.GetUpcomingFootballMatchReport("riQ962dS")
		//err = testParseTr(trsrc)
		//err = loadOdds()
		//_, err = loadFootballMatchSummary("Sv4qwtWD")
		if err != nil {
			panic(err)
		}
	*/
}

/*
	FootballChampIDs: []string{
		"/england/premier-league",
		"/england/championship",
		"/england/league-one",
		//"/england/league-two",
		//"/england/national-league",
		//"/england/national-league-north",
		//"/england/national-league-south",
		//"/england/npl-premier-division",
		//"/england/southern-league-central-division",
		//"/england/southern-league-south-division",
		//"/england/isthmian-league-premier-division",
		//"/england/professional-development-league",
		//"/england/efl-cup",
		//"/england/efl-trophy",
		//"/england/fa-cup",
		//"/england/fa-trophy",
		//"/england/premier-league-cup",
		//"/england/premier-league-international-cup",
		//"/england/premier-league-2",
		//"/england/premier-league-u18",
		//"/england/fa-youth-cup",
		//"/england/women-s-super-league", 06.09
		//"/england/women-s-championship",
		//"/england/women-s-fa-cup", 26.09
		//"/england/women-s-league-cup",
		//"/germany/bundesliga",
		//"/germany/2-bundesliga",
		"/germany/3-liga",
		//"/germany/regionalliga-north",
		"/germany/regionalliga-nordost",
		//"/germany/regionalliga-west", // 04.09
		//"/germany/regionalliga-sudwest", // 4.09
		//"/germany/regionalliga-bayern",
		//"/germany/dfb-pokal", // 12.09
		"/germany/oberliga-nofv-nord",
		"/germany/oberliga-nofv-sud",
		//"/germany/bundesliga-women", // 4.09
		//"/germany/2-bundesliga-women",
		"/austria/tipico-bundesliga",
		"/austria/2-liga",
		//"/regionalliga-west-promotion-group", // WTF ?
		"/austria/regionalliga-east",
		"/austria/regionalliga-central",
		"/austria/regionalliga-west-tyrol",
		"/austria/regionalliga-west-salzburg",
		"/austria/regionalliga-west-vorarlberg",
		"/austria/regionalliga-west",
		"/austria/ofb-cup",
		"/austria/bundesliga-women",
		//"/netherlands/eredivisie", // 12.09
		"/netherlands/eerste-divisie", // 28.08
		//"/netherlands/tweede-divisie", // 5.09
		//"/netherlands/derde-divisie", 05.09
		//"/netherlands/reserve-league",
		"/netherlands/knvb-beker",
		//"/netherlands/eredivisie-u19",
		"/france/ligue-1",
		"/france/ligue-2",
		"/france/national",
		"/france/national-2-group-a",
		"/france/national-2-group-b",
		"/france/national-2-group-c",
		"/france/national-2-group-d",
		//"/france/coupe-de-la-ligue",
		//"/france/coupe-de-france",
		//"/france/division-1-women", 09
		//"/france/division-2-women", 09
		//"/france/coupe-de-france-women",
		//"/netherlands/eredivisie-women",  06.09
		//"/netherlands/knvb-beker-women",
		//"/netherlands/eredivisie-cup-women",
		"/italy/serie-a",
		"/italy/serie-b",
		"/italy/serie-a-women",
		//"/italy/serie-b-women",
		//"/italy/coppa-italia-women",
		"/spain/laliga",
		"/spain/laliga2",
		//"/spain/primera-division-women",
		//"/spain/copa-de-la-reina-women",
		"/portugal/primeira-liga",
		//"/portugal/ligapro",
		//"/portugal/campeonato-de-portugal",
		//"/portugal/taca-de-portugal",
		//"/portugal/league-cup",
		//"/portugal/liga-revelacao-u23",
		//"/portugal/liga-bpi-women",
		//"/portugal/taca-de-portugal-women",
		"/belgium/jupiler-league", // 08.08
		//"/belgium/proximus-league", // 22.08
		//"/belgium/first-amateur-division",
		//"/belgium/belgian-cup",
		//"/belgium/pro-league-u21",
		//"/belgium/super-league-women",
		//"/belgium/1st-national-women",
		//"/belgium/belgian-cup-women",
		"/russia/premier-league",
		"/russia/fnl",
		"/russia/russian-cup",
		"/russia/fnl-cup",
		"/russia/pfl-group-1",
		"/russia/pfl-group-2",
		"/russia/pfl-group-3",
		"/russia/pfl-group-4",
		"/russia/youth-league",
		"/russia/supreme-division-women",
		"/switzerland/super-league",
		"/switzerland/challenge-league",
		//"/switzerland/promotion-league", 15.08 ??? (FRO after date)
		//"/switzerland/1-liga-classic-group-1",
		//"/switzerland/1-liga-classic-group-2",
		//"/switzerland/1-liga-classic-group-3",
		//"/switzerland/1-liga-classic-play-offs",
		//"/switzerland/swiss-cup",
		"/switzerland/nationalliga-a-women",
		"/ukraine/premier-league",
		"/ukraine/persha-liga",
		"/ukraine/druha-liga",
		"/belarus/vysshaya-liga",
		"/belarus/pershaya-liga",
		"/belarus/vysshaya-liga-women",
		"/estonia/meistriliiga",
		"/estonia/esiliiga",
		"/estonia/estonian-cup",
		"/latvia/optibet-virsliga",
		"/latvia/1-liga",
		"/latvia/latvian-cup",
		"/lithuania/a-lyga",
		"/lithuania/i-lyga",
		"/lithuania/lithuanian-cup",
		"/moldova/divizia-nationala",
		"/moldova/moldovan-cup",
		"/greece/super-league",
		"/greece/super-league-2",
		"/greece/football-league",
		"/north-macedonia/1-mfl",
		//"/north-macedonia/2-mfl",
		//"/north-macedonia/macedonian-cup",
		"/turkey/super-lig",
		"/turkey/1-lig",
		//"/turkey/turkish-cup",
		"/bulgaria/parva-liga",
		"/bulgaria/vtora-liga",
		//"/cyprus/first-division", // 22.08
		//"/cyprus/division-2", // ??
		"/israel/ligat-ha-al",
		//"/israel/leumit-league",
		"/israel/state-cup",
		"/bahrain/premier-league",
		//"/bahrain/king-s-cup",
		//"/bahrain/bahrain-cup",
		"/faroe-islands/premier-league",
		"/faroe-islands/faroe-islands-cup",
		"/hungary/otp-bank-liga",
		"/hungary/merkantil-bank-liga",
		"/hungary/nb-iii-east",
		"/hungary/nb-iii-centre",
		"/hungary/nb-iii-west",
		"/hungary/hungarian-cup",
		//"/hungary/nb-i-women",
		"/montenegro/prva-crnogorska-liga",
		"/bosnia-and-herzegovina/premier-league",
		"/luxembourg/national-division", // 23.08
		"/luxembourg/luxembourg-cup",
		"/romania/liga-1",
		"/romania/liga-2",
		//"/romania/romanian-cup",
		//"/romania/superliga-women",
		//"/romania/romanian-cup-women",
		"/croatia/1-hnl",
		"/croatia/2-hnl",
		//"/croatia/croatian-cup",
		"/czech-republic/1-liga",
		"/czech-republic/division-2",
		"/czech-republic/cfl-group-a",
		"/czech-republic/cfl-group-b",
		"/czech-republic/msfl",
		"/czech-republic/mol-cup",
		"/czech-republic/u19-league",
		"/czech-republic/division-a",
		"/czech-republic/division-b",
		"/czech-republic/division-c",
		"/czech-republic/division-d",
		"/czech-republic/division-e",
		"/czech-republic/division-f",
		"/czech-republic/1-liga-women",
		//"/czech-republic/2-liga-women", // 6.09
		"/czech-republic/czech-cup-women",
		"/poland/ekstraklasa",
		"/poland/division-1",
		"/poland/division-2",
		"/poland/iii-liga-group-i",
		"/poland/iii-liga-group-ii",
		"/poland/iii-liga-group-iii",
		"/poland/iii-liga-group-iv",
		"/poland/polish-cup",
		//"/poland/central-youth-league",
		"/poland/ekstraliga-women",
		"/poland/polish-cup-women",
		"/georgia/crystalbet-erovnuli-liga",
		"/georgia/crystalbet-erovnuli-liga-2",
		//"/georgia/georgian-cup",
		"/armenia/premier-league",
		"/armenia/first-league",
		"/denmark/superliga",
		"/denmark/1st-division",
		"/denmark/2nd-division-group-1",
		"/denmark/2nd-division-group-2",
		"/denmark/2nd-division-group-3",
		//"/denmark/2nd-division-promotion-group", // WTF?
		//"/denmark/2nd-division-relegation-group", // WTF
		//"/denmark/landspokal-cup",
		"/denmark/elitedivisionen-women",

		"/finland/veikkausliiga",
		"/finland/ykkonen", // странное расписание
		"/finland/kakkonen-group-a",
		"/finland/kakkonen-group-b",
		"/finland/kakkonen-group-c",
		"/norway/eliteserien",
		"/norway/obos-ligaen",
		"/norway/division-2-group-1",
		"/norway/division-2-group-2",
		//"/norway/division-3-group-1",
		//"/norway/division-3-group-2",
		//"/norway/division-3-group-3",
		//"/norway/division-3-group-4",
		//"/norway/division-3-group-5",
		//"/norway/division-3-group-6",
		//"/norway/junior-u19",
		//"/norway/nm-cup",
		"/norway/toppserien-women",
		"/sweden/allsvenskan",
		"/sweden/superettan",
		"/sweden/division-1-norra",
		"/sweden/division-1-sodra",
		//"/sweden/division-1-relegation",
		//"/sweden/division-2-norrland", // FRO
		//"/sweden/division-2-norra-svealand",
		//"/sweden/division-2-vastra-gotaland",
		//"/sweden/division-2-sodra-svealand",
		//"/sweden/division-2-norra-gotaland",
		"/sweden/svenska-cupen",
		"/sweden/allsvenskan-women",
		"/sweden/elitettan-women",
		"/sweden/svenska-cupen-women",
		"/iceland/pepsideild",
		"/iceland/inkasso-deildin",
		"/iceland/division-2",
		//"/iceland/league-cup",
		"/iceland/icelandic-cup",
		"/iceland/urvalsdeild-women",
		"/iceland/icelandic-cup-women",
		"/ireland/premier-division",
		"/ireland/division-1",
		"/ireland/fai-cup",
		"/ireland/league-cup",
		//"/northern-ireland/nifl-premiership",
		//"/northern-ireland/nifl-championship",
		"/scotland/premiership",
		//"/scotland/championship", // 17.10
		//"/scotland/league-one", // 17.10
		//"/scotland/league-two", // 17.10
		//"/wales/cymru-premier",
		"/serbia/super-liga",
		"/serbia/prva-liga",
		//"/serbia/serbian-cup",
		"/kosovo/superliga",
		"/slovakia/fortuna-liga",
		"/slovakia/2-liga",
		"/slovakia/divison-c-west",
		"/slovakia/divison-c-east",
		"/slovakia/divison-c-central",
		"/slovakia/divison-c-bratislava",
		"/slovakia/slovak-cup",
		"/slovakia/1-liga-women",
		"/slovenia/prva-liga",
		"/slovenia/2-snl",
		"/malta/premier-league",
		"/malta/division-1",
		//"/malta/fa-trophy",
		"/malta/summer-cup",
		"/kazakhstan/premier-league",
		"/kazakhstan/first-division",
		//"/kyrgyzstan/premier-liga", // 8.08 FRO
		"/egypt/premier-league",
		"/tunisia/ligue-professionnelle-1",
		//"/tunisia/ligue-2",
		"/qatar/qsl",
		"/qatar/qsl-cup",
		"/saudi-arabia/saudi-professional-league",
		"/saudi-arabia/division-1",
		//"/saudi-arabia/king-cup",
		"/jordan/premier-league",
		//"/jordan/jordan-cup",
		//"/jordan/shield-cup",
		//"/oman/professional-league",
		//"/oman/sultan-cup",
		//"/iran/persian-gulf-pro-league",
		"/iran/division-1",
		"/iran/hazfi-cup",
		//"/iraq/super-league",
		"/kuwait/premier-league",
		//"/kuwait/crown-prince-cup",
		"/kuwait/emir-cup",
		//"/pakistan/premier-league",

		// AMERICA

		"/uruguay/primera-division",
		"/uruguay/segunda-division",
		"/usa/mls",
		"/usa/usl-championship",
		"/usa/usl-league-one",
		//"/usa/usl-league-two",
		"/usa/nisa",
		"/usa/nwsl-women",
		//"/usa/nwsl-challenge-cup-women",
		"/canada/canadian-premier-league",
		"/canada/championship",
		"/brazil/serie-a",
		"/brazil/serie-b",
		"/brazil/serie-c",
		//"/brazil/serie-d", // 06.09
		//"/brazil/primeira-liga",

		//"/brazil/campeonato-acreano",
		"/brazil/campeonato-alagoano",
		//"/brazil/campeonato-amapaense",
		//"/brazil/campeonato-amazonense",
		"/brazil/campeonato-baiano",
		//"/brazil/campeonato-brasiliense",
		//"/brazil/campeonato-capixaba",
		//"/brazil/campeonato-carioca",
		"/brazil/campeonato-catarinense",
		//"/brazil/campeonato-cearense",
		"/brazil/campeonato-gaucho",
		//"/brazil/campeonato-gaucho-2",
		//"/brazil/campeonato-goiano",
		"/brazil/campeonato-maranhense",
		//"/brazil/campeonato-matogrossense",
		"/brazil/campeonato-mineiro",
		//"/brazil/campeonato-mineiro-2",
		"/brazil/campeonato-paraense",
		"/brazil/campeonato-paraibano",
		"/brazil/campeonato-paranaense",
		"/brazil/campeonato-paulista",
		//"/brazil/campeonato-paulista-a2",
		"/brazil/campeonato-pernambucano",
		//"/brazil/campeonato-piauiense",
		"/brazil/campeonato-potiguar",
		//"/brazil/campeonato-rondoniense",
		"/brazil/campeonato-roraimense",
		"/brazil/campeonato-sergipano",
		//"/brazil/campeonato-sul-matogrossense",
		"/brazil/campeonato-tocantinense",
		"/brazil/copa-do-brasil",
		//"/brazil/copa-do-brasil-u20",
		//"/brazil/copa-verde",
		"/brazil/copa-do-nordeste",
		//"/brazil/brasileiro-u20",
		//"/brazil/brasileiro-women",
		//"/brazil/brasileiro-a2-women",
		//"/argentina/superliga",
		//"/argentina/primera-nacional",
		//"/argentina/torneo-federal",
		"/mexico/liga-mx",
		//"/mexico/ascenso-mx",
		"/mexico/liga-mx-women",
		//"/bolivia/division-profesional",
		//"/bolivia/nacional-b",
		//"/chile/primera-division",
		//"/chile/primera-b",
		//"/chile/chilean-cup",
		//"/colombia/primera-a", // 06.09
		//"/colombia/primera-b", // 05.09
		"/ecuador/liga-pro",
		"/ecuador/serie-b",
		//"/ecuador/copa-ecuador",
		"/costa-rica/primera-division",
		//"/costa-rica/liga-de-ascenso", end
		"/paraguay/primera-division",
		//"/paraguay/division-intermedia",
		//"/paraguay/copa-paraguay",
		"/peru/liga-1",
		//"/peru/liga-2",
		"/panama/lpf",
		"/costa-rica/primera-division",
		//"/costa-rica/liga-de-ascenso",
		"/nicaragua/liga-primera",
		//"/jamaica/premier-league",
		// OCEANIA
		"/australia/a-league",
		"/australia/npl-act",
		"/australia/npl-northern-nsw",
		"/australia/npl-nsw",
		"/australia/npl-queensland",
		"/australia/npl-south-australian",
		"/australia/npl-tasmania",
		"/australia/npl-victoria",
		"/australia/npl-western-australia",
		"/australia/brisbane-premier-league",
		"/australia/ffa-cup",
		//"/australia/y-league",
		//"/australia/w-league",
		"/new-zealand/football-championship",
		//"/new-zealand/national-league-women",
		//"/philippines/pfl",
		"/china/super-league",
		//"/china/jia-league",
		//"/china/yi-league",
		//"/china/fa-cup",
		"/mongolia/premier-league",
		"/south-korea/k-league-1",
		"/south-korea/k-league-2",
		"/south-korea/k3-league",
		"/south-korea/korean-cup",
		"/south-korea/league-cup",
		"/south-korea/wk-league-women",
		"/japan/j1-league",
		"/japan/j2-league",
		"/japan/j3-league",
		"/japan/ybc-levain-cup",
		//"/japan/emperors-cup",
		"/japan/nadeshiko-league-women",
		"/japan/empress-s-cup-women",
		"/vietnam/v-league-1",
		"/vietnam/v-league-2",
		//"/vietnam/vietnamese-cup",
		"/vietnam/national-league-women",
		"/vietnam/vietnamese-cup-women",
		"/singapore/premier-league",
		//"/singapore/singapore-cup",
		"/taiwan/premier-league",
		"/taiwan/mulan-football-league-women",
		"/thailand/thai-league-1",
		"/thailand/thai-league-2",
		//"/thailand/thai-fa-cup",
		"/cambodia/c-league",
		"/laos/lao-league",
		"/indonesia/liga-1",
		"/indonesia/liga-2",
		"/indonesia/president-cup",
		"/malaysia/super-league",
		"/malaysia/premier-league",
		//"/malaysia/malaysia-cup",
		//"/malaysia/fa-cup",
		//"/sri-lanka/champions-league", end
		// EUROPA
		"/europe/europa-league",
		"/europe/champions-league",
		// AFRICA
		//"/nigeria/npfl",
		//"/nigeria/nnl",
		//"/nigeria/aiteo-cup",
		//"/senegal/ligue-1",
		"/south-africa/premier-league",
		"/south-africa/gladafrica-championship",
		"/south-africa/nedbank-cup",
		//"/sudan/premier-league",
		//"/zimbabwe/premier-soccer-league",
		"/zambia/super-league",
		//"/cameroon/elite-one",
		//"/cameroon/elite-two",
		//"/ivory-coast/ligue-1",
		//"/kenya/premier-league",
		//"/lesotho/premier-league",
		"/morocco/botola-pro",
		"/morocco/botola-2",
		//"/morocco/coupe-du-trone",

	},
*/
