package tao_vegetable

import (
	"fmt"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability2770"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability2770/domain"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability2770/request"
	"git.rosy.net.cn/baseapi/utils"
)

// ActCreateReq 1 活动详情
type ActCreateReq = domain.AlibabaRetailMarketingItemdiscountActivityCreateItemDiscountActivityOperateRequest

// ActSkuAddReq 2 活动商品详情
type ActSkuAddReq = domain.AlibabaRetailMarketingItemdiscountActivitySkuAddItemDiscountActivityElementOperateRequest
type SkuElement = domain.AlibabaRetailMarketingItemdiscountActivitySkuAddSkuActivityElementDto
type Limit = domain.AlibabaRetailMarketingItemdiscountActivitySkuAddLimitDto
type SkuChannelConfigs = domain.AlibabaRetailMarketingItemdiscountActivitySkuAddSkuChannelConfigDTO

// ActivityCreate 创建单品特价活动【同城零售】 1-1
func (a *API) ActivityCreate(param domain.AlibabaRetailMarketingItemdiscountActivityCreateItemDiscountActivityOperateRequest) (actID int64, err error) {
	act := ability2770.NewAbility2770(&a.client)
	resp, err := act.AlibabaRetailMarketingItemdiscountActivityCreate(&request.AlibabaRetailMarketingItemdiscountActivityCreateRequest{
		Param: &param,
	}, a.token)
	if err != nil {
		return 0, err
	}
	if resp.Result.ErrorMessage != nil {
		return 0, fmt.Errorf("ActivityCreate:requestId:" + resp.RequestId + "msg:" + *resp.Result.ErrorMessage)
	}
	return utils.Pointer2Int64(resp.Result.Data), nil
}

type skuAct struct {
	SkuID []string
	ActID int64
}

// ActivitySkuCreate 特价活动新增商品 1-2
func (a *API) ActivitySkuCreate(param domain.AlibabaRetailMarketingItemdiscountActivitySkuAddItemDiscountActivityElementOperateRequest) (error, *skuAct, []string) {
	act := ability2770.NewAbility2770(&a.client)
	resp, err := act.AlibabaRetailMarketingItemdiscountActivitySkuAdd(&request.AlibabaRetailMarketingItemdiscountActivitySkuAddRequest{
		Param: &param,
	}, a.token)
	if err != nil {
		return err, nil, *resp.Result.FailedSkuCodes
	}
	if resp.Result.ErrorMessage != nil {
		return fmt.Errorf("ActivityCreate:requestId:" + resp.RequestId + "msg:" + *resp.Result.ErrorMessage), nil, *resp.Result.FailedSkuCodes
	}
	successSkuCode := &skuAct{}
	failSkuCode := make([]string, 0)
	for _, v := range *param.SkuElements {
		if resp.Result.FailedSkuCodes == nil {
			successSkuCode.ActID = *param.ActId
			successSkuCode.SkuID = append(successSkuCode.SkuID, *v.SkuCode)
		} else {
			for _, i := range *resp.Result.FailedSkuCodes {
				if v.SkuCode == &i {
					continue
				}
				failSkuCode = append(failSkuCode, i)
			}
		}
	}
	if len(failSkuCode) > 0 {
		return nil, nil, failSkuCode
	} else {
		return nil, successSkuCode, nil
	}
}

// ActivityQuery 查询单品特价活动【同城零售】 2-1
func (a *API) ActivityQuery(actID int64, outActID string) (*domain.AlibabaRetailMarketingItemdiscountActivityQueryItemDiscountPromotionActivityDTO, error) {
	act := ability2770.NewAbility2770(&a.client)
	resp, err := act.AlibabaRetailMarketingItemdiscountActivityQuery(&request.AlibabaRetailMarketingItemdiscountActivityQueryRequest{
		Param0: &domain.AlibabaRetailMarketingItemdiscountActivityQueryItemDiscountActivityQueryRequest{
			ActId:    &actID,
			OutActId: &outActID,
		},
	}, a.token)
	if err != nil {
		return nil, err
	}
	if resp.ErrMessage != "" {
		return nil, fmt.Errorf("ActivityQuery:requestId:" + resp.RequestId + "msg:" + resp.ErrMessage)
	}
	//var retVal = &QueryActResp{}
	//retVal = ParseQueryActResp(resp.Data)
	return &resp.Data, nil
}

// ActivitySkuQuery 查询单品特价活动商品【同城零售】 2-2
func (a *API) ActivitySkuQuery(actID int64) ([]domain.AlibabaRetailMarketingItemdiscountSkuQuerySkuActivityElementDTO, error) {
	act := ability2770.NewAbility2770(&a.client)
	resp, err := act.AlibabaRetailMarketingItemdiscountSkuQuery(&request.AlibabaRetailMarketingItemdiscountSkuQueryRequest{
		Param0: &domain.AlibabaRetailMarketingItemdiscountSkuQueryItemDiscountActivitySkuQueryRequest{
			ActId: &actID,
			PageInfo: &domain.AlibabaRetailMarketingItemdiscountSkuQueryActivitySkuQueryDTO{
				Current:  &DefaultCurrent,
				PageSize: &DefaultPageSize,
			},
		},
	}, a.token)
	if err != nil {
		return nil, err
	}
	if resp.ErrMessage != "" {
		return nil, fmt.Errorf("ActivityCreate:requestId:" + resp.RequestId + "msg:" + resp.ErrMessage)
	}
	return resp.Data, nil
}

// ActivityDelete 删除单品特价活动【同城零售】 3-1
func (a *API) ActivityDelete(creatorID, creatorName string, actID int64) (err error) {
	act := ability2770.NewAbility2770(&a.client)
	resp, err := act.AlibabaRetailMarketingItemdiscountActivityDelete(&request.AlibabaRetailMarketingItemdiscountActivityDeleteRequest{
		Param: &domain.AlibabaRetailMarketingItemdiscountActivityDeleteItemDiscountActivityOperateRequest{
			CreatorId:   &creatorID,
			CreatorName: &creatorName,
			ActId:       &actID,
		},
	}, a.token)
	if err != nil {
		return err
	}
	if resp.Result.ErrorMessage != nil {
		return fmt.Errorf("ActivityDelete:requestId:" + resp.RequestId + "msg:" + *resp.Result.ErrorMessage)
	}
	return nil
}

// ActivitySkuDelete 删除特价活动商品 3-2
func (a *API) ActivitySkuDelete(param domain.AlibabaRetailMarketingItemdiscountActivitySkuDeleteItemDiscountActivityElementOperateRequest) error {
	act := ability2770.NewAbility2770(&a.client)
	resp, err := act.AlibabaRetailMarketingItemdiscountActivitySkuDelete(&request.AlibabaRetailMarketingItemdiscountActivitySkuDeleteRequest{
		Param: &param,
	}, a.token)
	if err != nil {
		return err
	}
	if resp.Result.ErrorMessage != nil {
		return fmt.Errorf("ActivityCreate:requestId:" + resp.RequestId + "msg:" + *resp.Result.ErrorMessage)
	}
	return nil
}

// ActivityUpdate 更新单品特价活动【同城零售】
func (a *API) ActivityUpdate(param domain.AlibabaRetailMarketingItemdiscountActivityUpdateItemDiscountActivityOperateRequest) (err error) {
	act := ability2770.NewAbility2770(&a.client)
	resp, err := act.AlibabaRetailMarketingItemdiscountActivityUpdate(&request.AlibabaRetailMarketingItemdiscountActivityUpdateRequest{
		Param: &param,
	}, a.token)
	if err != nil {
		return err
	}
	if resp.Result.ErrorMessage != nil {
		return fmt.Errorf("ActivityUpdate:requestId:" + resp.RequestId + "msg:" + *resp.Result.ErrorMessage)
	}
	return nil
}

//辅助函数

// ParseQueryActResp 解析查询活动返回值
func ParseQueryActResp(data domain.AlibabaRetailMarketingItemdiscountActivityQueryItemDiscountPromotionActivityDTO) *QueryActResp {
	retVal := &QueryActResp{
		DiscountType: utils.Pointer2Int64(data.DiscountType),
		OutActId:     utils.Pointer2String(data.OutActId),
		ActId:        utils.Pointer2Int64(data.ActId),
		ActivityName: utils.Pointer2String(data.ActivityName),
		Description:  utils.Pointer2String(data.Description),
		StartTime:    utils.Pointer2Int64(data.StartTime),
		EndTime:      utils.Pointer2Int64(data.EndTime),
		CreatorId:    utils.Pointer2String(data.CreatorId),
		CreatorName:  utils.Pointer2String(data.CreatorName),
	}
	if data.Weekdays != nil {
		retVal.Weekdays = *data.Weekdays
	}
	if data.EveryDayPeriods != nil {
		retVal.EveryDayPeriods = *data.EveryDayPeriods
	}
	if data.Terminals != nil {
		retVal.Terminals = *data.Terminals
	}
	if data.StoreIds != nil {
		retVal.StoreIds = *data.StoreIds
	}
	if data.OuterStoreIds != nil {
		retVal.OuterStoreIds = *data.OuterStoreIds
	}
	if data.MemberCrowdCodes != nil {
		retVal.MemberCrowdCodes = *data.MemberCrowdCodes
	}
	if data.Limit != nil {
		retVal.Limit = ActivityQueryLimit{
			TotalLimitCnt:      utils.Pointer2Int64(data.Limit.TotalLimitCnt),
			DailyTotalLimitCnt: utils.Pointer2Int64(data.Limit.DailyTotalLimitCnt),
			UserTotalLimitCnt:  utils.Pointer2Int64(data.Limit.UserTotalLimitCnt),
			UserDailyLimitCnt:  utils.Pointer2Int64(data.Limit.UserDailyLimitCnt),
			OrderLimitCnt:      utils.Pointer2Int64(data.Limit.OrderLimitCnt),
		}
	}
	fmt.Println(utils.Format4Output(retVal, false))
	return retVal
}
