package filter

import (
	"errors"
	"fmt"
	"time"

	"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 ItemStateFilter struct {
	itemStateDao module.ItemStateFilterDao
}

func NewItemStateFilter(config recconf.FilterConfig) *ItemStateFilter {
	filter := ItemStateFilter{
		itemStateDao: module.NewItemStateFilterDao(config),
	}

	return &filter
}
func (f *ItemStateFilter) Filter(filterData *FilterData) error {
	if _, ok := filterData.Data.([]*module.Item); !ok {
		return errors.New("filter data type error")

	}
	return f.doFilter(filterData)
}

func (f *ItemStateFilter) doFilter(filterData *FilterData) error {
	start := time.Now()
	items := filterData.Data.([]*module.Item)

	newItems := f.itemStateDao.Filter(filterData.Uid, items)

	filterData.Data = newItems
	log.Info(fmt.Sprintf("requestId=%s\tevent=ItemStateFilter\tcount=%d\tcost=%d", filterData.Context.RecommendId, len(newItems), utils.CostTime(start)))
	return nil
}
