package mtwmapi

import (
	"git.rosy.net.cn/jx-callback/globals"
	"time"

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

type FoodCategoryInfo struct {
	Name     string    `json:"name"`
	Sequence int       `json:"sequence"`
	CTime    time.Time `json:"ctime"`
	UTime    time.Time `json:"utime"`
}

func (a *API) FoodCatUpdate(poiCode, originName, name string, sequence int) (err error) {
	params := map[string]interface{}{
		KeyAppPoiCode:          poiCode,
		"category_name_origin": originName,
		"sequence":             sequence,
	}
	if name != "" {
		params["category_name"] = name
	}
	_, err = a.AccessAPI("foodCat/update", false, params)
	return err
}

func (a *API) FoodCatDelete(poiCode, name string) (err error) {
	_, err = a.AccessAPI("foodCat/delete", false, map[string]interface{}{
		KeyAppPoiCode:   poiCode,
		"category_name": name,
	})
	return err
}

func (a *API) FoodInitData(poiCode, foodCode string, params map[string]interface{}) (err error) {
	_, err = a.AccessAPI("food/initdata", false, utils.MergeMaps(map[string]interface{}{
		KeyAppPoiCode:  poiCode,
		KeyAppFoodCode: foodCode,
	}, params))
	return err
}

func (a *API) FoodDelete(poiCode, foodCode string) (err error) {
	_, err = a.AccessAPI("food/delete", false, map[string]interface{}{
		KeyAppPoiCode:  poiCode,
		KeyAppFoodCode: foodCode,
	})
	return err
}

// offset 从0开始，limit最大不能超过200
func (a *API) FoodList(poiCode string, offset, limit int) (foodList []map[string]interface{}, err error) {
	result, err := a.AccessAPI("food/list", true, map[string]interface{}{
		KeyAppPoiCode: poiCode,
		"offset":      offset,
		"limit":       limit,
	})
	if err == nil {
		return utils.Slice2MapSlice(result.([]interface{})), nil
	}
	return nil, err
}

func (a *API) FoodBatchInitData(poiCode string, foodData map[string]interface{}) (err error) {
	_, err = a.AccessAPI("food/batchinitdata", false, map[string]interface{}{
		KeyAppPoiCode: poiCode,
		"food_data":   foodData,
	})
	return err
}

func (a *API) FoodCatList(poiCode string) (foodCatList []*FoodCategoryInfo, err error) {
	result, err := a.AccessAPI("foodCat/list", true, map[string]interface{}{
		KeyAppPoiCode: poiCode,
	})
	if err == nil {
		catListTmp := result.([]interface{})
		foodCatList = make([]*FoodCategoryInfo, len(catListTmp))
		for k, v := range catListTmp {
			cat := v.(map[string]interface{})
			foodCatList[k] = &FoodCategoryInfo{
				Name:     utils.Interface2String(cat["name"]),
				Sequence: int(utils.MustInterface2Int64(cat["sequence"])),
				CTime:    time.Unix(utils.MustInterface2Int64(cat["ctime"]), 0),
				UTime:    time.Unix(utils.MustInterface2Int64(cat["utime"]), 0),
			}
		}
		return foodCatList, nil
	}
	return nil, err
}

func (a *API) FoodSkuSave(poiCode, foodCode string, skus []map[string]interface{}) (err error) {
	_, err = a.AccessAPI("food/sku/save", false, map[string]interface{}{
		KeyAppPoiCode:  poiCode,
		KeyAppFoodCode: foodCode,
		"skus":         skus,
	})
	return err
}

func (a *API) FoodSkuDelete(poiCode, foodCode, skuID string) (err error) {
	_, err = a.AccessAPI("food/sku/delete", false, map[string]interface{}{
		KeyAppPoiCode:  poiCode,
		KeyAppFoodCode: foodCode,
		"sku_id":       skuID,
	})
	return err
}

func (a *API) FoodSkuPrice(poiCode string, foodData []map[string]interface{}) (err error) {
	_, err = a.AccessAPI("food/sku/price", false, map[string]interface{}{
		KeyAppPoiCode: poiCode,
		"food_data":   foodData,
	})
	return err
}

func (a *API) FoodSkuStock(poiCode string, foodData []map[string]interface{}) (err error) {
	_, err = a.AccessAPI("food/sku/stock", false, map[string]interface{}{
		KeyAppPoiCode: poiCode,
		"food_data":   foodData,
	})
	return err
}

func (a *API) FoodGet(poiCode, foodCode string) (food map[string]interface{}, err error) {
	result, err := a.AccessAPI("food/get", true, map[string]interface{}{
		KeyAppPoiCode:  poiCode,
		KeyAppFoodCode: foodCode,
	})
	if err == nil {
		return result.(map[string]interface{}), nil
	}
	return nil, err
}

func (a *API) FoodSkuSellStatus(poiCode string, foodData []map[string]interface{}, status int) (err error) {
	_, err = a.AccessAPI("food/sku/sellStatus", false, map[string]interface{}{
		KeyAppPoiCode: poiCode,
		"food_data":   foodData,
		"status":      status,
	})
	return err
}

// GetSkuPurchasePrice 获取商品进货价
func (a *API) GetSkuPurchasePrice(poiCode, skuId string) {
	data, err := a.AccessAPI("retail/purchase/price/list", true, map[string]interface{}{
		KeyAppPoiCode: poiCode,
		"sku_id":      skuId,
		"page_size":   20,
	})
	globals.SugarLogger.Debugf("data :== %s", utils.Format4Output(data, false))
	globals.SugarLogger.Debugf("err :== %s", utils.Format4Output(err, false))
}
