package scoreboard

import (
	"encoding/json"
	"os"

	"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
)

type TestRecord struct {
	TestName     string              `json:"testname"`
	StageResults []stage.StageResult `json:"stageresults"`
}

type ScoreboardData struct {
	TestRecords []TestRecord `json:"testrecords"`
}

type Scoreboard struct {
	scoreboard ScoreboardData
}

func (b *Scoreboard) Init() {
	b.scoreboard.TestRecords = make([]TestRecord, 0)
}

func (b *Scoreboard) AddScore(testname string, results []stage.StageResult) {
	b.scoreboard.TestRecords = append(b.scoreboard.TestRecords,
		TestRecord{TestName: testname, StageResults: results})
}

func (b *Scoreboard) SaveFile(filePath string) {
	json_file, _ := os.Create(filePath)
	defer json_file.Close()

	encoder := json.NewEncoder(json_file)
	encoder.SetEscapeHTML(false)
	encoder.SetIndent("", "  ")
	_ = encoder.Encode(b.scoreboard)
}