package cms

import (
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/business/partner/purchase/mtwm"
)

// GetMtSkuDetailInfo 获取美团商品信息,暂时只支持美团
func GetMtSkuDetailInfo(ctx *jxcontext.Context, vendorOrderID string, vendorSkuName string) (outSkuNameExt *model.SkuNameExt, err error) {
	orderDetail, err := partner.CurOrderManager.LoadOrder(vendorOrderID, model.VendorIDMTWM)
	if err != nil {
		return nil, err
	}

	// 获取订单详情
	mtApi := mtwm.GetAPI(orderDetail.VendorOrgCode, orderDetail.JxStoreID, orderDetail.VendorStoreID)
	mtOrderDetail, err := mtApi.OrderGetOrderDetail(utils.Str2Int64(vendorOrderID), false)
	if err != nil {
		return nil, err
	}
	var detail []map[string]interface{}
	if err = utils.UnmarshalUseNumber([]byte(mtOrderDetail["detail"].(string)), &detail); err != nil {
		return nil, err
	}

	mtCode := ""
	for _, product := range detail {
		if product["food_name"].(string) == vendorSkuName {
			if product["app_food_code"].(string) == "" {
				return nil, fmt.Errorf("[%s] 未获取到商品的美团code,无法创建商品", vendorSkuName)
			}
			mtCode = product["app_food_code"].(string)
			break
		}
	}
	if mtCode == "" {
		return nil, fmt.Errorf("[%s] 未获取到商品的美团code,无法创建商品", vendorSkuName)
	}

	// 获取美团商品详情
	mtSkuDetail, err := mtApi.RetailGet(orderDetail.VendorStoreID, mtCode)
	if err != nil {
		return nil, err
	}

	skuName := &model.SkuNameExt{
		SkuName: model.SkuName{
			Prefix:          "",
			Name:            mtSkuDetail.Name,
			Upc:             utils.String2Pointer(mtSkuDetail.UpcCode),
			Status:          1,
			CategoryID:      291, // ?
			IsGlobal:        1,
			Unit:            mtSkuDetail.Unit,
			Price:           utils.Float64TwoInt(mtSkuDetail.Price * float64(100)),
			DescImg:         mtSkuDetail.PictureContents,
			JdsStockSwitch:  1,
			PreparationTime: 0,
			MtAttribute:     "",
			// 京西创建是传入的属性
			// [{"attr_id":"1200000088","attr_name":"品牌","valueList":[{"value":"其他"}]},{"attr_id":"1200000135","attr_name":"包装方式","valueList":[{"value_id":"1300000231","value":"袋装"}]}]
			// 美团获取的属性,不太好转换,暂时不弄
			// [{"attrId":1200000202,"attrName":"品种","setAttrId":true,"setAttrName":true,"setValueList":true,"valueList":[{"setValue":true,"setValueId":true,"value":"二荆条","valueId":1300015565}],"valueListIterator":[{"$ref":"$[0].valueList[0]"}],"valueListSize":1},{"attrId":1200000135,"attrName":"包装方式","setAttrId":true,"setAttrName":true,"setValueList":true,"valueList":[{"setValue":true,"setValueId":true,"value":"散装","valueId":1300000232}],"valueListIterator":[{"$ref":"$[1].valueList[0]"}],"valueListSize":1}]
		},
		Skus:           nil,
		MtwmCategoryID: utils.Int2Str(mtSkuDetail.TagID),
	}

	skus := make([]*model.SkuWithVendor, 0, 0)
	for _, mtSks := range mtSkuDetail.SkuList {
		sku := &model.SkuWithVendor{
			Sku: &model.Sku{
				MinOrderCount:  utils.Str2Int(mtSks.MinOrderCount),
				LadderBoxNum:   utils.Str2Int(mtSks.LadderBoxNum),
				LadderBoxPrice: utils.Float64TwoInt(utils.Str2Float64(mtSks.Price) * float64(100)),
				SpecQuality:    float32(utils.Str2Float64(mtSks.Weight)),
				Weight:         utils.Str2Int(mtSks.Weight),
				SpecUnit:       mtSks.Unit,
				Status:         1,
				Comment:        "",
				EclpID:         "",
			},
		}
		skus = append(skus, sku)
	}
	skuName.Skus = skus

	mTImg2Jx(skuName, mtSkuDetail.PictureList)
	return AddSkuName(ctx, skuName, ctx.GetUserName())
}

func mTImg2Jx(sku *model.SkuNameExt, pictureList []string) {
	switch len(pictureList) {
	case 0:
		return
	case 1:
		sku.Img = pictureList[0]
	case 2:
		sku.Img = pictureList[0]
		sku.Img2 = pictureList[1]
	case 3:
		sku.Img = pictureList[0]
		sku.Img2 = pictureList[1]
		sku.Img3 = pictureList[2]
	case 4:
		sku.Img = pictureList[0]
		sku.Img2 = pictureList[1]
		sku.Img3 = pictureList[2]
		sku.Img4 = pictureList[3]
	case 5:
		sku.Img = pictureList[0]
		sku.Img2 = pictureList[1]
		sku.Img3 = pictureList[2]
		sku.Img4 = pictureList[3]
		sku.Img5 = pictureList[4]
	}
}
