package recall

import (
	"fmt"
	"strings"
	"time"

	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/context"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/log"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/module"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/recconf"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/utils"
)

type UserGlobalHotRecall struct {
	*BaseRecall
	userGroupHotRecallDao module.UserGlobalHotRecallDao
}

func NewUserGlobalHotRecall(config recconf.RecallConfig) *UserGlobalHotRecall {
	recall := &UserGlobalHotRecall{
		BaseRecall:            NewBaseRecall(config),
		userGroupHotRecallDao: module.NewUserGlobalHotRecallDao(config),
	}
	return recall
}

func (r *UserGlobalHotRecall) GetCandidateItems(user *module.User, context *context.RecommendContext) (ret []*module.Item) {
	start := time.Now()
	if r.cache != nil {
		key := r.modelName
		cacheRet := r.cache.Get(key)
		switch itemStr := cacheRet.(type) {
		case []uint8:
			itemIds := strings.Split(string(itemStr), ",")
			for _, id := range itemIds {
				item := module.NewItem(id)
				item.ItemType = r.itemType
				item.RetrieveId = r.modelName
				ret = append(ret, item)
			}
		case string:
			itemIds := strings.Split(itemStr, ",")
			for _, id := range itemIds {
				item := module.NewItem(id)
				item.ItemType = r.itemType
				item.RetrieveId = r.modelName
				ret = append(ret, item)
			}
		default:
		}

		if len(ret) > 0 {
			log.Info(fmt.Sprintf("requestId=%s\tmodule=UserGlobalHotRecall\tfrom=cache\tcount=%d\tcost=%d", context.RecommendId, len(ret), utils.CostTime(start)))
			return
		}
	}

	ret = r.userGroupHotRecallDao.ListItemsByUser(user, context)
	if r.cache != nil && len(ret) > 0 {
		go func() {
			key := r.modelName
			var itemIds string
			for _, item := range ret {
				itemIds += string(item.Id) + ","
			}
			itemIds = itemIds[:len(itemIds)-1]
			cacheTime := r.cacheTime
			if cacheTime == 0 {
				cacheTime = 1800
			}
			if err := r.cache.Put(key, itemIds, time.Duration(cacheTime)*time.Second); err != nil {
				log.Error(fmt.Sprintf("requestId=%s\tmodule=UserGlobalHotRecall\terror=%v",
					context.RecommendId, err))
			}
		}()
	}
	log.Info(fmt.Sprintf("requestId=%s\tmodule=UserGlobalHotRecall\tname=%s\tcount=%d\tcost=%d", context.RecommendId, r.modelName, len(ret), utils.CostTime(start)))
	return
}
