package polaris

import (
	"github.com/go-kratos/kratos/contrib/polaris/v2"
	"github.com/go-kratos/kratos/v2/config"
	"github.com/go-lynx/lynx/app"
	"github.com/go-lynx/lynx/plugin"
	"github.com/go-lynx/lynx/plugin/polaris/conf"
	"github.com/polarismesh/polaris-go/api"
)

var name = "polaris"

type PlugPolaris struct {
	polaris polaris.Polaris
	conf    conf.Polaris
	weight  int
}

func (p *PlugPolaris) Weight() int {
	return p.weight
}

func (p *PlugPolaris) Name() string {
	return name
}

func (p *PlugPolaris) Load(b config.Value) (plugin.Plugin, error) {
	err := b.Scan(&p.conf)
	if err != nil {
		return nil, err
	}

	sdk, err := api.InitContextByConfig(api.NewConfiguration())
	if err != nil {
		app.Lynx().GetHelper().Error(err)
		panic(err)
	}

	p.polaris = polaris.New(
		sdk,
		polaris.WithService(app.Name()),
		polaris.WithNamespace(p.conf.Namespace),
	)

	// set polaris plane for lynx
	app.Lynx().SetControlPlane(p)
	configuration := app.Lynx().GetBootConfiguration()
	delete(configuration, name)
	if len(configuration) > 1 {
		app.Lynx().PlugManager().LoadSpecificPlugins(
			app.Lynx().PlugManager().PreparePlug(configuration),
			configuration,
		)
	}
	return p, nil
}

func (p *PlugPolaris) Unload() error {
	return nil
}

func Polaris() plugin.Plugin {
	return &PlugPolaris{
		weight: 999999,
	}
}
