package daemon

import (
	"encoding/json"
	"fmt"
	"time"

	"gordenko.dev/dima/flashscore"
	"gordenko.dev/dima/timeutil"
	"gordenko.dev/dima/tipper/model"
	"gordenko.dev/dima/web"
)

func (s *Daemon) PageMatches(state *web.State, reply *web.Reply) (err error) {
	buf, _ := json.MarshalIndent(s.teamMatches, "", "    ")
	//reply.Render("matches", buf)
	reply.Write(buf)
	return
}

type TipView struct {
	Time     string
	Name     string
	Champ    string
	Link     string
	Result   string
	Status   model.TipStatus
	Strategy string
}

type DayTips struct {
	Date string
	Tips []TipView
}

type PageTipsData struct {
	Days []DayTips
}

func (s *Daemon) PageHandballByDays(state *web.State, reply *web.Reply) (err error) {
	data, err := s.getPageTipsData(flashscore.Handball)
	if err != nil {
		return
	}

	reply.Render("temp_tips", data)
	return
}

func (s *Daemon) PageFootballByDays(state *web.State, reply *web.Reply) (err error) {
	data, err := s.getPageTipsData(flashscore.Football)
	if err != nil {
		return
	}

	reply.Render("temp_tips", data)
	return
}

func (s *Daemon) PageTennisByDays(state *web.State, reply *web.Reply) (err error) {
	data, err := s.getPageTipsData(flashscore.Tennis)
	if err != nil {
		return
	}

	reply.Render("temp_tips", data)
	return
}

func (s *Daemon) getPageTipsData(sportID int) (_ PageTipsData, err error) {
	tips, err := s.ListSportTips(sportID)
	if err != nil {
		return
	}

	var days []DayTips

	var currentDay int64
	var dayTips DayTips

	for _, tip := range tips {
		if tip.Status != model.Won && tip.Status != model.Lost {
			continue
		}

		tm := time.Unix(tip.TipTime, 0)

		tm = timeutil.FirstSecondInPeriod(tm, "d")
		//if err != nil {
		//	return
		//}

		day := tm.Unix()

		if day != currentDay {
			if currentDay != 0 {
				days = append(days, dayTips)
			}

			dayTips = DayTips{
				Date: tm.Format("Monday Jan 2, 2006"),
			}

			currentDay = day
		}

		dayTips.Tips = append(dayTips.Tips, TipView{
			Time:     tm.Format("15:04"),
			Name:     tip.MatchName,
			Champ:    tip.ChampName,
			Link:     fmt.Sprintf("https://www.flashscore.com/match/%s/#h2h;overall", tip.MatchID),
			Result:   tip.Result,
			Status:   tip.Status,
			Strategy: tip.Strategy,
		})

	}

	if currentDay != 0 {
		days = append(days, dayTips)
	}

	return PageTipsData{
		Days: days,
	}, nil
}

/*
func (s *Tipper) pageLinked(state *web.State, reply *web.Reply) (err error) {
	//reply.Render("aliases", nil)
	var list []*interestingFootballMatch

	for _, x := range s.onexbetMatchToInterestingFootballMatch {
		list = append(list, x)
	}

	buf, _ := json.MarshalIndent(list, "", "    ")

	reply.WriteString(string(buf))
	return
}


func (s *Tipper) pageWatch(state *web.State, reply *web.Reply) (err error) {
	//reply.Render("aliases", nil)
	var matchID string
	state.Val("id", &matchID)

	s.onexbetAPIClient.Call("watch", matchID)

	reply.WriteString(matchID)
	return
}

func (s *Tipper) pageUnwatch(state *web.State, reply *web.Reply) (err error) {
	//reply.Render("aliases", nil)
	var matchID string
	state.Val("id", &matchID)

	s.onexbetAPIClient.Call("unwatch", matchID)

	reply.WriteString(matchID)
	return
}
*/
