package tao_vegetable

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

const (
	StoreOnline      = 1 //营业
	StoreOnlineWord  = "营业"
	StoreOffline     = -1 //不营业
	StoreOfflineWord = "不营业"
)

var (
	OnlineMap = map[string]int{
		StoreOnlineWord:  StoreOnline,
		StoreOfflineWord: StoreOffline,
	}
	chanType = int64(DefaultChannelSourceType)
)

// ShopUpdateStatus 更新渠道店营业状态
func (a *API) ShopUpdateStatus(storeID string, status int64) error {
	store := ability3156.NewAbility3156(&a.client)
	resp, err := store.AlibabaAelophyShopUpdatestatus(&request.AlibabaAelophyShopUpdatestatusRequest{
		ShopStatusUpdateRequest: &domain.AlibabaAelophyShopUpdatestatusShopStatusUpdateRequest{
			StoreId:           &storeID,
			ChannelSourceType: &chanType,
			Status:            &status,
		},
	}, a.token)
	if err != nil {
		return err
	}
	if resp.ApiResult.ErrMsg != nil {
		return fmt.Errorf("ShopUpdateStatus:requestId:" + resp.RequestId + "msg:" + *resp.ApiResult.ErrMsg)
	}
	return nil
}

// ShopUpdateRange 更新渠道店销售范围
func (a *API) ShopUpdateRange(storeID string, points []Points) error {
	var (
		store   = ability3156.NewAbility3156(&a.client)
		tPoints []domain.AlibabaAelophyShopUpdaterangePoint
		//tPoint  domain.AlibabaAelophyShopUpdaterangePoint
	)
	for _, v := range points {
		tLat := v.Lat
		tLng := v.Lng
		tPoint := domain.AlibabaAelophyShopUpdaterangePoint{
			Lat: &tLat,
			Lng: &tLng,
		}
		tPoints = append(tPoints, tPoint)
	}
	resp, err := store.AlibabaAelophyShopUpdaterange(&request.AlibabaAelophyShopUpdaterangeRequest{ShopRangeUpdateRequest: &domain.AlibabaAelophyShopUpdaterangeShopRangeUpdateRequest{
		StoreId: &storeID,
		ShopRanges: &[]domain.AlibabaAelophyShopUpdaterangeShopRange{
			{
				Points: &tPoints,
			},
		},
		ChannelSourceType: &chanType,
	},
	}, a.token)
	if err != nil {
		return err
	}
	if resp.ApiResult.ErrMsg != nil {
		return fmt.Errorf("ShopUpdateRange:requestId:" + resp.RequestId + "msg:" + *resp.ApiResult.ErrMsg)
	}
	return nil
}

// ShopUpdateInfo 更新渠道店基础信息，只能更新营业时间
//	08:00	营业开始时间(HH:mm)
func (a *API) ShopUpdateInfo(storeID, sTime, eTime string) error {
	store := ability3156.NewAbility3156(&a.client)
	resp, err := store.AlibabaAelophyShopUpdateinfo(&request.AlibabaAelophyShopUpdateinfoRequest{
		ShopInfoUpdateRequest: &domain.AlibabaAelophyShopUpdateinfoShopInfoUpdateRequest{
			StoreId:           &storeID,
			StartTime:         &sTime,
			EndTime:           &eTime,
			ChannelSourceType: &chanType,
		},
	}, a.token)
	if err != nil {
		return err
	}
	if resp.ApiResult.ErrMsg != nil {
		return fmt.Errorf("msg:" + *resp.ApiResult.ErrMsg)
	}
	return nil
}
