package partner

import (
	"time"

	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
)

type IActManager interface {
	IsVendorActExist(ctx *jxcontext.Context, vendorActID string, vendorID int) (isExist bool)
	CreateActFromVendor(ctx *jxcontext.Context, act2 *model.Act2, actStoreSku []*model.ActStoreSku2) (actID int, err error)
}

type IPurchasePlatformActHandler interface {
	// // 如果是单品级活动，actOrderRules为空
	// // 如果是订单级活动，actStoreSku可以为空（表示不限制SKU）
	// CreateAct(ctx *jxcontext.Context, parentTask tasksch.ITask, act *model.Act2, actOrderRules []*model.ActOrderRule, actStoreSku []*model.ActStoreSku2) (err error)
	// UpdateAct(ctx *jxcontext.Context, parentTask tasksch.ITask, act *model.Act2, actOrderRules []*model.ActOrderRule, actStoreSku []*model.ActStoreSku2) (err error)
	// // 取消整个京西活动
	// CancelAct(ctx *jxcontext.Context, parentTask tasksch.ITask, act *model.Act2, actStoreSku []*model.ActStoreSku2) (err error)

	SyncAct(ctx *jxcontext.Context, parentTask tasksch.ITask, act *model.Act2, actOrderRules []*model.ActOrderRule, actStoreSkuList []*model.ActStoreSku2) (err error)

	//活动丰富度
	GetActAmple(ctx *jxcontext.Context, vendorStoreID, vendorOrgCode string) (ample int, err error)
}

type IPurchasePlatformPageActHandler interface {
	GetPageActList(ctx *jxcontext.Context, createdFrom time.Time) (actList []*model.Act2, err error)
	GetPageActSkuList(ctx *jxcontext.Context, vendorPageActID string) (actStoreSkuList []*model.ActStoreSku2, err error)
}

var (
	CurActManager IActManager
)

func InitActManager(p IActManager) {
	CurActManager = p
}

func SplitActStoreSku(actStoreSkuList []*model.ActStoreSku2) (actStoreSkuMap map[int][]*model.ActStoreSku2) {
	actStoreSkuMap = make(map[int][]*model.ActStoreSku2)
	for _, v := range actStoreSkuList {
		actStoreSkuMap[v.StoreID] = append(actStoreSkuMap[v.StoreID], v)
	}
	return actStoreSkuMap
}

func SplitActStoreSku2List(actStoreSkuList []*model.ActStoreSku2) (actStoreSkuListList [][]*model.ActStoreSku2) {
	actStoreSkuMap := SplitActStoreSku(actStoreSkuList)
	for _, v := range actStoreSkuMap {
		actStoreSkuListList = append(actStoreSkuListList, v)
	}
	return actStoreSkuListList
}

func Act2ActMap(act *model.Act2) (actMap *model.ActMap) {
	actMap = &model.ActMap{}
	actMap.ID = act.MapID
	return actMap
}

func ActStoreSku2ActStoreSkuMap(actStoreSku *model.ActStoreSku2) (actStoreSkuMap *model.ActStoreSkuMap) {
	actStoreSkuMap = &model.ActStoreSkuMap{
		ModelIDCULD: actStoreSku.ModelIDCULD,
		BindID:      actStoreSku.MapID,

		ActID:          actStoreSku.ActID,
		StoreID:        actStoreSku.StoreID,
		SkuID:          actStoreSku.SkuID,
		VendorID:       actStoreSku.VendorID,
		VendorActID:    actStoreSku.VendorActID,
		SyncStatus:     actStoreSku.SyncStatus,
		VendorPrice:    actStoreSku.VendorPrice,
		ActualActPrice: actStoreSku.ActualActPrice,

		EarningPrice: actStoreSku.EarningPrice,
	}
	actStoreSkuMap.ID = actStoreSku.MapID
	return actStoreSkuMap
}

func Act2Update(ctx *jxcontext.Context, act *model.Act2, syncStatus int) (item *dao.KVUpdateItem) {
	kvs := map[string]interface{}{
		model.FieldSyncStatus:   0,
		model.FieldUpdatedAt:    time.Now(),
		model.FieldLastOperator: ctx.GetUserName(),
	}
	if syncStatus == model.SyncFlagDeletedMask {
		kvs[model.FieldDeletedAt] = time.Now()
	} else if syncStatus == model.SyncFlagNewMask {
		kvs[model.FieldVendorActID] = act.VendorActID
	}
	item = &dao.KVUpdateItem{
		Item: Act2ActMap(act),
		KVs:  kvs,
	}
	return item
}

func ActStoreSku2Update(ctx *jxcontext.Context, actStoreSkuList []*model.ActStoreSku2, syncStatus int) (items []*dao.KVUpdateItem) {
	for _, v := range actStoreSkuList {
		v.SyncStatus = 0
		v.UpdatedAt = time.Now()
		v.LastOperator = ctx.GetUserName()
		kvs := map[string]interface{}{
			model.FieldSyncStatus:   v.SyncStatus,
			model.FieldUpdatedAt:    v.UpdatedAt,
			model.FieldLastOperator: v.LastOperator,
		}
		if syncStatus == model.SyncFlagDeletedMask {
			v.DeletedAt = time.Now()
			kvs[model.FieldDeletedAt] = v.DeletedAt
		} else if syncStatus == model.SyncFlagNewMask {
			kvs[model.FieldVendorActID] = v.VendorActID
		}
		items = append(items, &dao.KVUpdateItem{
			Item: ActStoreSku2ActStoreSkuMap(v),
			KVs:  kvs,
		})
	}
	return items
}

func GetVendorIDsFromActMap(actMap map[int]*model.Act2) (vendorIDs []int) {
	for vendorID := range actMap {
		vendorIDs = append(vendorIDs, vendorID)
	}
	return vendorIDs
}
