package utils

import (
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"qteam/config"
	"sync"
)

var (
	client naming_client.INamingClient
	once   sync.Once
)

func GetNaocosClient() naming_client.INamingClient {
	once.Do(func() {
		//注册nacos
		fmt.Println(config.GetConf().Nacos.Port)
		sc := []constant.ServerConfig{
			*constant.NewServerConfig(config.GetConf().Nacos.Url, uint64(config.GetConf().Nacos.Port), constant.WithContextPath("/nacos")),
		}

		//create ClientConfig
		cc := *constant.NewClientConfig(
			constant.WithNamespaceId(""),
			constant.WithTimeoutMs(5000),
			constant.WithNotLoadCacheAtStart(true),
			constant.WithLogDir("/tmp/nacos/log"),
			constant.WithCacheDir("/tmp/nacos/cache"),
			constant.WithLogLevel("debug"),
		)
		var err error
		// create naming client
		client, err = clients.NewNamingClient(
			vo.NacosClientParam{
				ClientConfig:  &cc,
				ServerConfigs: sc,
			},
		)
		if err != nil {
			panic(err)
		}
	})
	return client
}
