package ebai

import (
	"fmt"
	"time"

	"git.rosy.net.cn/jx-callback/business/jxutils"

	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
	"git.rosy.net.cn/jx-callback/globals/api"

	"git.rosy.net.cn/baseapi/platformapi/ebaiapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model"
)

type tShowStatusAndTypeInfo struct {
	ShowStatus   int
	ActivityType int
}

func ebaiPageActType2Jx(activityType int) int {
	if activityType == ebaiapi.PageActivityTypeSkuDiscount || activityType == ebaiapi.PageActivityTypeSkuDirectDown {
		return model.ActSkuDirectDown
	} else if activityType == ebaiapi.PageActivityTypeSkuSpecialPrice {
		return model.ActSkuSecKill
	}
	return 0
}

func ebaiPageActStatus2Jx(status int) int {
	if status == ebaiapi.PageActivityStatusWaiting || status == ebaiapi.PageActivityStatusOnGoing {
		return model.ActStatusCreated
	} else if status == ebaiapi.PageActivityStatusEnded {
		return model.ActStatusEnded
	}
	return model.ActStatusNA
}

func (c *PurchaseHandler) GetPageActList(ctx *jxcontext.Context, createdFrom time.Time) (actList []*model.Act2, err error) {
	var typeAndStatusList []*tShowStatusAndTypeInfo
	for _, showStatus := range []int{
		ebaiapi.PageActivityShowStatusWaiting,
		ebaiapi.PageActivityShowStatusOnGoing,
		// ebaiapi.PageActivityStatusEnded,
	} {
		for _, activityType := range []int{
			ebaiapi.PageActivityTypeSkuDiscount,
			ebaiapi.PageActivityTypeSkuSpecialPrice,
			ebaiapi.PageActivityTypeSkuDirectDown,
		} {
			typeAndStatusList = append(typeAndStatusList, &tShowStatusAndTypeInfo{
				ShowStatus:   showStatus,
				ActivityType: activityType,
			})
		}
	}

	task := tasksch.NewParallelTask("ebai GetPageActList", tasksch.NewParallelConfig().SetParallelCount(10), ctx,
		func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
			info := batchItemList[0].(*tShowStatusAndTypeInfo)
			list, err := api.EbaiAPI.BegetActivityList(info.ShowStatus, info.ActivityType, 0, api.EbaiAPI.GetSupplierID())
			if err == nil {
				retVal = list
			}
			return retVal, err
		}, typeAndStatusList)
	task.Run()
	list, err := task.GetResult(0)
	if len(list) > 0 {
		for _, v := range list {
			ebaiAct := v.(*ebaiapi.PageActItem)
			if utils.Timestamp2Time(ebaiAct.CreateTime).Sub(createdFrom) > 0 {
				act := &model.Act2{
					Act: model.Act{
						Name:       fmt.Sprintf("%s-%s", ebaiAct.ActivityName, ebaiAct.ActivityID),
						Type:       ebaiPageActType2Jx(ebaiAct.ActivityType),
						Status:     ebaiPageActStatus2Jx(ebaiAct.Status),
						Source:     ebaiAct.User,
						CreateType: model.ActCreateTypeSpider,
						BeginAt:    utils.Timestamp2Time(ebaiAct.StartTime),
						EndAt:      utils.Timestamp2Time(ebaiAct.EndTime),
						VendorMask: model.GetVendorMask(model.VendorIDEBAI),
						LimitCount: 1,
					},
					VendorID:      model.VendorIDEBAI,
					VendorOrgCode: api.EbaiAPI.GetSource(),
					VendorActID:   ebaiAct.ActivityID,
				}
				actList = append(actList, act)
			}
		}
	}
	return actList, err
}

func (c *PurchaseHandler) GetPageActSkuList(ctx *jxcontext.Context, vendorPageActID string) (actStoreSkuList []*model.ActStoreSku2, err error) {
	skus, err := api.EbaiAPI.BegetActSkuList(utils.Str2Int64(vendorPageActID), 0, api.EbaiAPI.GetSupplierID())
	if err != nil {
		return nil, err
	}
	for _, v := range skus {
		actStoreSku := &model.ActStoreSku2{
			ActStoreSku: model.ActStoreSku{
				Stock:         v.Stock,
				OriginalPrice: jxutils.StandardPrice2Int(v.OriginalPrice),
			},
			VendorStoreID:  utils.Int64ToStr(v.Wid),
			VendorSkuID:    v.SkuID,
			VendorActID:    v.ActivityID,
			ActualActPrice: jxutils.StandardPrice2Int(v.PromotionPrice),
		}
		actStoreSkuList = append(actStoreSkuList, actStoreSku)
	}
	return actStoreSkuList, nil
}
