package abtest

import (
	"os"

	"gitlab.alibaba-inc.com/pai_biz_arch/pairec-experiment/client_golang/experiments"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec-experiment/client_golang/model"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/log"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/recconf"
)

var experimentClient *experiments.ExperimentClient

func Load(config *recconf.RecommendConfig) {
	if config.ABTestConf.Host != "" {
		env := config.RunMode
		if os.Getenv("PAIREC_ENVIRONMENT") != "" {
			env = os.Getenv("PAIREC_ENVIRONMENT")
		}

		l := log.ABTestLogger{}
		client, err := experiments.NewExperimentClient(config.ABTestConf.Host, env,
			experiments.WithLogger(experiments.LoggerFunc(l.Infof)),
			experiments.WithErrorLogger(experiments.LoggerFunc(l.Errorf)),
			experiments.WithToken(config.ABTestConf.Token),
		)

		if err != nil {
			panic(err)
		}

		experimentClient = client
	}
}

func GetExperimentClient() *experiments.ExperimentClient {
	return experimentClient
}

func GetParams(sceneName string) model.SceneParams {
	return experimentClient.GetSceneParams(sceneName)
}
