package api

import (
	"log"
	"th7/data/core"
	"th7/ports"
	"time"

	"encoding/json"
)

const (
	CheckChannelDur = time.Millisecond * 1200
	StartupSleepDur = time.Second * 5
)

type Wrapper struct {
	Channels  []core.Channel `json:"channels"`
	Ratio     core.Ratio     `json:"ratio"`
	Timestamp string         `json:"time"`
}

// Regularly get the newest data and bung it out on ws
func startService(man *Manager, core ports.CorePort) {

	var wrap Wrapper

	time.Sleep(StartupSleepDur)

	for {
		wrap.Channels = core.GetChannels()
		wrap.Ratio = core.GetRatio()
		wrap.Timestamp = time.Now().Format(time.RFC1123)

		marshaled, err := json.Marshal(wrap)
		if err != nil {
			log.Println("Error marshaling struct:", wrap)
		}

		man.broadcast <- []byte(marshaled)

		time.Sleep(CheckChannelDur)
	}

}