package container

import (
	"fmt"
	"os/exec"

	crc "git.loafle.net/commons_go/rpc/client"
	crr "git.loafle.net/commons_go/rpc/registry"
	oocc "git.loafle.net/overflow/overflow_commons_go/config"
	ooccp "git.loafle.net/overflow/overflow_commons_go/config/probe"
	oopcc "git.loafle.net/overflow/overflow_probe_container/client"
	"git.loafle.net/overflow/overflow_probes/config"
)

func New(port int, rpcInvoker crr.RPCInvoker) crc.Client {
	addr := fmt.Sprintf("localhost:%d", port)

	c := oopcc.New(addr, rpcInvoker)

	return c
}

func GetContainerCommand(name string) (cmd *exec.Cmd, pidPath string) {

	pidPath = config.ContainerPIDPath(name)
	logConfigPath := config.ContainerLogConfigPath(name)
	binPath := config.ContainerBinPath(name)

	switch name {
	case ooccp.ContainerGeneralName:
		javaPath := config.JavaBinPath()

		cmd = exec.Command(javaPath, "-jar", binPath, pidPath, logConfigPath)
	case ooccp.ContainerNetworkName:
		arg1 := fmt.Sprintf("-%s=%s", oocc.FlagPidFilePathName, pidPath)
		arg2 := fmt.Sprintf("-%s=%s", oocc.FlagLogConfigFilePathName, logConfigPath)

		cmd = exec.Command(binPath, arg1, arg2)
	case ooccp.ContainerDiscoveryName:
		arg1 := fmt.Sprintf("-%s=%s", oocc.FlagPidFilePathName, pidPath)
		arg2 := fmt.Sprintf("-%s=%s", oocc.FlagLogConfigFilePathName, logConfigPath)

		cmd = exec.Command(binPath, arg1, arg2)
	default:
	}
	return
}
