package ebaiapi

import (
	"git.rosy.net.cn/baseapi/utils"
)

const (
	PageActivityShowStatusWaiting = 3 // 待生效
	PageActivityShowStatusOnGoing = 4 // 进行中
	PageActivityShowStatusEnded   = 5 // 已结束

	PageActivityStatusWaiting = 1 // 待生效
	PageActivityStatusOnGoing = 2 // 进行中
	PageActivityStatusEnded   = 3 // 结束

	PageActivityTypeSkuDiscount     = 64 // 商品折扣
	PageActivityTypeSkuSpecialPrice = 32 // 商品特价
	PageActivityTypeSkuDirectDown   = 2  // 商品直降
)

type PageActItem struct {
	ActivityID       string `json:"activity_id"`
	ActivityName     string `json:"activity_name"`
	ActivityType     int    `json:"activity_type"`
	ActivityPlatform string `json:"activity_platform"`
	StartTime        int64  `json:"start_time"`
	EndTime          int64  `json:"end_time"`
	User             string `json:"user"`
	Status           int    `json:"status"`
	StopTime         int64  `json:"stop_time"`
	CreatorType      int    `json:"creator_type"`
	CreateTime       int64  `json:"create_time"`
	ChildType        string `json:"child_type"`
	CreatedAt        string `json:"created_at"`
	ShopNum          int    `json:"shop_num,omitempty"`
	IsCanDelete      int    `json:"is_can_delete"`
	IsRisk           int    `json:"is_risk"`
	StopShopCount    int    `json:"stop_shop_count"`
	SkuImportStatus  int    `json:"sku_import_status"`
}

type PageActListInfo struct {
	Total        int            `json:"total"`
	Curpage      int            `json:"curpage"`
	Perpage      int            `json:"perpage"`
	ActivityList []*PageActItem `json:"activity_list"`
}

type PageActSku struct {
	ID              string  `json:"id"`
	ActivityID      string  `json:"activity_id"`
	ActivityType    int     `json:"activity_type"`
	SkuID           string  `json:"sku_id"`
	SupplierID      int     `json:"supplier_id"`
	Wid             int64   `json:"wid"`
	ShopName        string  `json:"shop_name"`
	UpcID           string  `json:"upc_id"`
	Upc             string  `json:"upc"`
	UpcName         string  `json:"upc_name"`
	OriginalPrice   float64 `json:"original_price"`
	PromotionPrice  float64 `json:"promotion_price"`
	FreeSkuName     string  `json:"free_sku_name"`
	PlatformSubsidy string  `json:"platform_subsidy"`
	ShopSubsidy     string  `json:"shop_subsidy"`
	Stock           int     `json:"stock"`
	Status          int     `json:"status"`
	CreateTime      int64   `json:"create_time"`
	UpdateTime      int64   `json:"update_time"`
	RejectReason    string  `json:"reject_reason"`
	SupplierName    string  `json:"supplier_name"`
	IsRecruit       string  `json:"is_recruit"`
	Cat1ID          string  `json:"cat1_id"`
	Cat2ID          string  `json:"cat2_id"`
	Cat3ID          string  `json:"cat3_id"`
	WeightFlag      string  `json:"weight_flag"`
	RealStock       int     `json:"real_stock"`
	Photos          string  `json:"photos"`
	ActivityTotal   int     `json:"activity_total"`
}
type PageActivityInfo struct {
	Total       int           `json:"total"`
	List        []*PageActSku `json:"list"`
	IsCanDelete int           `json:"is_can_delete"`
	Curpage     int           `json:"curpage"`
	Perpage     int           `json:"perpage"`
}

func (a *API) BegetActivityList(showStatus, activityType int, baiduShopID, supplierID int64) (actList []*PageActItem, err error) {
	// pageSize := 20
	// pageNo := 1
	// params := map[string]interface{}{
	// 	"perpage":       pageSize,
	// 	"show_status":   showStatus,
	// 	"activity_type": activityType, //不设置这个值，缺省为：PageActivityTypeSkuDirectDown
	// }
	// if baiduShopID > 0 {
	// 	params["wid"] = baiduShopID
	// }
	// if supplierID > 0 {
	// 	params[KeySupplierID] = supplierID
	// }
	// for {
	// 	params["curpage"] = pageNo
	// 	retVal, err2 := a.AccessStorePage("commodity/activity/begetactivitylist", "", params, false)
	// 	if err = err2; err == nil {
	// 		var listInfo *PageActListInfo
	// 		if err = utils.Map2StructByJson(retVal, &listInfo, true); err != nil {
	// 			return nil, err
	// 		}
	// 		if listInfo != nil {
	// 			actList = append(actList, listInfo.ActivityList...)
	// 		}
	// 		if listInfo == nil || len(actList) >= listInfo.Total {
	// 			return actList, nil
	// 		}
	// 		pageNo++
	// 	} else {
	// 		return nil, err
	// 	}
	// }
	return nil, err
}

func (a *API) BegetActSkuList(activityID, baiduShopID, supplierID int64) (actSkuList []*PageActSku, err error) {
	pageSize := 100
	pageNo := 1
	params := map[string]interface{}{
		"perpage":     pageSize,
		"activity_id": activityID,
	}
	if baiduShopID > 0 {
		params["wid"] = baiduShopID
	}
	if supplierID > 0 {
		params["wid"] = supplierID
	}
	for {
		params["curpage"] = pageNo
		retVal, err2 := a.AccessStorePage("commodity/activity/begetactskulist", "", params, false)
		if err = err2; err == nil {
			var pageActivityInfo *PageActivityInfo
			if err = utils.Map2StructByJson(retVal, &pageActivityInfo, true); err != nil {
				return nil, err
			}
			if pageActivityInfo != nil {
				actSkuList = append(actSkuList, pageActivityInfo.List...)
			}
			if pageActivityInfo == nil || len(actSkuList) >= pageActivityInfo.Total {
				return actSkuList, nil
			}
			pageNo++
		} else {
			return nil, err
		}
	}
	return nil, err
}
