package decortsdk

import (
	"fmt"
	"reflect"

	"repository.basistech.ru/BASIS/decort-golang-sdk/config"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/sdn"
)

type ClientInterface interface {
	CloudAPI() *cloudapi.CloudAPI
	CloudBroker() *cloudbroker.CloudBroker
	SDN() *sdn.SDN
}

func NewUniversal(cfg config.UniversalConfig) (ClientInterface, error) {
	countConfigs := 0
	var client ClientInterface

	switch {
	case cfg.Decs3oConfig != nil && reflect.TypeOf(*cfg.Decs3oConfig) == reflect.TypeOf(config.Config{}):
		client = New(*cfg.Decs3oConfig)
		countConfigs++
	case cfg.BVSConfig != nil && reflect.TypeOf(*cfg.BVSConfig) == reflect.TypeOf(config.BVSConfig{}):
		client = NewBVS(*cfg.BVSConfig)
		countConfigs++
	case cfg.LegacyConfig != nil && reflect.TypeOf(*cfg.LegacyConfig) == reflect.TypeOf(config.LegacyConfig{}):
		client = NewLegacy(*cfg.LegacyConfig)
		countConfigs++
	}
	if countConfigs != 1 {
		return nil, fmt.Errorf("only 1 config can be used at a time")
	}

	return client, nil
}
