package mtwmapi

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

//#region 买赠活动 (未测试,暂无门店有次活动)

// BatchQueryBuyGiftsAll 批量查询门店买赠活动
func (a *API) BatchQueryBuyGiftsAll(appPoiCode string) ([]*BatchQueryBuyGiftsAll, error) {
	params := map[string]interface{}{
		KeyAppPoiCode: appPoiCode,
		"offset":      0,
		"limit":       200,
	}
	i := 0

	buyGifts := make([]*BatchQueryBuyGiftsAll, 0, 0)

	for {
		params["offset"] = i
		result, err := a.AccessAPI2("act/buygifts/list", true, params, "data", "")
		if err != nil {
			return nil, err
		}
		buyGift := make([]*BatchQueryBuyGiftsAll, 0, 0)
		resultStr, err := json.Marshal(result)
		if err != nil {
			return nil, err
		}
		if err = json.Unmarshal([]byte(resultStr), &buyGift); err != nil {
			return nil, err
		}
		buyGifts = append(buyGifts, buyGift...)
		if len(buyGift) < 200 {
			break
		}
		i++
	}

	return buyGifts, nil
}

// BatchCreateBuyGifts 批量创建满赠活动,最大每组200数据
func (a *API) BatchCreateBuyGifts(appPoiCode string, param []*BatchCreateBuyGiftsParam) (interface{}, error) {
	params := map[string]interface{}{
		KeyAppPoiCode: appPoiCode,
		"act_data":    string(utils.MustMarshal(param)),
	}

	result, err := a.AccessAPI2("act/buygifts/batchsave", false, params, resultKeySuccessMsg, "")

	return result, err
}

//#endregion

//#region 满减(商品满减/门店满减)活动

// BatchGetDiscountList 批量获取门店满减活动列表 actType = 0 ,暂时不支持actType = 1 的返回,因为返回值里面没有商品详情
func (a *API) BatchGetDiscountList(appPoiCode string, actType int) ([]*GetDiscountList, error) {
	result, err := a.AccessAPI2("act/full/discount/list", false, map[string]interface{}{KeyAppPoiCode: appPoiCode, "act_type": actType}, "success_list", "")
	if err != nil {
		return nil, err
	}
	discountList := make([]*GetDiscountList, 0, 0)
	byteData, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(byteData, &discountList); err != nil {
		return nil, err
	}

	return discountList, nil
}

// BatchCreateDiscountList 批量创建满减活动
// 下面接口暂时不支持actType = 1 的活动创建因为上面接口查询返回时没有返回商品ID的详细信息,没返回actDetails 的信息所以无法创建
func (a *API) BatchCreateDiscountList(param *CreateDiscountList) (*CreateDiscountResult, error) {

	result, err := a.AccessAPI2("act/full/discount/batchsave", false, utils.Struct2Map(param, "", false), "", "")
	if err != nil {
		return nil, err
	}

	createDiscountResult := &CreateDiscountResult{}
	byteData, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(byteData, &createDiscountResult); err != nil {
		return nil, err
	}

	return createDiscountResult, nil
}

//#endregion

//#region 运费满减活动,只能获取阶梯运费活动,不能获取立减活动,且两个活动互斥,同一时间段只生效一个活动

// BatchGetShippingFee 查询运费满减活动配置
func (a *API) BatchGetShippingFee(appPoiCode string) (*ShippingFeeResult, error) {
	result, err := a.AccessAPI2("act/full/discount/shippingfee/list", true, map[string]interface{}{KeyAppPoiCode: appPoiCode}, "", "")
	if err != nil {
		return nil, err
	}
	shippingFee := &ShippingFeeResult{}
	byteData, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(byteData, &shippingFee); err != nil {
		return nil, err
	}

	return shippingFee, nil
}

// BatchCreateShippingFee 创建运费满减活动
func (a *API) BatchCreateShippingFee(AppPoiCode string, param []*ShippingFeeBatchCreateActData) (*ShippingResultData, error) {
	byteData, err := json.Marshal(param)
	if err != nil {
		return nil, err
	}
	paramMap := map[string]interface{}{
		AppPoiCode: AppPoiCode,
		"act_data": string(byteData),
	}
	result, err := a.AccessAPI2("act/full/discount/shippingfee/batchsave", false, paramMap, "", "")
	if err != nil {
		return nil, err
	}

	shippingFeeCreate := &ShippingResultData{}
	byteResult, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(byteResult, &shippingFeeCreate); err != nil {
		return nil, err
	}

	return shippingFeeCreate, nil
}

//#endregion

//#region 获取门店折扣爆品活动

// QueryProductsAll 获取门店折扣爆品活动列表
func (a *API) QueryProductsAll(poiCode string, actType int) (actList []*GetStoreRetailDiscountActData, err error) {
	return a.RetailDiscountList(poiCode, actType)
}

// CreateProductsAct 创建门店折扣爆品活动
func (a *API) CreateProductsAct(poiCode string, actType int, actData []*RetailDiscountActData) (actResult []*RetailDiscountActResult, failedList []*AppFoodResult, err error) {
	return a.RetailDiscountBatchSave2(poiCode, actType, actData)
}

//##endregion

//#region 查询商家券(店内发券)活动

// QueryStoreCoupon 查询商家券店内发券
func (a *API) QueryStoreCoupon(poiCode string) (couponActList []*CouponActInfo, err error) {
	couponActList = make([]*CouponActInfo, 0, 0)
	pageNum := 1
	pageSize := 20
	actType := 1
	for {
		actList, err := a.InStoreCouponList(poiCode, actType, pageNum, pageSize)
		if err != nil {
			return nil, err
		}
		if len(actList) != 0 {
			couponActList = append(couponActList, actList...)
		}

		if len(actList) < pageSize && actType == 1 {
			pageNum = 1
			actType = 2
		} else if len(actList) < pageSize && actType == 2 {
			break
		} else {
			pageNum += 1
		}
	}
	// 暂时只查询单店商家券
	return couponActList, nil
}

// CreateStoreCouponBatch 批量新增单店店内商家券
func (a *API) CreateStoreCouponBatch(poiCode string, limitTime *LimitTime, couponInfoList []*CouponInfo) (couponResultList []*CouponResult, err error) {
	return a.InStoreCouponBatchSave(poiCode, limitTime, couponInfoList)
}

//#endregion

//#region X件优惠活动

// QueryBundlesAct 本接口用于商家查询门店内X元M件、X件Y折活动的基本信息。 actType，0-X元M件，2-X件Y折，不填默认为0（待上线）
func (a *API) QueryBundlesAct(poiCode string, actType int) ([]*BundlesSuccessListDetail, error) {
	result, err := a.AccessAPI2("act/item/bundles/list", true, map[string]interface{}{KeyAppPoiCode: poiCode, "type": actType}, "success_list", "")
	if err != nil {
		return nil, err
	}
	bundlesList := make([]*BundlesSuccessListDetail, 0, 0)

	byteData, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(byteData, &bundlesList); err != nil {
		return nil, err
	}

	return bundlesList, nil
}

func (a *API) QueryBundlesSku(appPoiCode string, actId string) ([]*BundlesSkuSuccessListDetail, error) {
	result, err := a.AccessAPI2("act/item/bundles/foods/list", true, map[string]interface{}{KeyAppPoiCode: appPoiCode, "act_id": actId}, "success_list", "")
	if err != nil {
		return nil, err
	}
	resultByte, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}
	resultData := make([]*BundlesSkuSuccessListDetail, 0, 0)

	if err = json.Unmarshal(resultByte, &resultData); err != nil {
		return nil, err
	}

	return resultData, nil
}

// CreateBundlesAct 创建X件优惠活动
func (a *API) CreateBundlesAct(poiCode string, param *CreateBundlesActParam) ([]*BundlesSuccessListDetail, error) {
	paramByte, err := json.Marshal(param)
	if err != nil {
		return nil, err
	}

	result, err := a.AccessAPI2("act/item/bundles/save", true, map[string]interface{}{KeyAppPoiCode: poiCode, "act_data": string(paramByte)}, "success_list", "")
	if err != nil {
		return nil, err
	}
	bundlesList := make([]*BundlesSuccessListDetail, 0, 0)

	byteData, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(byteData, &bundlesList); err != nil {
		return nil, err
	}

	return bundlesList, nil
}

//#endregion

//#region 加价购 [暂时不对接,获取不到商品的换购价格和库存]

// QueryStoreRepurchase 查询门店所有加价购活动信息
func (a *API) QueryStoreRepurchase(poiCode string) ([]*RepurchaseResult, error) {
	result, err := a.AccessAPI2("act/markup/repurchase/list", true, map[string]interface{}{KeyAppPoiCode: poiCode}, "success_list", "")
	if err != nil {
		return nil, err
	}
	repurchaseList := make([]*RepurchaseResult, 0, 0)

	byteData, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(byteData, &repurchaseList); err != nil {
		return nil, err
	}

	return repurchaseList, nil
}

// QueryStoreSkuAct 查询门店商品层次的活动
func (a *API) QueryStoreSkuAct() {
	// act/markup/repurchase/main/food/list
}

// CreateStoreRepurchase 创建加价活动
func (a *API) CreateStoreRepurchase(poiCode string, param *RepurchaseCreate) ([]*RepurchaseCreateResult, error) {
	result, err := a.AccessAPI2("act/markup/repurchase/batchsave", false, map[string]interface{}{KeyAppPoiCode: poiCode, "act_data": param}, "success_list", "")
	if err != nil {
		return nil, err
	}

	byteResult, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}

	data := make([]*RepurchaseCreateResult, 0, 0)
	if err := json.Unmarshal(byteResult, &data); err != nil {
		return nil, err
	}

	return data, nil
}

//#endregion

//#region 商品券活动

// CreateCouponAct 创建门店商品券活动
func (a *API) CreateCouponAct(param *CreateCouponAct) ([]*CreateStoreCouponActResult, error) {
	byteParam, err := json.Marshal(param)
	if err != nil {
		return nil, err
	}
	result, err := a.AccessAPI2("act/goods/coupon/save", false, map[string]interface{}{"goods_coupon_data": string(byteParam)}, "success_list", "")
	if err != nil {
		return nil, err
	}

	resultData, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}
	successList := make([]*CreateStoreCouponActResult, 0, 0)
	if err := utils.UnmarshalUseNumber(resultData, &successList); err != nil {
		return nil, err
	}

	return successList, nil
}

// GetStoreCouponAct 获取门店商品券活动
func (a *API) GetStoreCouponAct(param *GetStoreCouponActParam) ([]*GetStoreCouponActSuccessList, error) {
	paramMap := utils.Struct2Map(param, "", false)
	result, err := a.AccessAPI2("act/goods/coupon/list", true, paramMap, "success_list", "")
	if err != nil {
		return nil, err
	}

	data := make([]*GetStoreCouponActSuccessList, 0, 0)
	for _, v := range result.([]interface{}) {
		coupon := &GetStoreCouponActSuccessList{}
		storeCouponAct, _ := json.Marshal(v)
		if err = json.Unmarshal(storeCouponAct, coupon); err != nil {
			return nil, err
		}

		//if len(coupon.ActPriceCouponInfo) != 0 || coupon.ActPriceCouponInfo != nil {
		//	actPriceCouponInfoObj := make([]GetStoreCouponActSuccessListActPriceCouponInfo, 0, 0)
		//	for _, apci := range coupon.ActPriceCouponInfo {
		//		apciObj := GetStoreCouponActSuccessListActPriceCouponInfo{}
		//		if err = json.Unmarshal([]byte(apci), apciObj); err != nil {
		//			return nil, err
		//		}
		//		actPriceCouponInfoObj = append(actPriceCouponInfoObj, apciObj)
		//	}
		//	coupon.ActPriceCouponInfoObj = actPriceCouponInfoObj
		//}
		//if len(coupon.DiscountCouponInfo) != 0 || coupon.DiscountCouponInfo != nil {
		//	discountCouponInfo := make([]GetStoreCouponActSuccessListDiscountCouponInfo, 0, 0)
		//	for _, dci := range coupon.DiscountCouponInfo {
		//		dciObj := GetStoreCouponActSuccessListDiscountCouponInfo{}
		//		if err = json.Unmarshal([]byte(dci), dciObj); err != nil {
		//			return nil, err
		//		}
		//		discountCouponInfo = append(discountCouponInfo, dciObj)
		//	}
		//	coupon.DiscountCouponInfoObj = discountCouponInfo
		//}

		data = append(data, coupon)
	}

	return data, nil
}

// GetCouponActSkuList 批量查询商品券活动中的商品
func (a *API) GetCouponActSkuList(poiCode, actIds string) ([]*CouponActSkuListSuccessList, error) {
	result, err := a.AccessAPI2("act/goods/coupon/goodslist", true, map[string]interface{}{"app_poi_code": poiCode, "act_ids": actIds}, "success_list", "")
	if err != nil {
		return nil, err
	}

	successList := make([]*CouponActSkuListSuccessList, 0, 0)
	resultByte, err := json.Marshal(result)
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(resultByte, &successList); err != nil {
		return nil, err
	}

	return successList, nil
}

//#endregion

//#region 第N件优惠活动

// GetStoreNDiscount 获取门店第N件优惠活动
func (a *API) GetStoreNDiscount(poiCode string) ([]*GetStoreXDiscountSuccessList, error) {
	result, err := a.AccessAPI2("act/nth/discount/info", true, map[string]interface{}{KeyAppPoiCode: poiCode}, "success_list", "")
	if err != nil {
		return nil, err
	}

	data := make([]*GetStoreXDiscountSuccessList, 0, 0)
	if err := json.Unmarshal([]byte(result.(string)), &data); err != nil {
		return nil, err
	}

	return data, nil
}

// CreateStoreNDiscount 创建门店地N件优惠活动
func (a *API) CreateStoreNDiscount(poiCode string, param *CreateStoreXDiscountParam) ([]*CreateStoreXDiscountResult, error) {
	paramStr, err := json.Marshal(param)
	if err != nil {
		return nil, err
	}

	result, err := a.AccessAPI2("act/nth/discount/save", true, map[string]interface{}{KeyAppPoiCode: poiCode, "act_data": string(paramStr)}, "success_list", "")
	if err != nil {
		return nil, err
	}

	return result.([]*CreateStoreXDiscountResult), nil
}

//#endregion
