package jdshop

import (
	"git.rosy.net.cn/baseapi/platformapi/jdshopapi"
	"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/model/dao"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/business/partner/putils"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
)

var (
	CurPurchaseHandler *PurchaseHandler
)

type PurchaseHandler struct {
	partner.BasePurchasePlatform
	putils.DefSingleStorePlatform
}

func init() {
	if api.JdShopAPI != nil {
		CurPurchaseHandler = New()
		partner.RegisterPurchasePlatform(CurPurchaseHandler)
	}
}

func New() (obj *PurchaseHandler) {
	obj = new(PurchaseHandler)
	obj.ISingleStoreStoreSkuHandler = obj
	return obj
}

func getAPI(appOrgCode string) (apiObj *jdshopapi.API) {
	if appOrgCode == "" {
		globals.SugarLogger.Warnf("getAPI appOrgCode is empty")
	}
	apiObj = partner.CurAPIManager.GetAPI(model.VendorIDJDShop, appOrgCode).(*jdshopapi.API)
	//if appOrgCode == "2" {
	if configs, err := dao.QueryConfigs(dao.GetDB(), "jdsCookie2", model.ConfigTypeCookie, ""); err == nil {
		apiObj.SetCookieWithStr(configs[0].Value)
	}
	//}
	return apiObj
}

//func getAPI2(appOrgCode string) (apiObj *jdshopapi.API) {
//	apiObj = jdshopapi.New("", "", "")
//	configs, _ := dao.QueryConfigs(dao.GetDB(), "jdsCookie", model.ConfigTypeCookie, "")
//	apiObj.SetCookieWithStr(configs[0].Value)
//	return apiObj
//}

func GetAPI(appOrgCode string) (apiObj *jdshopapi.API) {
	apiObj = partner.CurAPIManager.GetAPI(model.VendorIDJDShop, appOrgCode).(*jdshopapi.API)
	if configs, err := dao.QueryConfigs(dao.GetDB(), "jdsCookie2", model.ConfigTypeCookie, ""); err == nil {
		apiObj.SetCookieWithStr(configs[0].Value)
	}
	return apiObj
}

func (p *PurchaseHandler) GetVendorID() int {
	return model.VendorIDJDShop
}

func (p *PurchaseHandler) UploadImg(ctx *jxcontext.Context, vendorOrgCode, imgURL string, imgData []byte, imgName string, imgType int) (imgHint string, err error) {
	if globals.EnableJdShopWrite {
		if imgType > model.ImgTypeLocal {
			result, err := getAPI(vendorOrgCode).UploadPicture(imgData, 0, imgName)
			if err == nil {
				imgHint = result.PictureURL
			}
		}
	} else {
		imgHint = utils.GetUpperUUID()
	}
	return imgHint, err
}

func (p *PurchaseHandler) GetVendorCategories(ctx *jxcontext.Context) (vendorCats []*model.SkuVendorCategory, err error) {
	result, err := api.JdShopAPI.FindVendorCategories()
	for _, v := range result {
		cat := &model.SkuVendorCategory{
			VendorID:         model.VendorIDJDShop,
			Name:             v.Name,
			Level:            v.Lev,
			VendorCategoryID: utils.Int2Str(v.ID),
		}
		if v.Lev > 1 {
			cat.ParentID = utils.Int2Str(v.Fid)
			cat.IsLeaf = 1
		}
		vendorCats = append(vendorCats, cat)
	}
	return vendorCats, err
}

func (p *PurchaseHandler) GetOrderRider(vendorOrgCode, vendorStoreID string, param map[string]interface{}) (err error) {
	return nil
}
