package elmapi

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

// 没有创建与删除门店的概念
type StoreIdInfo struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
}

const (
	KeyProperties = "properties"
	KeyShopID     = "shopId"
	KeyShopIDs    = "shopIds"
)

// 获取商户账号信息，即得到所有账号下的门店相关信息
// https://open.shop.ele.me/openapi/apilist/eleme-user/eleme-user-getUser
func (a *API) GetUser() (map[string]interface{}, error) {
	result, err := a.AccessAPI("eleme.user.getUser", nil)
	if err == nil {
		return result.Result.(map[string]interface{}), nil
	}
	return nil, err
}

// 因GetUser得到的信息太杂，这个函数进一步给出账号与门店分开的信息
func (a *API) GetAccAndStores() ([]*StoreIdInfo, []*StoreIdInfo, error) {
	result, err := a.GetUser()
	if err == nil {
		result2 := result["authorizedShops"].([]interface{})
		accIds := make([]*StoreIdInfo, 0)
		storeIds := make([]*StoreIdInfo, 0)
		for _, v := range result2 {
			mapData := v.(map[string]interface{})
			id := int(utils.MustInterface2Int64(mapData["id"]))
			store := &StoreIdInfo{
				Id:   id,
				Name: utils.Interface2String(mapData["name"]),
			}
			if id >= 90000000 && id <= 99999999 { // 账号信息
				accIds = append(accIds, store)
			} else {
				storeIds = append(storeIds, store)
			}
		}
		return accIds, storeIds, nil
	}
	return nil, nil, err
}

// 查询店铺信息
// https://open.shop.ele.me/openapi/apilist/eleme-shop/eleme-shop-getShop
func (a *API) GetShop(shopId int) (map[string]interface{}, error) {
	result, err := a.AccessAPI("eleme.shop.getShop", utils.Params2Map(KeyShopID, shopId))
	if err == nil {
		return result.Result.(map[string]interface{}), nil
	}
	return nil, err
}

// 更新店铺基本信息
// https://open.shop.ele.me/openapi/apilist/eleme-shop/eleme-shop-updateShop
func (a *API) UpdateShop(shopId int, shopProperties map[string]interface{}) (map[string]interface{}, error) {
	result, err := a.AccessAPI("eleme.shop.updateShop", utils.MergeMaps(utils.Params2Map(KeyShopID, shopId), utils.Params2Map(KeyProperties, shopProperties)))
	if err == nil {
		return result.Result.(map[string]interface{}), nil
	}
	return nil, err
}

// 批量获取店铺简要
// https://open.shop.ele.me/openapi/apilist/eleme-shop/eleme-shop-mgetShopStatus
func (a *API) MgetShopStatus(shopIds []int) (map[string]interface{}, error) {
	result, err := a.AccessAPI("eleme.shop.mgetShopStatus", utils.Params2Map(KeyShopIDs, shopIds))
	if err == nil {
		return result.Result.(map[string]interface{}), nil
	}
	return nil, err
}

// 设置送达时间
// https://open.shop.ele.me/openapi/apilist/eleme-shop/eleme-shop-setDeliveryTime
func (a *API) SetDeliveryTime(shopId, deliveryBasicMins, deliveryAdjustMins int) error {
	_, err := a.AccessAPI("eleme.shop.setDeliveryTime", map[string]interface{}{
		KeyShopID:            shopId,
		"deliveryBasicMins":  deliveryBasicMins,
		"deliveryAdjustMins": deliveryAdjustMins,
	})
	return err
}

// 设置是否支持在线退单
// https://open.shop.ele.me/openapi/apilist/eleme-shop/eleme-shop-setOnlineRefund
func (a *API) SetOnlineRefund(shopId int, enabled bool) error {
	_, err := a.AccessAPI("eleme.shop.setOnlineRefund", map[string]interface{}{
		KeyShopID: shopId,
		"enable":  enabled,
	})
	return err
}

// 设置是否支持预定单及预定天数
// https://open.shop.ele.me/openapi/apilist/eleme-shop/eleme-shop-setBookingStatus
func (a *API) SetBookingStatus(shopId int, enabled bool, maxBookingDays int) error {
	_, err := a.AccessAPI("eleme.shop.setBookingStatus", map[string]interface{}{
		KeyShopID:        shopId,
		"enable":         enabled,
		"maxBookingDays": maxBookingDays,
	})
	return err
}
