package coordinator

import (
	"gitlink.org.cn/cloudream/common/pkgs/mq"
	cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
)

type HubService interface {
	GetHubConfig(msg *GetHubConfig) (*GetHubConfigResp, *mq.CodeMessage)

	GetHubs(msg *GetHubs) (*GetHubsResp, *mq.CodeMessage)

	GetHubConnectivities(msg *GetHubConnectivities) (*GetHubConnectivitiesResp, *mq.CodeMessage)
}

var _ = Register(Service.GetHubConfig)

type GetHubConfig struct {
	mq.MessageBodyBase
	HubID cortypes.HubID `json:"hubID"`
}
type GetHubConfigResp struct {
	mq.MessageBodyBase
	Hub cortypes.Hub `json:"hub"`
}

func ReqGetHubConfig(hubID cortypes.HubID) *GetHubConfig {
	return &GetHubConfig{
		HubID: hubID,
	}
}
func RespGetHubConfig(hub cortypes.Hub) *GetHubConfigResp {
	return &GetHubConfigResp{
		Hub: hub,
	}
}
func (client *Client) GetHubConfig(msg *GetHubConfig) (*GetHubConfigResp, error) {
	return mq.Request(Service.GetHubConfig, client.rabbitCli, msg)
}

// 获取指定节点的信息。如果HubIDs为nil，则返回所有Hub
var _ = Register(Service.GetHubs)

type GetHubs struct {
	mq.MessageBodyBase
	HubIDs []cortypes.HubID `json:"hubIDs"`
}
type GetHubsResp struct {
	mq.MessageBodyBase
	Hubs []*cortypes.Hub `json:"hubs"`
}

func NewGetHubs(hubIDs []cortypes.HubID) *GetHubs {
	return &GetHubs{
		HubIDs: hubIDs,
	}
}
func NewGetHubsResp(hubs []*cortypes.Hub) *GetHubsResp {
	return &GetHubsResp{
		Hubs: hubs,
	}
}
func (r *GetHubsResp) GetHub(id cortypes.HubID) *cortypes.Hub {
	for _, n := range r.Hubs {
		if n.HubID == id {
			return n
		}
	}

	return nil
}
func (client *Client) GetHubs(msg *GetHubs) (*GetHubsResp, error) {
	return mq.Request(Service.GetHubs, client.rabbitCli, msg)
}

// 获取节点连通性信息
var _ = Register(Service.GetHubConnectivities)

type GetHubConnectivities struct {
	mq.MessageBodyBase
	HubIDs []cortypes.HubID `json:"hubIDs"`
}
type GetHubConnectivitiesResp struct {
	mq.MessageBodyBase
	Connectivities []cortypes.HubConnectivity `json:"hubs"`
}

func ReqGetHubConnectivities(hubIDs []cortypes.HubID) *GetHubConnectivities {
	return &GetHubConnectivities{
		HubIDs: hubIDs,
	}
}
func RespGetHubConnectivities(cons []cortypes.HubConnectivity) *GetHubConnectivitiesResp {
	return &GetHubConnectivitiesResp{
		Connectivities: cons,
	}
}
func (client *Client) GetHubConnectivities(msg *GetHubConnectivities) (*GetHubConnectivitiesResp, error) {
	return mq.Request(Service.GetHubConnectivities, client.rabbitCli, msg)
}
