package metacache

import (
	"time"

	"gitlink.org.cn/cloudream/common/pkgs/logger"
	stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
	coormq "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/mq/coordinator"
	cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
)

func (m *MetaCacheHost) AddHubMeta() *HubMeta {
	meta := &HubMeta{}
	meta.cache = NewSimpleMetaCache(SimpleMetaCacheConfig[cortypes.HubID, cortypes.Hub]{
		Getter: meta.load,
		Expire: time.Minute * 5,
	})

	m.caches = append(m.caches, meta)
	return meta
}

type HubMeta struct {
	cache *SimpleMetaCache[cortypes.HubID, cortypes.Hub]
}

func (h *HubMeta) Get(hubID cortypes.HubID) *cortypes.Hub {
	v, ok := h.cache.Get(hubID)
	if ok {
		return &v
	}
	return nil
}

func (h *HubMeta) GetMany(hubIDs []cortypes.HubID) []*cortypes.Hub {
	vs, oks := h.cache.GetMany(hubIDs)
	ret := make([]*cortypes.Hub, len(vs))
	for i := range vs {
		if oks[i] {
			ret[i] = &vs[i]
		}
	}
	return ret
}

func (h *HubMeta) ClearOutdated() {
	h.cache.ClearOutdated()
}

func (h *HubMeta) load(keys []cortypes.HubID) ([]cortypes.Hub, []bool) {
	vs := make([]cortypes.Hub, len(keys))
	oks := make([]bool, len(keys))

	coorCli, err := stgglb.CoordinatorMQPool.Acquire()
	if err != nil {
		logger.Warnf("new coordinator client: %v", err)
		return vs, oks
	}
	defer stgglb.CoordinatorMQPool.Release(coorCli)

	get, err := coorCli.GetHubs(coormq.NewGetHubs(keys))
	if err != nil {
		logger.Warnf("get hubs: %v", err)
		return vs, oks
	}

	for i := range keys {
		if get.Hubs[i] != nil {
			vs[i] = *get.Hubs[i]
			oks[i] = true
		}
	}

	return vs, oks
}
