package daemons

import (
	"fmt"
	"oc-schedulerd/conf"
	"os/exec"

	"github.com/rs/zerolog"
)

type LocalMonitor struct {
	ExecutionID string
	PeerID      string
	Duration    int
	Logger      zerolog.Logger
}

func (lm *LocalMonitor) LaunchLocalMonitor() {
	if lm.ExecutionID == "" {
		lm.Logger.Error().Msg("Missing parameter in LocalMonitor")
	}
	lm.execKube()
}

func (lm *LocalMonitor) execKube() {
	args := []string{
		"-e", lm.ExecutionID, "-p", lm.PeerID, "-u", conf.GetConfig().LokiUrl, "-m", conf.GetConfig().MongoUrl,
		"-d", conf.GetConfig().DBName,
	}

	if conf.GetConfig().Mode == "kubernetes" {
		args = append(args, []string{"-M", conf.GetConfig().Mode, "-H", conf.GetConfig().KubeHost, "-P", conf.GetConfig().KubePort,
			"-C", conf.GetConfig().KubeCert, "-D", conf.GetConfig().KubeData, "-c", conf.GetConfig().KubeCA}...)
	}
	if lm.Duration > 0 {
		args = append(args, "-t", fmt.Sprintf("%d", lm.Duration))
	}
	cmd := exec.Command(conf.GetConfig().MonitorPath, args...)
	fmt.Printf("Command : %v\n", cmd)
	err := cmd.Start()
	if err != nil {
		lm.Logger.Error().Msg("Could not start oc-monitor for " + lm.ExecutionID + " : " + err.Error())
	}
}
