package nacos

import (
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/clients/config_client"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
	"time"
)

type Logger struct {
}

func (l Logger) Info(args ...interface{}) {

}

func (l Logger) Warn(args ...interface{}) {

}

func (l Logger) Error(args ...interface{}) {
	fmt.Println("hello")
}

func (l Logger) Debug(args ...interface{}) {

}

func (l Logger) Infof(fmt string, args ...interface{}) {

}

func (l Logger) Warnf(fmt string, args ...interface{}) {

}

func (l Logger) Errorf(fmt string, args ...interface{}) {

}

func (l Logger) Debugf(fmt string, args ...interface{}) {

}

func NewNacosConfig(ip string, port uint64, opts ...constant.ClientOption) config_client.IConfigClient {

	sc := []constant.ServerConfig{
		*constant.NewServerConfig(ip, port),
	}

	clientOpts := append([]constant.ClientOption{
		constant.WithNamespaceId("public"),
		constant.WithTimeoutMs(5000),
		constant.WithLogDir("./log/nacos/log/" + time.Now().Format(time.DateOnly)),
		constant.WithCacheDir("./log/nacos/cache"),
		constant.WithLogLevel("error"),
		constant.WithCustomLogger(Logger{}),
	}, opts...)

	cc := constant.NewClientConfig(clientOpts...)

	client, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  cc,
			ServerConfigs: sc,
		},
	)

	if err != nil {
		panic(err)
	}

	return client
}
