package service

import (
	"fmt"
	"time"

	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/context"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/filter"
	"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/sort"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/utils"
)

type RecommendService struct {
}

func (s *RecommendService) GetUID(context *context.RecommendContext) module.UID {
	uid := context.GetParameter("uid")
	if uid == nil {
		uid = ""
	}

	userId := module.UID(uid.(string))
	return userId
}

func (s *RecommendService) Filter(uid module.UID, items []*module.Item, context *context.RecommendContext) []*module.Item {
	start := time.Now()
	filterData := filter.FilterData{Data: items, Uid: uid, Context: context}

	filter.Filter(&filterData, "")
	log.Info(fmt.Sprintf("requestId=%s\tmodule=Filter\tcost=%d", context.RecommendId, utils.CostTime(start)))
	return filterData.Data.([]*module.Item)
}

func (s *RecommendService) Sort(items []*module.Item, context *context.RecommendContext) []*module.Item {
	sortData := sort.SortData{Data: items, Context: context}

	sort.Sort(&sortData, "")
	return sortData.Data.([]*module.Item)
}

func (s *RecommendService) PreSort(items []*module.Item, context *context.RecommendContext) []*module.Item {
	sortData := sort.SortData{Data: items, Context: context}

	sort.Sort(&sortData, "_PreSort")
	return sortData.Data.([]*module.Item)
}
